0

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

1 回答 1

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

您很可能正在寻找elemMatch. 查看文档

于 2013-08-23T07:07:32.697 回答