【问题标题】:Update Data on MongoDB更新 MongoDB 上的数据
【发布时间】:2020-08-30 02:17:48
【问题描述】:

谁能帮帮我。我试图理解为什么我无法在 mongoDb 上更新我的数据库......谁能告诉我代码出了什么问题? 在此先感谢各位。

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mongo-exercises', {
  useNewUrlParser: true,
  useUnifiedTopology: true
})
.then(() => console.log('Connected to MongoDb'))
.catch(() => console.log('Connected Fails!!!', err));

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

const Exercicio = mongoose.model('Course', exerciseSchema);

async function updaterCourse(id) {
  const exercise = await Exercicio.findByIdAndUpdate(id, { 
    $set: { 
      isPublished: true, 
      author: 'ANOTHER' 
    }
  });
  console.log(exercise);
}

updaterCourse("5a68ff090c553064a218a547");

【问题讨论】:

  • 数据库中的记录有没有更新?

标签: javascript node.js mongodb express mean-stack


【解决方案1】:

我想我已经知道了......在架构上,我应该把 _id:,只有这样我认为 mongoose 才能找到该属性的槽

【讨论】:

  • mongoose 会自动添加 _id 如果你没有在你的架构中明确设置它,所以你不应该添加它。
  • 是的,我知道,但是如果我把那个参数放在模式上,ai 可以通过 id 更新......我知道你是对的,但是如果不这样做,我就无法得到对象不要把它放在架构上......我不知道如何通过 id 来更新
【解决方案2】:

试试这个:

const ObjectID = require('mongodb').ObjectID;

async function updaterCourse(id) {
  const exercise = await Exercicio.findByIdAndUpdate({_id: new ObjectID(id)}, { 
    $set: { 
      isPublished: true, 
      author: 'ANOTHER' 
    }
  });
  console.log(exercise);
}

我认为问题在于 id 应该是 ObjectId 类型时被视为字符串。

【讨论】:

  • mongoose 会自动将其转换为 ObjectId for findByIdAndUpdate 并且它直接接受 _id 而不是对象 { _id }
  • 错误:未定义 ObjectID
【解决方案3】:

您可以点击以下链接或代码

https://www.npmjs.com/package/quick-crud

async function updaterCourse(id) {
  const exercise = await Exercicio.findByIdAndUpdate(id, { 
      isPublished: true, 
      author: 'ANOTHER' 
  })
  console.log(exercise)
}

updaterCourse("5a68ff090c553064a218a547");

【讨论】:

  • Shuvro 你能帮忙解决那个包吗?我想从 id 中获取 intire 对象……我明白了吗?让我举个例子: function updaterCourse(id) { qc.index( {model: Exercicio, where: { _id: id}} ).then((doc) => { console.log(doc); }); } updaterCourse('5a68fdf95db93f6477053ddd'); TERMINAL: 连接到 MongoDb { currentPage: 1, pageCount: 1, resourceCount: 0, data: [] }
  • ?!你能帮忙解决那个 npm 吗?
  • 请仔细阅读本文档。这个包是我和我的团队制作的
  • 是的,我 didi@Shuvro,我想念参考资料,但最后我做到了。顺便说一句,这个包裹上的好店,很清楚
  • 谢谢,请把这个包分享给你的朋友。
【解决方案4】:

我不确定您遇到了什么错误,但可能只是尝试此代码,如果没有打印任何内容或旧数据,它可能会解决您的问题:

const mongoose = require('mongoose')
 mongoose.connect('mongodb://localhost/mongo-exercises', {
      useNewUrlParser: true,
      useUnifiedTopology: true
    })
    .then(() => console.log('Connected to MongoDb'))
    .catch(() => console.log('Connected Fails!!!', err));

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

    const Exercicio = mongoose.model('Course', exerciseSchema);

    async function updaterCourse(id) {
      const exercise = await Exercicio.findByIdAndUpdate(id,  {
        $set: { 
          isPublished: true, 
          author: 'ANOTHER' 
        }
       } ,
        {
         new : true 
        }
      );
      console.log(exercise);
    }

    updaterCourse("5a68ff090c553064a218a547");

【讨论】:

  • 不工作...在控制台上显示 msg NULL 并且在数据库上没有任何变化
  • 如果它记录NULL意味着它找不到要更新的文档,我猜你输入了一个错误的id
  • 但是当我返回所有对象时,我正在寻找它的相同 ID 出现
【解决方案5】:

mongoose.connect 返回一个承诺。

也许错误是在尝试查找和更新数据库上的文档之前没有等待连接成功。

通过将findByIdAndUpdate 包装在异步函数updateCourse 上的相同方式,您可以执行相同的操作来创建连接。

类似:

const connect = async () => {
  await mongoose.connect(...);
  // declare schema here ...
  const exercise = await Exercicio.findByIdAndUpdate(...);
  console.log(exercise);
}

【讨论】:

  • 不工作 rafael... 我不知道问题出在哪里
  • 你不需要在mongoose.connect()await,一旦与数据库建立连接,猫鼬就会执行查询
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-06
  • 2023-03-22
  • 1970-01-01
  • 2021-07-30
  • 2016-12-16
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多