user3434888
user3434888

Reputation: 41

MongoDB upsert array using NodeJS

I need to insert a list of elements and check if the element exists in database, if the element exists then the element will be update.

I used upsert and array:

    dbo.collection("Ebooks").update(arrayValues, {upsert: true, multi: true},function(err, res) {
         if (err) throw err;
         console.log("finish");
         db.close();
    });  

Array eg:

[{_id: 10, name='Teste1'}, {_id: 11, name='Teste2'}]

And is returning this error:

MongoError: BSON field 'update.updates.q' is the wrong type 'array', expected type 'object'

How I convert Array to this Object?

Thanks.

Upvotes: 2

Views: 3109

Answers (1)

Helen
Helen

Reputation: 211

Not sure, but looks like you don't pass query(first parameter), what data should de updated.

dbo.collection("Ebooks").update((), arrayValues, {upsert: true, multi: true},function(err, res) {})

or

dbo.collection("Ebooks").update((_id:someValue), arrayValues, {upsert: true, multi: true},function(err, res) {})

Upvotes: 1

Related Questions