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?
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.
What would be the correct command to find a document in a MongoDB database, update it and then insert the updated document as a new document with a new _id?
The original document should remain completely unchanged.
Are you using mongoose, MongoDB shell or Compass?
If you want to find a document and update it, why do you need a new id?
If you want to update an existing document, the id will remain the same, or you can a find and copy an existing document to a new one and update the new doc.
I am using python and mongoengine for this project.
And i am creating a restAPI. Since save() query in mongo inserts new or updates existing document.
SearchRating(stars=4, comment='').save() // returns "None" on success
This particular piece of code in mongoengine only creates new document. However the idea is that only once user is suppose to create this document, later they should only update it. But for updation i need to know what _id i just added in mongodb in search_rating collection.
So how shall i know the _id of document that i just added?
I have a collections called 'users' and I want to add new field called 'status: boolean' to already exists every documents using cloud functions and firestore database instantly.
There is no bulk update operation, so you will have to:
Query the collection for all documents
Iterate each one
Update the document with the new field
In MongoDB, you can use db.collection.save({_id:'abc'}, objectToSave) to perform an upsert.
Let's define objectToSave as below
{_id:'abc', field1:1, field2:2};
In my collection, I have already have a document with same _id value as below:
{_id:'abc', field3:3};
The save function above will replace the existing document in collection to
{_id:'abc', field1:1, field2:2};
What I want is to perform a $set operation to produce some document in collection as below
{_id:'abc', field1:1, field2:2, field3:3};
Can this be achieved in the save function or I have to write separate update statements?
Note that objectToSave's fields are dynamic. The language I'm using is Node.JS.
db.collection.update({'_id':'abc'},{$set:{field1:1,field2:2}},{upsert:true})
should do what you want:
It upserts, so if the document does not exist yet, it is created as {_id:'abc',field1:1,field2:2} and efficiently so, since an index is used which must exist
If the document already exists, the fields field1 and field2 are set to the value in the update statement.
If either of the fields exist in the document, it will be overwritten.
Since you didn't state what language you use: in plain mongoDB, there is no save function. The explicit requirement to merge new and persisted versions of entities is quite an unusual one, so yes, I'd assume that you have to write a custom function.