我有一个这样的集合:
{
_id: ObjectId("53c4000f821bc31e3ec90140"),
foo: "bar",
created: ISODate("2014-07-09T02:15:49.000+02:00"),
updated: ISODate("2014-07-09T02:15:49.000+02:00")
},
{
_id: ObjectId("53c4000f821bc4dw3ec90140"),
foo: "bar",
created: ISODate("2014-07-09T02:15:49.000+02:00"),
updated: ISODate("2014-07-09T02:19:49.000+02:00")
},
现在我想选择创建的集合的所有元素==已更新。在此示例中,查询应仅返回第一个元素。
我试过这个:
db.foo.find( {$where: "this.created == this.updated"})
但我没有结果: - (
答案 0 :(得分:0)
对象比较不像JavaScript那样。您需要比较“值”而不是整个对象:
db.foo.find(function(){ return this.created.valueOf() == this.updated.valueOf() })
所以.valueOf()
是关键所在。当你没有其他条件时,这只是$where
的简写。