简体   繁体   English

用Mongoose查询MongoDB中的日期字段

[英]Querying date field in MongoDB with Mongoose

I'm trying to query documents in MongoDB using findOne() , but it's not working. 我正在尝试使用findOne()查询MongoDB文档,但是它不起作用。 The field I'm trying to filter by is 'date', and I'm not sure if perhaps it's a special word I shouldn't be using when inserting docs. 我尝试过滤的字段是“日期”,我不确定这是否是插入文档时不应该使用的特殊单词。 Is there something I'm missing? 有什么我想念的吗?

Mongo Docs: Mongo文件:

{
    "_id" : ObjectId("52c271a5d1344a7a326c0d48"),
    "author" : "User",
    "title" : "Fourth post",
    "body" : "This is the fourth post",
    "date" : "1000000"
}

Node: 节点:

var postSchema = mongoose.Schema({
    author:String,
    title:String,
    body:String,
    date:Number,
    attachments:Array
}),
    post = mongoose.model('post', postSchema);

exports.getPost = function(id, callback) {
    console.log(id);
    post.findOne({date:id}, function(err, item) {
        console.log(item);
    });
}

Console: (querying http://localhost:8080/posts/1000000 ) 控制台:(查询http://localhost:8080/posts/1000000

1000000
null

{ "date" : "1000000" } in your Mongo doc seems suspect. 您的Mongo文档中的{ "date" : "1000000" }似乎是可疑的。 Since it's a number, it should be { date : 1000000 } 由于是数字,因此应为{ date : 1000000 }

It's probably a type mismatch. 这可能是类型不匹配。 Try post.findOne({date: "1000000"}, callback) and if that works, you have a typing issue. 尝试post.findOne({date: "1000000"}, callback) ,如果post.findOne({date: "1000000"}, callback) ,则您遇到打字问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM