Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose Model.update在我的代码中不起作用_Node.js_Database_Mongodb_Mongoose_Backend - Fatal编程技术网

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

Node.js Mongoose Model.update在我的代码中不起作用,node.js,database,mongodb,mongoose,backend,Node.js,Database,Mongodb,Mongoose,Backend,我想用给定的id更新一些课程属性。 这是我的代码: const mongoose = require('mongoose'); const courseSchema = new mongoose.Schema({ name: String, author: String, tags: [String], date: Date, isPublished: Boolean, price: Number }); const Course = mongo

我想用给定的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 }
课程没有更新。错误在哪里?
请注意,如果我想创建新文档,它会毫无问题地创建

我看不到代码中有任何问题,这里的查询和语法是正确的

update()方法返回一个WriteResult对象,该对象包含 操作状态。成功后,WriteResult对象包含 与查询条件匹配的文档数 更新插入的文档数,以及文档数 修改

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

尝试使用

isPublished: {
    type: Boolean,
    default: false
}
而不是

isPublished: Boolean

返回的
{ok:1,nModified:0,n:0}
表示操作已成功,但无法找到或更新任何条目,因此这意味着它甚至不匹配

我不完全确定,但我认为猫鼬只能通过
ObjectId
而不是
string
来查找ID

因此,您应该通过转换来获得所需的结果,如下所示:

const result = await Course.update({ _id: mongoose.Types.ObjectId(id) }, { 
    $set: { author: 'New', isPublished: false }
});
update({\u id:id},…)
指定
\u id
期望文档中的
\u id
类型为
ObjectID


update()
findById()
,不查找
\u id
,并返回
null
,因为
\u id
类型可能是集合/文档中的字符串,-请使用指南针检查

在模式中添加
\u 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课程中遇到了同样的问题,这个解决方案对我很有效:)

author
isPublished
是否已经与您试图设置的值相同?您好。isPublished具有相同的值,但作者不同。我尝试了其他更新选项,例如通过id查找元素,然后对其进行更新,但它不起作用。您尝试使用相同的硬编码值进行更新,因此mongodb发现新旧记录相同,因此
nModified
字段设置为零。我添加了要更新的元素,它有不同的价值。不起作用,结果是一样的。我想这可能是猫鼬或猫鼬的问题。