Mojax Razmi
Mojax Razmi

Reputation: 23

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!

Upvotes: 0

Views: 43

Answers (1)

Eric Russell
Eric Russell

Reputation: 120

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!

Upvotes: 1

Related Questions