Fre*_*iot 44 mongodb node.js isodate
我使用nodejs与node-mongodb-native驱动程序(http://mongodb.github.io/node-mongodb-native/).
我有一个日期属性存储为ISODate类型的文档.
通过nodejs,我正在使用此查询:
db.collection("log").find({
localHitDate: {
'$gte': '2013-12-12T16:00:00.000Z',
'$lt': '2013-12-12T18:00:00.000Z'
}
})
Run Code Online (Sandbox Code Playgroud)
它什么都不返回.为了使它工作,我需要做以下事情:
db.collection("log").find({
localHitDate: {
'$gte': ISODate('2013-12-12T16:00:00.000Z'),
'$lt': ISODate('2013-12-12T18:00:00.000Z')
}
})
Run Code Online (Sandbox Code Playgroud)
但是ISODate在我的nodejs代码中无法识别.
那么如何通过我的nodejs程序对mongo日期字段进行查询呢?
谢谢
dam*_*hat 63
您可以使用new Date('2013-12-12T16:00:00.000Z')的node.js;
new 是必须的,因为Date()已经用于返回日期字符串.
ISODate是在mongodb中隐藏的,你可以在mongodb控制台中使用它,但它可以在不同的编程语言中使用.
小智 5
你可以用这个,对我来说效果很好
//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){
}
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45149 次 |
| 最近记录: |