【问题标题】:Mongoose save modified users arrayMongoose 保存修改后的用户数组
【发布时间】:2020-01-20 11:52:00
【问题描述】:

我在这里修改了这个Users 数组,现在我想保存它。但它不会让我保存,因为我收到一个错误:save is not a function。我该如何解决这个问题?

代码:

let users = await User.find({served: false});

users.splice(userIndex, 1);

userIndex++;

users.splice(userIndex, 0, user);
users.save() // save is not a function

更新

User.insertMany(users).then(docs => {
    console.log(docs)
})

这将保存它,但我得到文件的重复错误。也许我可以清除所有未服务的用户并添加这些新用户?

【问题讨论】:

  • 将 users.save() 更改为 User.save(users)
  • 还是同样的错误

标签: mongodb mongoose


【解决方案1】:

这行得通。我删除了元素,然后再次修改添加。

User.deleteMany({served: false}).then(() => {
    User.insertMany(users).then(users => {
        console.log(users)
    })
});

【讨论】:

    猜你喜欢
    • 2014-03-27
    • 1970-01-01
    • 2014-06-19
    • 2022-12-19
    • 2015-07-14
    • 1970-01-01
    • 2021-01-03
    • 2014-08-21
    • 2018-03-10
    相关资源
    最近更新 更多