【问题标题】:Querying date field in MongoDB with Mongoose使用 Mongoose 查询 MongoDB 中的日期字段
【发布时间】:2014-01-01 06:50:39
【问题描述】:

我正在尝试使用findOne() 查询MongoDB 中的文档,但它不起作用。我试图过滤的字段是“日期”,我不确定它是否是我在插入文档时不应该使用的特殊词。我有什么遗漏吗?

Mongo 文档:

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

节点:

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);
    });
}

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

1000000
null

【问题讨论】:

  • console.log(typeof id) 你会发现问题的。
  • 你是如何传递你的id的?或者怎么调用函数?
  • err中是否有错误?
  • 尝试使用 parseInt(id)
  • @BradM:就是这样。我投入了{date:parseInt(id)},它就像一个魅力。谢谢!

标签: node.js mongoose


【解决方案1】:

{ "date" : "1000000" } 在您的 Mongo 文档中似乎很可疑。既然是数字,那就应该是{ date : 1000000 }

这可能是类型不匹配。尝试post.findOne({date: "1000000"}, callback),如果可行,则说明您有打字问题。

【讨论】:

    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 2021-10-08
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    相关资源
    最近更新 更多