As part of an upgrade script, I want to use $where
to find docs where the id matches one of the fields. I know that's slow because it's done with a cursor, but that's OK with me; I'm only going to run it once. But I can't get it to work:
> db.things.drop()
true
> stuff = {}
{ }
> db.things.save(stuff)
> stuff.original_id = stuff._id
ObjectId("4ff5f9f97fadec5abb7b5392")
> db.things.save(stuff)
//why doesn't this return anything?
> db.things.find({$where: "this._id == this.original_id"})
//interstingly, this works fine
> db.things.find({$where: "this._id == this._id"})
{ "_id" : ObjectId("4ff5f9f97fadec5abb7b5392"), "original_id" : ObjectId("4ff5f9f97fadec5abb7b5392") }
What's wrong here? Why can't I compare _id
to original_id
?