Re: How to filter array in subdocument with MongoDB

2,244 views
Skip to first unread message

Rodrigo Machado

unread,
Feb 27, 2013, 11:46:10 AM2/27/13
to mongod...@googlegroups.com
Hi salanyot,

You can do something like this but I think that this is not very efficient: (Probably another schema design would be better)

db.test.aggregate(
  {$match: {_id: ObjectId("512e28984815cbfcb21646a7")}},
  {$unwind: '$list'},
  {$match: {"list.a": {$gt: 3}}},
  {$group: {_id: "$_id", "list": {$push:"$list"}}}
);

The $unwind will separate the elements in the array into its own documents, the we match for what we want, then use a $group with $push to, err, group the elements again into an array.

Hope this helps.

Cheers,
Rodrigo Machado

On Wednesday, February 27, 2013 1:21:55 PM UTC-3, salanyot suwannachoti wrote:
Hello everyone.
I have array in subdocument like this

    {
    "_id" : ObjectId("512e28984815cbfcb21646a7"),
    "list" : [
    {
    "a" : 1
    },
    {
    "a" : 2
    },
    {
    "a" : 3
    },
    {
    "a" : 4
    },
    {
    "a" : 5
    }
    ]
    }

Can I filter subdocument for a > 3

My expect result below

    {
    "_id" : ObjectId("512e28984815cbfcb21646a7"),
    "list" : [
    {
    "a" : 4
    },
    {
    "a" : 5
    }
    ]
    }

I try to use $elemMatch but returns the first matching element in the array

My query 
    db.test.find( { _id" : ObjectId("512e28984815cbfcb21646a7") }, { 
    list: { 
    $elemMatch: 
    { a: { $gt:3 } 
   
   
    } )

The result return one element in array

    { "_id" : ObjectId("512e28984815cbfcb21646a7"), "list" : [ { "a" : 4 } ] }

and I try to use aggregate with $match but not work

    db.test.aggregate({$match:{_id:ObjectId("512e28984815cbfcb21646a7"), 'list.a':{$gte:5}  }})

It's return all element in array

    {
        "_id" : ObjectId("512e28984815cbfcb21646a7"),
        "list" : [
            {
                "a" : 1
            },
            {
                "a" : 2
            },
            {
                "a" : 3
            },
            {
                "a" : 4
            },
            {
                "a" : 5
            }
        ]
    }

Can I filter element in array to get result as expect result?

Sam

unread,
Aug 1, 2013, 11:16:24 PM8/1/13
to mongod...@googlegroups.com
Hi,

I am also having the same problem.

I have Big documents. I need to query on array of elements.
I am getting all elements instead of matched elements in the array.

Do we have any better solution?

Please help me.

Thanks,
Samanth

Asya Kamsky

unread,
Aug 5, 2013, 1:04:09 PM8/5/13
to mongodb-user
You can take a look at $elemMatch projection operator
http://docs.mongodb.org/manual/reference/projection/elemMatch/

That will allow you to return the first matching array element. If you
need multiple elements then it's likely that a different schema might
serve your use case better - in general, embedding is best when you
normally want all or none of the array elements - though it also works
well when you want just one.

Asya
> --
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb
>
> ---
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-user...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages