简体   繁体   English

更新 mongoose 中的嵌入文档

[英]Updating embedded documents in mongoose

I have a user and a premium schema.我有一个用户和一个高级模式。 The premium one is embedded in user.高级版嵌入在用户中。

 const premiumSchema = new mongoose.Schema( { subscriptionID: { type: String, required: true, }, guild: { type: Object, default: {}, }, payment_id: { type: String, required: true, }, expiry: { type: Date, require: true, }, reminded: { type: Boolean, default: false, }, }, { collection: "premium" } );

 const userSchema = new mongoose.Schema({ discordId: { type: String, required: true, }, accessToken: { type: String, required: true, }, premium: { type: [premiumSchema], required: false, }, });

I am trying to update a value of my premium collection by:我正在尝试通过以下方式更新我的高级收藏的价值:

 const user = await User.findOne({ "premium._id": new ObjectId(req.body.premiumId), }); const premium = user.premium.id(new ObjectId(req.body.premiumId)); premium.reminded = true; await user.save();

The Problem is that it updates the reminded attribute in my user.premium array but doesn't update it in the premium collection itself.问题是它更新了我的user.premium数组中的reminded属性,但没有在高级集合本身中更新它。 Is there anything I am doing wrong?有什么我做错了吗?

You can use findOneAndUpdate method and positional operator to directly update your user.您可以使用 findOneAndUpdate 方法和位置运算符直接更新您的用户。

Positional operator $ allows you to iterate through the array and update only the corresponding subdocument位置运算符 $ 允许您遍历数组并仅更新相应的子文档

const savedUser = await User.findOneAndUpdate(
  {_id: new ObjectId(req.body.premiumId)},
  {$set: {'premium.$.reminded': true}}
);

See doc: https://docs.mongodb.com/manual/reference/operator/update/positional/请参阅文档: https://docs.mongodb.com/manual/reference/operator/update/positional/

This way you can update the subdocument within the User collection, but the value in the Premium collection will not be updated .这样您就可以更新 User 集合中的子文档,但不会更新 Premium 集合中的值 This is because when you define the premium property within the Users collection you are telling mongoose (and mongodb) to save the object within the user collection itself.这是因为当您在 Users 集合中定义高级属性时,您是在告诉 mongoose(和 mongodb)将 object 保存在用户集合本身中。 In this case there are no references between the two collections: the data will be saved only in the user collection, intended as subdocuments.在这种情况下,两个 collections 之间没有引用:数据将仅保存在用户集合中,作为子文档。 You are just defining schema for child won't create a separate collection for the children in the database instead you will embed the whole child document in the parent (user).您只是为子定义架构,不会为数据库中的子创建单独的集合,而是将整个子文档嵌入父(用户)中。

Example data:示例数据:

user1: {
  discordId: { 'abc' },
  accessToken: { 'token' },
  premium: {
    subscriptionID: { 'idSubscription' } 
    guild: { },
    payment_id: { 'idPayment' },
    expiry: { '2021-07-31T20:41:19.618+00:00' },
    reminded: { true }
  },
});

The two User and Premium collections remain completely separate.两个 User 和 Premium collections 完全分开。 If you want to upgrade the Premium collection as well, you can do it using two different calls, one for User and one for Premium.如果您还想升级高级系列,您可以使用两个不同的调用来完成,一个用于用户,一个用于高级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM