MongoDB how to compare two IDs fields on same document
To compare two fields on same document in MongoDB you can use a $where (just be aware it will be fairly slow, has to execute Javascript code on every record) :
db.my_collection.find( "this.fieldA > this.fieldB" } );
(check this thread on StackOverflow)
In my case I want to find all the documents where a field id_A is equal to field id_B, so I tried this:
db.my_collection.find( "this.id_A == this.id_B" } );
but this not works because you can’t overload == in javascript, so the way to do is:
db.my_collection.find( "this.id_A.equals(this.id_B)" } );
And from ruby with MongoID you use:
MyCollection.where("$where" => "this.id_A.equals(this.id_B)")