猫鼬findbyid / find({id:“ ...”})返回Null

时间:2019-02-06 02:55:00

标签: mongodb mongoose

我已经多次看到这个问题,但是我没有找到可行的解决方案。我正在尝试使用Mongoose .findById查询MongoDB,但没有得到预期的结果。 find({id:“ ...”})也未返回任何内容。

使用不带任何参数的查找将显示所有预期结果,包括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()响应。

1 个答案:

答案 0 :(得分:1)

关于从JSON文件导入数据的讨论中,因此将_id作为字符串插入,通过_id查找文档时,moognoose自动将字符串转换为Object。

// 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,请按如下所示用$oid更改_id字段,

{
    "_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
}