Mongoose.Model.findById()返回null

编程入门 行业动态 更新时间:2024-10-09 14:27:24

Mongoose.<a href=https://www.elefans.com/category/jswz/34/1771352.html style=Model.findById()返回null"/>

Mongoose.Model.findById()返回null

const mongoose =  require('mongoose');
mongoose.connect('mongodb://localhost/mongo-exercises')
        .then(()=>{
            console.log('DataBase Connected..!!');
        })
        .catch(err=> console.log('Connection err: ',err));
const schema = new mongoose.Schema({
    tags:[ String ],
    date:Date,
    name: String,
    author: String,
    isPublished: Boolean,
    price: Number
});
const Course = mongoose.model('course',schema);
async function updateCourse(id){
    const course = await Course.findById(id);
    if(!course) return;
    course.name = "mongo db";
    console.log(course)
    const result = await course.save();
}

updateCourse("5a68fdc3615eda645bc6bdec");      

我的updateCourse(id)返回null。输出::

C:\Users\Divay Mohan\Desktop\100DaysOfCode\Day9\PracticeCRUD>node index.js
DataBase Connected..!!
  mquery findOne courses { _id: 5a68fdc3615eda645bc6bdec } { fields: {} } +0ms

并且使用相同的程序::

const mongoose =  require('mongoose');

mongoose.connect('mongodb://localhost/mongo-exercises')
        .then(()=>console.log('DataBase Connected..!!'))
        .catch(err=> console.log('Connection err: ',err));

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

const Course = mongoose.model('course',schema);
async function getCourses(){
    const result =  await Course.find();
    console.log(result);
}
getCourses();

输出:

C:\Users\Divay Mohan\Desktop\100DaysOfCode\Day9\PracticeCRUD>node exercise3.js
DataBase Connected..!!
[ { tags: [ 'react', 'frontend' ],
    _id: 5a68fdf95db93f6477053ddd,
    date: 2018-01-24T21:43:21.589Z,
    name: 'React Course',
    author: 'Parbhu',
    isPublished: false,
    __v: 0 },
  { tags: [ 'express', 'backend' ],
    _id: 5a68fdc3615eda645bc6bdec,
    date: 2018-01-24T21:42:27.388Z,
    name: 'Express.js Course',
    author: 'DM',
    isPublished: true,
    price: 10,
    __v: 0 },
  { tags: [ 'aspnet', 'backend' ],
    _id: 5a68fde3f09ad7646ddec17e,
    date: 2018-01-24T21:42:59.605Z,
    name: 'ASP.NET MVC Course',
    author: 'Divay',
    isPublished: true,
    price: 15,
    __v: 0 },
  { tags: [ 'node', 'backend' ],
    _id: 5a68ff090c553064a218a547,
    date: 2018-01-24T21:47:53.128Z,
    name: 'Node.js Course by Mohan',
    author: 'Mohan',
    isPublished: false,
    price: 12,
    __v: 0 },
  { tags: [ 'angular', 'frontend' ],
    _id: 5a6900fff467be65019a9001,
    date: 2018-01-24T21:56:15.353Z,
    name: 'Angular Course',
    author: 'kate',
    isPublished: true,
    price: 15,
    __v: 0 },
  { tags: [ 'node', 'backend' ],
    _id: 5a68fdd7bee8ea64649c2777,
    date: 2018-01-24T21:42:47.912Z,
    name: 'Node.js Course',
    author: 'Tom',
    isPublished: true,
    price: 20,
    __v: 0 },
  { tags: [ 'node', 'backend' ],
    _id: 5a68fe2142ae6a6482c4c9cb,
    date: 2018-01-24T21:44:01.075Z,
    name: 'Node.js Course by More',
    author: 'More',
    isPublished: true,
    price: 12,
    __v: 0 } ]

比较程序和输出。第一个与Model.find()一起工作正常,但Model.findById()无法工作。这是我现在正在使用的配置:

{
  "name": "PracticeCRUD",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongoose": "^5.0.1"
  }
}

  1. npm版本为5.5.1
  2. 节点版本为8.9.4
  3. MongoDB版本为MongoDB 4.2.6社区

请帮助我。谢谢..

回答如下:

由于尚未在架构中指定_id的类型,因此默认情况下将键入ObjectId。因此,为了使findById工作,数据库中_id的值也应为ObjectId类型。在您的情况下,如果您在mongo shell中列出文档,则_id应该类似于"_id" : ObjectId("5a68fdc3615eda645bc6bdec")

更多推荐

Mongoose.Model.findById()返回null

本文发布于:2024-05-07 20:24:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757098.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Model   Mongoose   null   findById

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!