简体   繁体   中英

how add an array to another array that exist in an object in mongodb

here is my schema

    const CustomerSchema = mongoose.Schema(
  {
    name: {
      type: String,
      trim: true,
    },
    purchased: {
      modelsId: [{
        type: mongoose.Schema.ObjectId ,
        trim: true,
      }],
      collectionsId: [{
        type: mongoose.Schema.ObjectId,
        trim: true,
      }]
    }
  },
  { timestamps: true }
);

and i want to add an array to modelsId array in purchased object with mongoose

Customer.findOneAndUpdate({ _id: customer._id }, { $addToSet: { 'purcased.modelsId': { $each: modelsId } } }, 
{ new: true }).then(res => {
                        console.log('response', res)
                    }).catch(e => {
                        console.log('error', e)
                    })

and it's not working! thank you for your help!

Looks like you may just have a simple spelling error? Try changing 'purcased.modelsId' to 'purchased.modelsId' . You're just missing an 'h' I think!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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