2

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 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?

2 Answers 2

2

See "Array" header

Sign up to request clarification or add additional context in comments.

3 Comments

that did not help :( do you have another example (in java)? or maybe a bug on jira to show that its not possible? TIA
Using a forEach for each nesting of array works. but what I wish, is to return a particular block, when some data inside it is what i want. eg: person.country== "india" should return blocks with id:5435df and id: dfg434.
In that case you're going to want to put them into an array. See this thread I open with the developers on their forum. groups.google.com/group/mongodb-user/browse_thread/thread/…
0

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})})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.