MongoDB, finding all documents where property id equals to record id

Viewed 191

I've got collection like this:

{
   "_id" : ObjectId("546089aaf83a94de268a35c3"),
  "parent" : {
        "$ref" : "someRef",
        "$id" : ObjectId("546089aaf83a94de268a35c3"),
        "$db" : "someDB",
    }
}

How can I look for all the record where _id is equal to parent.id?

Tried find({"parent.$id": "_id"}) but obviously that can't be it

1 Answers

The parent here holds the database reference. Thus we need to call the getter method of the reference to get its object ID.

The following query can get us the expected output:

db.collection.find({
    $where: function(){
        return this._id.toString() == this.parent.getId().toString();
    }
}).pretty()
Related