MongoDB + nodejs:如何查询 ISODate 字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20561381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB + nodejs : how to query ISODate fields?
提问by Fred Mériot
I am using nodejs with the node-mongodb-native driver (http://mongodb.github.io/node-mongodb-native/).
我正在将 nodejs 与 node-mongodb-native 驱动程序(http://mongodb.github.io/node-mongodb-native/)一起使用。
I have documents with a date property stored as ISODatetype.
我有一个日期属性存储为ISODate类型的文档。
Through nodejs, I am using this query:
通过nodejs,我正在使用这个查询:
db.collection("log").find({
localHitDate: {
'$gte': '2013-12-12T16:00:00.000Z',
'$lt': '2013-12-12T18:00:00.000Z'
}
})
It returns nothing. To make it work I need to do the following instead:
它什么都不返回。为了使其工作,我需要执行以下操作:
db.collection("log").find({
localHitDate: {
'$gte': ISODate('2013-12-12T16:00:00.000Z'),
'$lt': ISODate('2013-12-12T18:00:00.000Z')
}
})
But ISODateis not recognized in my nodejs code.
但ISODate在我的 nodejs 代码中无法识别。
So how can I make a query against mongo date fields through my nodejs program?
那么如何通过我的 nodejs 程序对 mongo 日期字段进行查询?
Thank you
谢谢
回答by damphat
You can use new Date('2013-12-12T16:00:00.000Z')in node.js;
您可以new Date('2013-12-12T16:00:00.000Z')在node.js;
newis a must, because Date() is already use to return date string.
new是必须的,因为 Date() 已经用于返回日期字符串。
ISODate is concepted in mongodb, you can use it in mongodb console, but it can be different for different programming language.
ISODate 是 mongodb 中的概念,你可以在 mongodb 控制台中使用它,但是对于不同的编程语言它可能会有所不同。
回答by klevin
You can use this, for me worked perfectly
你可以用这个,对我来说效果很好
//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');
//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;
// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost/klevin';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to', url);
// Get the documents collection
var collection = db.collection('frames');
//We have a cursor now with our find criteria
var cursor = collection.find({
tv: 'tematv',
date_created: {"$gte": new Date("2015-10-01T00:00:00.000Z") , "$lt": new Date("2017-03-13T16:17:36.470Z") }});
//We need to sort by age descending
cursor.sort({_id: -1});
//Limit to max 10 records
cursor.limit(50);
//Skip specified records. 0 for skipping 0 records.
cursor.skip(0);
//Lets iterate on the result
cursor.each(function (err, doc) {
if (err) {
console.log(err);
} else {
console.log('Fetched:', doc);
if(doc !== null){
}
}
});
}
});
回答by KARTHIKEYAN.A
we need to use new Date()is best option to get the data.
我们需要使用new Date()是获取数据的最佳选择。
db.getCollection('orders').aggregate([
{
'$match': {
$and: [
{
status: 'UNASSIGNED'
},
{
plannedDeliveryDate: {
'$eq': new Date('2017-10-09')
}
}
]
}
},
{
$lookup: {
from: "servicelocations",
localField: "serviceLocationId",
foreignField: "serviceLocationId",
as: "locations"
}
},
{
$unwind: "$locations"
},
{
"$project": {
"accountId": 1,
"orderId": 1,
"serviceLocationId": 1,
"orderDate": 1,
"description": 1,
"serviceType": 1,
"orderSource": 1,
"takenBy": 1,
"plannedDeliveryDate": 1,
"plannedDeliveryTime": 1,
"actualDeliveryDate": 1,
"actualDeliveryTime": 1,
"deliveredBy": 1,
"size1": 1,
"size2": 1,
"size3": 1,
"jobPriority": 1,
"cancelReason": 1,
"cancelDate": 1,
"cancelBy": 1,
"reasonCode": 1,
"reasonText": 1,
"status": 1,
"lineItems": 1,
"locations": {
"lng": "$locations.location.lng",
"lat": "$locations.location.lat"
}
}
}
])

