2013-08-23 86 views
0

我在從數據庫中提取數據時遇到問題。我在mongodb數據庫中有一個數組類型列,其中我保存了每個數組索引中的哈希類型數據。我必須根據數據庫中的散列匹配來獲取數據。查詢在mongoDB中包含散列的數組字段?

我在數據庫中的數據被保存這樣

{ 
"_id" : ObjectId("521603970ff5fa4b47000018"), 
"message_text" : "#cricket #badminton #sachin #tendulkar #ibl #masterblaster #srt Cricketing world lights up IBL http://t.co/RWskRM2EQf via @msncricket", 
"source" : "<a href=\"http://twitter.com/tweetbutton\" rel=\"nofollow\">Tweet Button</a>", 
"status_id" : NumberLong("370521615638884352"), 
"status_created_at" : "2013-08-22 17:53:31 +0530", 
"user_id" : "31677680", 
"screen_name" : "msncricket", 
"name" : "MSN Cricket", 
"followers_count" : 11515, 
"friends_count" : 303, 
"query" : [ 
    { 
     "filterId" : "5215b40c0ff5fa111e000001", 
     "subfilterId" : "60728003610375795", 
     "type" : "keyword", 
     "source" : "twitter", 
     "monitoring_type" : false, 
     "search_type" : false, 
     "searchParameter" : "sachin", 
     "geo" : "india", 
     "language" : "en" 
    } 
] 
} 

查詢列是陣列類型列。

在尋找我做一個哈希再次形成匹配它像如下

query = {"filterId"=>"5215b40c0ff5fa111e000001", "subfilterId"=>"60728003610375795",  "type"=>"keyword", "source"=>"twitter", "monitoring_type"=>false, "search_type" => false, "searchParameter"=>"sachin", "geo"=>"india", "language"=>"en"} 
@result = Statuses.where({query: query}) 

回答

0
db.collection.find({ "query": { $elemMatch: { "filterId": "5215b40c0ff5fa111e000001", "subfilterId": "60728003610375795" } } }); 

你最可能尋找elemMatch。檢查出docs

+0

我該如何在rails中運行查詢活動記錄 –

+0

我不使用ruby。對不起:(但現在你可以肯定谷歌如何做elemMatch在你的紅寶石mongo使用什麼樣的ODM或包裝。:) –

+0

其在mongodb控制檯也工作 –

相關問題