【问题标题】:Mongoose findbyid / find({id: "..."}) returning NullMongoose findbyid / find({id: "..."}) 返回 Null
【发布时间】:2019-02-06 12:35:20
【问题描述】:

我已经多次看到这个问题,但我还没有找到有效的解决方案。我正在尝试使用 Mongoose .findById 查询 MongoDB,但没有得到预期的结果。 find({id: "..."}) 也没有返回任何东西。

使用不带任何参数的 find 会显示所有预期结果,包括 id 键值对,但我不能在查询中使用 id 值。

使用猫鼬:5.4.9

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mongo-exercises', { useNewUrlParser: true });

const courseSchema = new mongoose.Schema({
  name: String,
  author: String,
  tags: [String],
  date: { type: Date, default: Date.now },
  isPublished: Boolean,
  price: Number
});

const Course = mongoose.model('Course', courseSchema);

async function getCourses() {
  return await Course
    .find()
}

async function run() {
  const result = await getCourses();
  console.log(result);
}
run();
//Return
    [ { tags: [ 'react', 'frontend' ],
    _id: 5a68fdf95db93f6477053ddd,
    date: 2018-01-24T21:43:21.589Z,
    name: 'React Course',
    author: 'Mosh',
    isPublished: false,
    __v: 0 },
  { tags: [ 'aspnet', 'backend' ],
    _id: 5a68fde3f09ad7646ddec17e,
    date: 2018-01-24T21:42:59.605Z,
    name: 'ASP.NET MVC Course',
    author: 'Mosh',
    isPublished: true,
    price: 15,
    __v: 0 },
  { tags: [ 'node', 'backend' ],
    _id: 5a68fe2142ae6a6482c4c9cb,
    date: 2018-01-24T21:44:01.075Z,
    name: 'Node.js Course by Jack',
    author: 'Jack',
    isPublished: true,
    price: 12,
    __v: 0 },
  { tags: [ 'node', 'backend' ],
    _id: 5a68fdd7bee8ea64649c2777,
    date: 2018-01-24T21:42:47.912Z,
    name: 'Node.js Course',
    author: 'Mosh',
    isPublished: true,
    price: 20,
    __v: 0 },
  { tags: [ 'node', 'backend' ],
    _id: 5a68ff090c553064a218a547,
    date: 2018-01-24T21:47:53.128Z,
    name: 'Node.js Course by Mary',
    author: 'Mary',
    isPublished: false,
    price: 12,
    __v: 0 },
  { tags: [ 'angular', 'frontend' ],
    _id: 5a6900fff467be65019a9001,
    date: 2018-01-24T21:56:15.353Z,
    name: 'Angular Course',
    author: 'Mosh',
    isPublished: true,
    price: 15,
    __v: 0 },
  { tags: [ 'express', 'backend' ],
    _id: 5a68fdc3615eda645bc6bdec,
    date: 2018-01-24T21:42:27.388Z,
    name: 'Express.js Course',
    author: 'Mosh',
    isPublished: true,
    price: 10,
    __v: 0 } ]

该代码验证我已连接到正确的数据库并检索真实 ID。当我如下所示修改 getCourses 函数时,我得到 null 或空数组,具体取决于我使用的是 findById 还是 find({id: "..."})。

async function getCourses() {
  return await Course
    .findById('5a68fdf95db93f6477053ddd')
}
//null

async function getCourses() {
  return await Course
    .find({ id: '5a68fdf95db93f6477053ddd' })
}
// []

async function getCourses() {
  return await Course
    .find({ _id: '5a68fdf95db93f6477053ddd' })
}
// []

任何帮助将不胜感激。谢谢!

编辑:显示完整的 find() 响应。

【问题讨论】:

  • 您能否显示 .find({}) 的完整响应?看看它是如何存储在数据库中的可能会对我有所帮助
  • 完成。谢谢。
  • MongoDB 中的集合名称?
  • @ŞivāSankĂr 课程
  • 试试mongoose.model('Course', courseSchema, 'courses');

标签: mongodb mongoose


【解决方案1】:

根据我们的讨论,当您从 JSON 文件中导入数据时,将_id 作为字符串插入,通过_id 查找文档时,moognoose 会自动将字符串转换为对象。

// Original JSON
{
    "_id": "5a68fde3f09ad7646ddec17e",
    "tags": [
        "aspnet",
        "backend"
    ],
    "date": "2018-01-24T21:42:59.605Z",
    "name": "ASP.NET MVC Course",
    "author": "Mosh",
    "isPublished": true,
    "price": 15,
    "__v": 0
}

解决方案 1:

要在 JSON 中插入 _id,请将 _id 字段更改为 $oid,如下所示,

{
    "_id": { "$oid": "5a68fde3f09ad7646ddec17e" },
    "tags": [
        "aspnet",
        "backend"
    ],
    "date": "2018-01-24T21:42:59.605Z",
    "name": "ASP.NET MVC Course",
    "author": "Mosh",
    "isPublished": true,
    "price": 15,
    "__v": 0
}

解决方案 2:

从您的 JSON 中删除 _id,MongoDB 将自动生成一个 _id

{
    "tags": [
        "aspnet",
        "backend"
    ],
    "date": "2018-01-24T21:42:59.605Z",
    "name": "ASP.NET MVC Course",
    "author": "Mosh",
    "isPublished": true,
    "price": 15,
    "__v": 0
}

【讨论】:

    猜你喜欢
    • 2016-06-24
    • 2020-05-13
    • 2018-05-29
    • 2020-09-09
    • 2016-04-19
    • 2020-11-19
    • 2020-03-28
    • 2019-07-15
    • 1970-01-01
    相关资源
    最近更新 更多