我一直在尝试使用应用程序中的日期范围来获取 node.js / mongoose 查询,但它没有奏效。因此,我将 [Mongo DB 食谱中的代码用于日期范围]:http ://cookbook.mongodb.org/patterns/date_range/并将其放入一个简单的 node.js 应用程序中,看看我是否可以让它工作(没有成功)。
这是代码:
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/tb');
var PostSchema = new mongoose.Schema(
title: String,
author: String,
content: String,
created_on: Date
});
Post = mongoose.model('Post', PostSchema);
var newPost = new Post({
"title" : "A blog post",
"author" : "Mike",
"content" : "...",
"created_on" : new Date()
});
Post.remove({},
function(err) {
console.log("cleared out Posts...");
newPost.save( function (err, record) {
console.log("record saved: " + record);
var start = new Date(2013, 7, 1);
var end = new Date(2013, 8, 1);
Post.find({ },
function( err, result_set1) {
console.log("result without date query: " + JSON.stringify(result_set1));
Post.find({created_on: { $gte: start, $lt: end }},
function( err, result_set2) {
console.log("result with date query: " + JSON.stringify(result_set2));
});
});
});
});
这是结果。当我不通过日期过滤器时,我会得到结果,但是当我输入日期过滤器时,我不会得到结果。我非常感谢帮助解决这个问题。
cleared out Posts...
record saved: { __v: 0,
title: 'A blog post',
author: 'Mike',
content: '...',
created_on: Fri Jul 26 2013 23:07:59 GMT+0000 (UTC),
_id: 51f3014f02fee3c438000001 }
result without date query: [{"title":"A blog post","author":"Mike","content":"...","created_on":"2013-07-26T23:07:59.136Z","_id":"51f3014f02fee3c438000001","__v":0}]
result with date query: []