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"
}
}
和
- npm版本为5.5.1
- 节点版本为8.9.4
- MongoDB版本为MongoDB 4.2.6社区
请帮助我。谢谢..
回答如下:由于尚未在架构中指定_id
的类型,因此默认情况下将键入ObjectId
。因此,为了使findById
工作,数据库中_id的值也应为ObjectId
类型。在您的情况下,如果您在mongo shell中列出文档,则_id
应该类似于"_id" : ObjectId("5a68fdc3615eda645bc6bdec")
更多推荐
Mongoose.Model.findById()返回null
发布评论