"user_crawls": {
"0": {
"_id": ObjectId("4e4b5e1c151c0d0336000093"),
"rand_id": "kPxMuXOY8Jfh6nXt",
"network": "tw",
"sourcenetwork": "874777",
"sourceName": "Krishna",
},
"1": {
"_id": ObjectId("4e4b5e1c151c0d0336000094"),
"rand_id": "kPxMuXOY8Jfh6nXt",
"network": "fb",
"sourcenetwork": "145875",
"sourceName": "Krishna",
},
"2": {
"_id": ObjectId("4e4b5e1c151c0d0336000095"),
"rand_id": "kPxMuXOY8Jfh6nXt",
"network": "fb",
"sourcenetwork": "145875",
"sourceName": "Ram",
}
我想选择网络为fb且sourcename为Krishna的文档。从abouve数据通常我想得到结果是id为
的第二个doc4e4b5e1c151c0d0336000094
但我得到了所有记录
我使用以下代码收集数据
DdNetworkCrawlLink.limit(10).all(:conditions => {'user_crawls.network' => "fb",'user_crawls.sourceName' => "Krishna")
感谢您的时间
Sreeraj
答案 0 :(得分:0)
在Mongo中查询时,您只能查询根文档。所以它也在MongoMapper中。
要获得匹配的嵌入式文档,您必须先查询所有根文档,然后从中嵌入文档:
DdNetworkCrawlLink
.limit(10)
.all('user_crawls.network' => "fb",'user_crawls.sourceName' => "Krishna")
.map do |dbncl|
dcncl.user_crawls.select { |uc| uc.network == "fb" && uc.sourceName == "Krishna" }
end.flatten
由于上述代码非常复杂,因此建议仅在嵌入式文档与其父级同时使用/显示/需要时才嵌入文档。
答案 1 :(得分:-1)
我想你错过了':'char。 在关于mongomapper的文档中,它说
Patient.all(:last_name => 'Johnson', :order => :last_name.asc)