mongodb查询从文档中选择多个子集

时间:2014-01-06 05:14:21

标签: mongodb subset

{
_id: ObjectId("52ca2d45b80de42808000001"),
id: "1111139048239",
name: "Bruce Lim",
first_name: "Bruce",
last_name: "Lim",
friends: [
    {
        id: "1913681",
        name: "John Sim",
        icon: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash2/1117702_1913681_1171369396_q.jpg",
        photos: [
            {
                src: "https://scontent-a.xx.fbcdn.net/hphotos-ash2/t1/230718_10150181312510976_7606555_n.jpg",
                lat: "38.2289",
                lng: "-85.7495"
            },
            {
                src: "https://scontent-b.xx.fbcdn.net/hphotos-frc3/230480_10150181312620976_3864544_n.jpg",
                lat: "38.2289",
                lng: "-85.7495"
            }
        ]
    },
    {
        id: "31925743892",
        name: "Mike Holloway",
        icon: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/211634_31925743892_1471358831_q.jpg",
        photos: [
            {
                src: "https://scontent-a.xx.fbcdn.net/hphotos-ash2/t1/230718_10150181312510976_7606555_n.jpg",
                lat: "38.2289",
                lng: "-85.7495"
            },
            {
                src: "https://scontent-b.xx.fbcdn.net/hphotos-frc3/230480_10150181312620976_3864544_n.jpg",
                lat: "38.2289",
                lng: "-85.7495"
            }
        ]
    },
    {
        id: "1954048",
        name: "Christiana Basset",
        icon: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/211634_1954048_1471358831_q.jpg",
        photos: [
            {
                src: "https://scontent-a.xx.fbcdn.net/hphotos-ash2/t1/230718_10150181312510976_7606555_n.jpg",
                lat: "38.2289",
                lng: "-85.7495"
            },
            {
                src: "https://scontent-b.xx.fbcdn.net/hphotos-frc3/230480_10150181312620976_3864544_n.jpg",
                lat: "38.2289",
                lng: "-85.7495"
            }
        ]
    }
]

}

当我用

查询这些文档的集合时
db.mapping.find(
    {"id":"1111139048239"},
    {"friends":{
        $elemMatch:{"id":"1913681"}
    }}
)

我得到一个匹配的friend子集。

{
        "_id" : ObjectId("52ca2d45b80de42808000001"),
        "friends" : [
                {
                        "id" : "1913681",
                        "name" : "John Sim",
                        "icon" : "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash2/1117702_1913681_1171369396_q.jpg",
                        "photos" : [
                                {
                                        "src" : "https://scontent-a.xx.fbcdn.net/hphotos-ash2/t1/230718_10150181312510976_7606555_n.jpg",
                                        "lat" : "38.2289",
                                        "lng" : "-85.7495"
                                },
                                {
                                        "src" : "https://scontent-b.xx.fbcdn.net/hphotos-frc3/230480_10150181312620976_3864544_n.jpg",
                                        "lat" : "38.2289",
                                        "lng" : "-85.7495"
                                }
                        ]
                }
        ]
}

如何选择多个子集。

db.mapping.find(
    {"id":"1111139048239"},
    {"friends":{
        $elemMatch:{"id":"1913681", "id":"1954048"}
    }}
)

db.mapping.find(
    {"id":"1111139048239"},
    {"friends":{
        $elemMatch:{"id":"1913681"},
        $elemMatch:{"id":"1954048"}
    }}
)

只获取最后一场比赛,在这种情况下为1954048。我如何得到两者 - 1913681,1954048?

1 个答案:

答案 0 :(得分:2)

mongodb中find的一般语法是

db.collection.find(<criteria>,<projection>)

在你的情况下,

 criteria: id should be "1111139048239"
 projection: listing friends who have id 1913681, 1954048
elemMatch只能获得元素的第一个存在,并且当为同一属性给出多个值时,它将仅显示文档中最后执行的elemMatch。

我建议你去聚合。它将帮助您获得所需的输出。

db.mapping.aggregate([
          {$match:{id:"1111139048239"}}, // STEP 1
          {$unwind:"$friends"}, // STEP 2 
          {$match:{"friends.id":{$in:["1913681","1954048"]}}} // STEP 3
                    ])

执行:

 STEP 1: Selects the document with id "1111139048239"
 STEP 2: Unwinds the friends array in the selected document and 
         create multiple documents as per the size of friends array.
         In this case 3 documents.
 STEP 3: Select documents which has a friends id "1913681", "1954048".
         In this case 2 documents will be selected. Append values to array to get
         more documents as output
         {"friends.id":{$in:["1913681","1954048",etc]}