繁体   English   中英

在猫鼬中找到ID的所有文件?

[英]Find all documents by Id in mongoose?

我有以下文件:

{
    "_id": {
        "$oid": "5bf662551a0ffc2f40c91945"
    },
    "categories": [
        "angular"
    ],
    "title": "1",
    "details": "1",
    "url": "undefined",
    "category": {
        "$oid": "5bf135657d6f2c69a527186d"
    },
    "created_at": {
        "$date": "2018-11-22T08:01:25.418Z"
    },
    "__v": 0
}

我想根据类别ID查找所有文档。 我尝试了这个:

articleModel
    .find({category: req.params.id})
    .sort({ $natural: -1 })
    .exec(function(err, blogs) {
      if (err) {
        throw err;
      }
      console.log("get blogs by category ", blogs);
      res.json({
        success: true,
        message: "blogs by category",
        blogs: blogs
      });
    });

但它返回空数组。

使用new mongoose.Types.ObjectId(YOUR_ID)鼬对象ID查找时,请使用new mongoose.Types.ObjectId(YOUR_ID)

articleModel
    .find({"category":  new mongoose.Types.ObjectId(req.params.id)})
    .exec(function(err, blogs) {
      if (err) {
        throw err;
      }
      console.log("get blogs by category ", blogs);
      res.json({
        success: true,
        message: "blogs by category",
        blogs: blogs
      });
    });

暂无
暂无

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

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