1
votes

In mongodb update, upsert is just a flag, meaning that the document is created if it does not exist and updated if it exists.

In elasticsearch, one can separately specify a doc json, with fields that are updated, and an upsert json, with fields that are added only to new documents. So, the fields specified in the upsert don't affect existing documents.

Example:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc" : {
    "name" : "new_name"
},
"upsert" : {
  "is_new" : true
 }
}

So, if document 1 exists its name gets changed to "new_name", but "is_new" field is not added. If it does not exist, it will have "is_new" = true.

Is anything like that feasable in mongodb?

Thanks

1

1 Answers

1
votes

Yes. The $setOnInsert modifier:

db.collection.update({ "_id": 1 },{ "$setOnInsert": { "field": 2 } },{ "upsert" true })

That means the content within $setOnInsert only gets applied when a new document exists. Other update modifiers apply "both" on insert and on a document match.