How can I query for an Array of DBRefs as embedded document?

1,176 views
Skip to first unread message

fbaaken

unread,
Jul 10, 2011, 12:04:00 PM7/10/11
to mongodb-user
Hi guys!

I am relatively new to MongoDB, sorry for the seemingly newbish
question:

I have an object which holds an array of DBrefs:

{
id : ObjectId,
parents : [ DBRef ]
}

Now, I would like to query based on the IDs of the DBrefs. That is:

find({
parents : {
"$id" : {
$in : [ ObjectId('4e19be8b326a023a40120000'),
ObjectId('4e19be8b326a023a40120001') ]
}
}
})

But it says the "$id" operator is invalid. Now, how DO I query if a
DBRef ID is in an array?

Thanks for your help (much appreciated, stuck on this for some days
now).

Cheers!

Scott Hernandez

unread,
Jul 10, 2011, 4:53:29 PM7/10/11
to mongod...@googlegroups.com
http://www.mongodb.org/display/DOCS/Database+References

You use a new DBRef in the mongo javascript shell:

> new DBRef("coll", "id")
{ "$ref" : "coll", "$id" : "id" }

> var refs = [new DBRef(...), new DBRef(...)]
> db.coll.find({parents : { $in : refs }})

> --
> 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.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

fbaaken

unread,
Jul 11, 2011, 7:59:44 AM7/11/11
to mongodb-user
Yup, thanks for your help!

On Jul 10, 10:53 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> http://www.mongodb.org/display/DOCS/Database+References
>
> You use a new DBRef in the mongo javascript shell:
>
> > new DBRef("coll", "id")
>
> { "$ref" : "coll", "$id" : "id" }
>
>
>
>
>
>
>
> > var refs = [new DBRef(...), new DBRef(...)]
> > db.coll.find({parents : { $in : refs }})
Reply all
Reply to author
Forward
0 new messages