MazMat
MazMat

Reputation: 2094

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

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

Upvotes: 2

Views: 263

Answers (1)

Himanshu Sharma
Himanshu Sharma

Reputation: 3010

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

Upvotes: 2

Related Questions