How to get a key type in a mongodb collection on a ruby ​​on rails

Initially, I import the data into a datatype Hash , since I have a column called schedule, I need the type of the specific β€œschedule” column from my db. my verified code

schedule = scheduleWorld.all schedule.each do |sec| sec.attributes.each do |key, value, type| puts "%%%%%%%%%%%%%%%%%%%%%%%%%" puts key puts value puts type puts "%%%%%%%%%%%%%%%%%%%%%%%%%" end end 

To this, I get null in the type, I tried another code

  schedule = scheduleWorld.where({schedule:{$type=>2}}) 

error

 undefined method `specify' for nil:NilClass (eval):2:in `where' 

Anyone have an idea about this?

+4
source share
1 answer

A type is only one key-value pair in the hash attributes of a mongodb document. Therefore, you can always get it like this. I am using Mongoid. The exact name may differ from your ORM.

  schedule = scheduleWorld.all schedule.each do |sec| type = sec.attributes["_type"] sec.attributes.each do |key, value| puts "%%%%%%%%%%%%%%%%%%%%%%%%%" puts key puts value puts "%%%%%%%%%%%%%%%%%%%%%%%%%" end end 
0
source

Source: https://habr.com/ru/post/1483801/


All Articles