Mongoose Model.update 在我的代码中不起作用

Mongoose Model.update not working in my code

我想用给定的 ID 更新一些课程属性。 这是我的代码:

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

const Course = mongoose.model('Course', courseSchema);
mongoose.connect('mongodb://localhost/mongo-exercises');

async function updateCourse(id) {
     const result = await Course.update({_id: id}, {
        $set: {
            author: 'New', 
            isPublished: false
        }
    });

console.log(result);
}

updateCourse('5a68fde3f09ad7646ddec17e');

在控制台上我得到 {ok: 1, nModified: 0, n: 0}

我试过用这种其他方式更新元素,但没有用。对于这段代码,我没有得到结果,没有响应:

const course = await Course.findById(id);
  if(!course) return; 

  course.isPublished = true; 
  course.author = 'Another Author';

  const result = await course.save();
  console.log(result);

当我尝试使用 findByIdAndUpdate 和其他代码进行更新时,控制台中的结果为空:

 async function updateCourse(id) {
    const course = await Course.findByIdAndUpdate(id, {
    $set: {
        author: 'Billy',
        isPublished: false
    }
 });

   console.log(course);

 }
updateCourse('5a68fde3f09ad7646ddec17e');

我正在使用的数据库有:

 { _id: 5a68fdd7bee8ea64649c2777,
   name: 'Node.js Course',
   author: 'Sam',
   isPublished: true,
   price: 20 },
 { _id: 5a68fde3f09ad7646ddec17e,
   name: 'ASP.NET MVC Course',
   author: 'Mosh',
   isPublished: true,
   price: 15 },
 { _id: 5a6900fff467be65019a9001,
   name: 'Angular Course',
   author: 'Mosh',
   isPublished: true,
   price: 15 },
 { _id: 5a68fe2142ae6a6482c4c9cb,
   name: 'Node.js Course by Jack',
   author: 'Jack',
   isPublished: true,
   price: 12 }

课程没有更新。错误在哪里? 请注意,如果我想创建新文档,它不会有任何问题。

根据 MongoDB update doc.

,我在代码中没有发现任何问题,这里的查询和语法是正确的

The update() method returns a WriteResult object that contains the status of the operation. Upon success, the WriteResult object contains the number of documents that matched the query condition, the number of documents inserted by the update, and the number of documents modified.

此处未修改任何文档,唯一的原因是,您在查询中设置要更新的相同数据。

尝试使用

isPublished: {
    type: Boolean,
    default: false
}

而不是

isPublished: Boolean

return {ok: 1, nModified: 0, n: 0} 表示操作成功但找不到,也没有更新任何条目,所以这意味着它甚至不匹配。

我不完全确定,但我认为 mongoose 只能通过 ObjectId 找到 ID,而不是通过 string

所以你应该通过转换得到想要的结果,就像这样:

const result = await Course.update({ _id: mongoose.Types.ObjectId(id) }, { 
    $set: { author: 'New', isPublished: false }
});

update({_id: id},...) 指定 _id 期望文档中的 _id 类型为 ObjectID.

update()findById(),没有找到_id和returnnull,因为_id类型是,大概, String in your collection/document, - 检查 Compass。

在您的架构中添加 _id:String

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

并用这个替换你的承诺:

async function updateCourse(id) {
  const result = await Course.update({ _id: mongoose.Types.ObjectId(id) }, { 
    $set: { author: 'New Author', isPublished: false }
  });

我在 mosh node.js 课程中遇到了同样的问题,这个解决方案对我有用! :)