查询mongoDB中包含哈希的数组字段?

时间:2013-08-23 05:21:48

标签: mongodb ruby-on-rails-3.1

从数据库中获取数据时遇到问题。我在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})

1 个答案:

答案 0 :(得分:0)

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

您很可能正在寻找elemMatch。查看docs