Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose update only updates first document

Okay.. this is odd and annoying, so any help would be GREATLY appreciated. Here's my code:

        Target.update {location_city: "New York"}, {location_country: "FUDGE!"}, {safe: true}, (err, res) ->
          console.log "Updating with New York"
          console.log res
          console.log "Err #{err}"

No error, NADA. BUT only the FIRST document gets updated for some reason. When I run a find on the SAME query, I get multiple results.

Any help would be AWESOMELY appreciated.

like image 223
Shamoon Avatar asked Oct 13 '11 15:10

Shamoon


People also ask

What is the difference between findOneAndUpdate and updateOne?

findOneAndUpdate returns a document whereas updateOne does not (it just returns the _id if it has created a new document).

Does Mongoose save overwrite?

Mongoose save with an existing document will not override the same object reference. Bookmark this question. Show activity on this post.

What is Upsert true in Mongoose?

In MongoDB, an upsert is an update query that inserts a new document if no document matches the given filter. Note that Mongoose will only inert at most one document even if you use updateMany() with upsert .

What does $Set do in Mongoose?

The $set operator replaces the value of a field with the specified value. The $set operator expression has the following form: { $set: { <field1>: <value1>, ... } } To specify a <field> in an embedded document or in an array, use dot notation.


1 Answers

multi has to be true.

So the correct query would be

    Target.update {location_city: "New York"}, {location_country: "FUDGE!"}, {multi: true}, (err, res) ->
      console.log "Updating with New York"
      console.log res
      console.log "Err #{err}"
like image 76
Shamoon Avatar answered Oct 19 '22 04:10

Shamoon