Node.js to MongoDB: find by Date

回眸只為那壹抹淺笑 提交于 2019-12-06 14:18:38

You do not need any of this wrapping. A date is a date:

var zeroth = {$or:[ {start: new Date(), {users:{$size:2}} ]}; 

Now of course if those "dates" in your document are actually "strings" and not proper date types then that is your problem. And what you really need to to is fix those values so they are real dates.

This applies to any language implementation, where you should be working with the native "date" type and let the driver do the conversion for you.

How to determine if the field is a string

Well the clear difference is when you look at a document in the mongo shell, if it is a real BSON date type then it will look like this:

"start": ISODate("2014-03-31T08:47:48.946Z"),

If that is not clear enough then there is the $type operator which you can use in a query like this:

db.collection.find({ "start": { "$type": 2 } }).count()

It is also not MongoDB that is doing this if it is a string, but rather bad implementation in the application or import that was responsible from creating this. Which was what the points made in the initial response were all about.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!