how to update a field in a document if the field already exists else update the value of the field? $set updates fine on documents where the field already exists. just found lot of hints how to insert a new field into an entire collection but how to solve this on document basis?
Regards,
Chris
$set will add the specified field or fields if they do not exist in this document or replace the existing value of the specified field(s) if they already exist.
Use the upsert flag in your update statement.
Related
when I am inserting a document to MongoDB collection I get an error: E11000 duplicate key error collection
I would like to override the existing document with a new one.
Is there any way to force insert new data with the same _id field?
I am using go
You cannot insert two separate documents in MongoDB with the same _id field. This is stated in the documentation on document structure:
The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array.
What you're describing sounds more like an upsert, an operation that updates a document if it exists, and if it does not, inserts it instead. You could accomplish this by using updateOne and setting the upsert flag to true.
This Meteor server code needs to remove all fields except "fName" from a document found by a field and if the document does not exist then create it.
Is there a way to do that at one go? thx
myCol.update({fName: someName}, {fName: someName}); // works if doc exists, fails if no doc.
myCol.upsert({fName: someName}, {fName: someName}); // failed if doc exists, works if it exists
You can use fName :{$exists:true} in your query part.
This will update document only if fName in present.
I want to update a document(push a json object) matched by an "id", but if that document doesn't exists, create one(with the same structure) followed by the same update(push) to that document.
You can use the update command with the upsert option:
db.yourCollection.update({id:xxx}, {id:xxx, field1:yyy, field2:zzz}, {upsert:true})
The first parameter is the search query and should be done on a uniquely indexed field. The second is the actual document to insert/update and the third tells it to do an upsert.
db.yourCollection.update({id:xxx}, {id:xxx, field1:yyy, field2:zzz},
{upsert:false})
upsert is a Optional. If set to true, creates a new document when no document matches the query criteria. If set to false, which does not insert a new document when no match is found.
How to insert new document in a MongoDB collection if there's no other document with specified unique field exists, or update the existing one otherwise?
Is there anything more reasonable than just using findOne and save methods with some conditions?
I want to do a batch insert in mongodb , but if the record exists already it should replace it with the new one.There is update command but its not possible to do it in batch.Any idea whether it is possible? I am using java api.
Thanks
Edit:
As my collection size is not very huge, i am renaming the collection with drop Target option set to true and creating a new collection with this data.As i cant risk deleting and creating a new collection this is better, but it will be awesome if there is replace into equivalent.
If you are having any primary key in your collection, then it will replace automatically.Make sure your documents have _id key.
Look at mongodb document:
Shorthand for insert/update is save - if _id value set, the record is updated if it exists or inserted if it does not; if the _id value is not set, then the record is inserted as a new one.
in http://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html