使用rails的ActiveRecord获取json的值
使用ActiveRecord提取json值。
我用”mongoDB查询嵌套文档”作为关键词进行了搜索。
以下是两个关于MongoDB和嵌入式文档查询的Stack Overflow问题链接的简述:
1. https://stackoverflow.com/questions/3954520/mongoid-mongodb-and-querying-embedded-documents
这个问题讨论了如何使用Mongoid和MongoDB进行嵌入式文档查询。
2. https://stackoverflow.com/questions/24749312/mongoid-querying-embedded-docs/24749539
这个问题中,关于Mongoid如何查询嵌入式文档的讨论着重于特定的用例和查询方法。
如果将层次化的JSON数据用点号连接起来,似乎可以获取到该数据。
我这次想要做的就是从cptrans内提取只有特定key的值不为空的记录。
网络模型
{
_id: 1,
cptrans: {
_id: ObjectId('61712564a3519d0001fe97aa'),
mcc: 440,
mnc: 51,
tracking_area_code: 41481,
cell_id: '68030469',
location_area_code: 0,
rsrp: -66,
rssi: -41,
rsrq: -7,
rssnr: 0,
traffic_channel: 0,
band: 18,
radio_interface: 'LTE',
band_width: '0',
}
}
如果想从存储在MongoDB中的Networks模型中提取特定键值不为null的记录,可以进行如下操作。
结果
请将想要提取的关键词放入 rank_type。
Nerwork.where("cptrans.#{rank_type}" => { '$ne' => nil })
为了能够根据不同的key进行提取,我创建了一个scope,并采用了以下类似的方法来处理。
如果对scope不了解的人可以参考这个链接:https://qiita.com/ngron/items/14a39ce62c9d30bf3ac3
scope :rank_type, ->(rank_type) {
case rank_type
when :rsrp, :rsrq, :rssnr then where('cptrans' => { '$ne' => nil })
when :rssi, :nr_rssi, :nr_rsrp, :nr_rsrq then where("cptrans.#{rank_type}" => { '$ne' => nil })
when :tx_rate then where('iperf' => { '$ne' => nil })
end
}
请提供以下内容的中文释义,仅需要一种选项:
– “Can you give me a sandwich?”
– “Where is the nearest hospital?”
– “What time is the meeting?”
– “How much does this shirt cost?”
– “I am from the United States.”
– “Please close the door.”
– “Do you have any vegetarian options?”
– “How far is the airport from here?”
– “Could you recommend a good restaurant?”
– “What is your phone number?”
– “Can you help me with directions to the train station?”
– “Is there a pharmacy nearby?”
– “What is the weather like today?”
– “I would like a glass of water.”
– “Do you accept credit cards?”
范围
https://qiita.com/ngron/items/14a39ce62c9d30bf3ac3
获取MongoDB哈希值的方法:
https://stackoverflow.com/questions/3954520/mongoid-mongodb-and-querying-embedded-documents
https://stackoverflow.com/questions/24749312/mongoid-querying-embedded-docs/24749539