开发者

Accessing array in a mongoDB collection

I have a collection as follows:

{ "_id" : ObjectId("4c9c63a95d765f996ca8dff4"), "count" : "28", "id" : "5565", "person" : [
    {
        "id" : "5435df",
        "name" : {
            "fn" : "abc",
            "ln" : "xyz"
        },
        "sex" : "m",
        "location" : {
            "country" : "india"
        }

    },
    {
        "id" : "dfg434",
        "name" : {
            "fn" : "def",
            "ln" : "pqr"
        },
        "sex" : "f",
        "location" : {

            "country" : "india"
        }

    }
.
.
.

]
}

the person is an array, with mentioned fields. i wish to find people with a particular id AND country. only those "people" records should be returned. How do i go 开发者_C百科about doing that? I am using java. Is the structure correct? Should I change the array structure, into something else?

thanks.

EDIT: How do access rules change when brackets change, in this case, i have used [], where i could have used {}. what changes?


See "Array" header


Maybe try aggregate from mongo v2.2+ :

    fagg=db.collection.aggregate([{$unwind: "$person"},
    {$project: {name:"$person.name",sex:"$person.sex"}},
    {$match: {sex: "f"}}])

    fagg.result.forEach(function(o){
    db.person.insert({name: o.name, sex: o.sex})})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜