比较查询运算符ObjectId<->日期 [英] Comparison query operator ObjectId <-> Date
问题描述
是否可以针对ObjectId
执行Date
的比较查询$gt
,$lt
等,反之亦然? mongodb驱动程序会自动强制转换吗?此mongodb服务器会自动强制转换吗?
Is it possible to execute comparison queries $gt
, $lt
, etc. of a Date
against an ObjectId
and vice versa? Does the mongodb driver cast this automatically? Does this mongodb server cast this automatically?
推荐答案
是,不是.
有可能在基于JavaScript的方法下从ObjectId值获取日期,您可以在其中进行比较.同样也应该构造一个给定特定日期值的ObjectId,但是看不到它的实用性.但是所有这些在日期之前都是有效且必不可少的:
It is possible under the JavaScript based methods to get a date from the ObjectId value in which you can use for comparisons. It also should be possible to construct an ObjectId given a specific date value as well, but not seeing the utility of that though. But all are valid and essentialy by date:
ObjectId("53473d87cb495e216c982929") > ObjectId("53473e57cb495e216c98292a")
ObjectId("53473d87cb495e216c982929").getTimestamp() >
ObjectId("53473e57cb495e216c98292a").getTimestamp()
ObjectId("53473d87cb495e216c982929").getTimestamp() >
ISODate("2014-04-11T00:55:35Z")
所以这样的形式也可以使用,即使实际上不是那么好声明:
So forms such as this will work, even if really not that great a statement:
db.collection.find({
"$where": function() {
return this._id.getTimestamp() > new Date("2014-01-01");
}
}
对于_id
的创建",它们要么在驱动程序"中完成,要么由用户显式完成,或者如果仍然省略,则服务器将生成一个.
As for the "creation" of _id
's they are either done in the "driver" or explicitly by the user, or if still omitted the server will generate one.
这篇关于比较查询运算符ObjectId<->日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!