【问题标题】:I want to implement a simple update in my db but I am getting this { ok: 0, n: 0, nModified: 0 } in my console?我想在我的数据库中实现一个简单的更新,但我在控制台中得到了这个 { ok: 0, n: 0, nModified: 0 }?
【发布时间】:2021-07-10 16:49:14
【问题描述】:

# I can't update keep getting { ok: 0, n: 0, nModified: 0 } ! # ... const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/mongo-exercises', {useNewUrlParser: true, useUnifiedTopology: true }) ... .then(() => console.log('连接到 MongoDB...')) .catch(() => console.error('Could not connect to MongoDB...', err));

...

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


 const Course = mongoose.model('Course', courseSchema);

 async function updateCourse(id){
    const result = await Course.updateOne({_id: id}, {
        $set: {
            author: 'Momo',
            isPublished: true
        }
    });
...

    console.log(result);    //{ n: 0, nModified: 0, ok: 1 }

}

...

 updateCourse('5a68ff090c553064a218a547');

块引用

【问题讨论】:

  • 如果您使用 Postmon 发送请求,请确保在 Body 中选择 JSON。默认的Text 不起作用,会报这个错误。

标签: node.js mongodb mongoose


【解决方案1】:

你可以试试这个。

const result = await Course.updateOne({_id: id}, {
        $set: {
            author: 'Momo',
            isPublished: true
        },
      {new:true}
    }).lean();

【讨论】:

    【解决方案2】:

    我明白了。我确实从 json 文件将数据导入到我的数据库中,并且 id 没有 ObjectId,并且不适用于较新版本的 mongoose。

    所以 id 是这样的:"_id":"5a68fdc3615eda645bc6bdec"

    这是 mongoose 5.9.18 的正确方式:"_id": ObjectId("5a68fdc3615eda645bc6bdec")

    【讨论】:

      【解决方案3】:
      const mongoose = require('mongoose');
      async function connectDatabase() {
          try {
              await mongoose.connect('mongodb://localhost/newdb21', {
                  useNewUrlParser: true,
                  useUnifiedTopology: true,
                  useFindAndModify: false,
                  useCreateIndex: true
                }).then(console.log('successfully connected to MongoDb'));
          } catch(err) {
              console.log('Could not connect to the database');
          }
      }
      connectDatabase();
          
      // define a schema to store data
      const courseSchema = mongoose.Schema({
          name: { type: String, required: true },
          price: Number,
          isPublised:{ type:Boolean, default:false},
          tags: [String],
          date: { type: Date, default: Date.now },
          author: String
      });
      
      // create a mode for the course
      const Course = mongoose.model('Course',courseSchema);
      async function updateCourses(id) {
          try {
              const courses = await Course .findById(id);
              if (!courses) return;
              courses.set({name:'nedu BOss'})
              courses.save();
              console.log(courses);
          } catch(err) {
              console.log('Couldnt update any item in the mongo database');
          }
      }
          
      //updateCourses('45563664345677');
      // if the data was imported from another database you will experience the earlier issue
      

      【讨论】:

      • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
      猜你喜欢
      • 2019-03-13
      • 2019-07-15
      • 2019-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多