0

I tried something like this:

query:

query = {
      pictures: {$pushAll: files}
}

files array look like this:

[{ fieldname: 'productPhotos',
        originalname: 'images.jpg',
        encoding: '7bit',
        mimetype: 'image/jpeg',
        filename: '422e64cfb147eff02fee5166d63939b10564d4ad-images.jpg',
        }]

Update mongoose model:

 Product.update({ _id: product.productId}, query,...

Model look like this for pictures:

pictures: [{
        filename: {
            type: String,
            required: false
        }
    }],

Problem is array in database not update... Any solution?

1 Answer 1

4

Wrong way around, and you should use $push and $each. i.e

Product.update(
 { "_id": product.productId }, 
 { "$push": { "pictures": { "$each": files } } },
 function(err,callback) {

 }
) 

So the update operator ( i.e $push ) is at the root of the update statement followed by the field(s) to apply to.$each is a modifier to accept a "list" if content to add as opposed to "singular"

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, how can I pull single element? Tried like this {$pull: { pictures: { filename: 'product.png' } } } but not working...
@Vladimir That's a different question. If you have to, then Ask a New Question. It's clearer that way and how this site works. A clear separation of questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.