使用mongodb获取一个与子文档字段中的条件匹配的子文档

时间:2014-12-18 11:22:44

标签: php mongodb

如果我有一组像下面这样的MongoDB文档,我该怎么做才能获得只返回一个文档的find()结果

my document look like this

 {
        "_id" : ObjectId("549182eb1579f6340c000000"),
        "cmp_userid" : "549129de471b083409000000",
        "company_about" : "web  development",
        "company_address" : "kochin",
        "company_location" : "kochin",
        "company_name" : "innomind",
        "company_state" : "Kerala",
        "company_type" : "software",
        "details" : [ 
            {
                "int_id" : ObjectId("549291dc1579f60c04123321"),
                "title" : "php trainee",
                "description" : "innomind",
                "responsibilities" : "innomind resp",
                "qualification" : "btech",
                "sdate" : "1/1/2014",
                "edate" : "2/1/2014",
                "paid" : "Paid",
                "salary" : "1111"
            }, 
            {
                "int_id" : ObjectId("549291dc1579f60c04234300"),
                "title" : "php trainee innomind",
                "description" : "fsxgbhfgbh",
                "responsibilities" : "gbhfgbhsdf",
                "qualification" : "btech",
                "sdate" : "1/1/2014",
                "edate" : "2/1/2014",
                "paid" : "unpaid",
                "salary" : "1222"
            }, 
            {
                "int_id" : ObjectId("549291dc1579f60c04221100"),
                "title" : "web development",
                "description" : "web development innomind",
                "responsibilities" : "web development innomind",
                "qualification" : "asdf",
                "sdate" : "1/1/2014",
                "edate" : "2/1/2014",
                "paid" : "unpaid",
                "salary" : "1222"
            }, 
            {
                "int_id" : ObjectId("549291dc1579f60c04011110"),
                "title" : "web development",
                "description" : "fgfgfgfg",
                "responsibilities" : "dddddd",
                "qualification" : "asdf",
                "sdate" : "1/1/2014",
                "edate" : "2/1/2014",
                "paid" : "unpaid",
                "salary" : "1222"
            }, 
            {
                "int_id" : ObjectId("549291dc1579f60c04000000"),
                "title" : "seo",
                "description" : "gdsxfgsdfg",
                "responsibilities" : "fgsdf sdfgsdg.",
                "qualification" : "btech",
                "sdate" : "1/1/2014",
                "edate" : "2/1/2014",
                "paid" : "unpaid",
                "salary" : "1111"
            }, 
            {
                "int_id" : ObjectId("5492b2001579f60c04000002"),
                "title" : "bpo",
                "description" : "fvbhxdfvbhxdfgb",
                "responsibilities" : "fgsdzfgsdf",
                "qualification" : "btech",
                "sdate" : "1/1/2014",
                "edate" : "2/1/2014",
                "paid" : "unpaid",
                "salary" : "1111"
            }
        ]
    }

我想从详细子文档中只获取一个文档_id = ObjectId(“549182eb1579f6340c000000”) 和int_id = ObjectId(“549291dc1579f60c04123321”)     预期结果

_id:ObjectId("549182eb1579f6340c000000"),

"details" : [ 
        {
            "int_id" : ObjectId("549291dc1579f60c04123321"),
            "title" : "php trainee",
            "description" : "innomind",
            "responsibilities" : "innomind resp",
            "qualification" : "btech",
            "sdate" : "1/1/2014",
            "edate" : "2/1/2014",
            "paid" : "Paid",
            "salary" : "1111"
        }]

but i can't get any results please help me

1 个答案:

答案 0 :(得分:0)

由于它是一个嵌入式文档,并且您只想要与查询匹配的嵌入式文档,因此您必须使用 $ unwind

db.can.aggregate({$unwind:"$details"},{$match:{_id: ObjectId("549182eb1579f6340c000000"),"details.int_id" : ObjectId("549291dc1579f60c04123321")}})