How to find a document in MongoDB, update it, and insert a new document? - mongodb

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.

Related

How to create new document if doesn't exist and delete if does existed by _id in MongoDB

I'm working with Mongoose/Mongodb and I'm facing a situation. Specifically, I want to create a new document if this document doesn't existing which condition by _id and else will delete if it does.
I needs to the help from everyone!

How to retrieve the document that's just been added in MongoDB

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?

Can I get inserted record immediate after collection.InsertOneAsync(document) in MongoDB

We are using MongoDB framework in C# for DB calls.
When we insert new document in collection, we want updated result with inserted _id into database.
After you insert document using official C# driver, you already have it with updated _id column. Inserted document is updated by default.

How to insert or update the existing document in MongoDB collection

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?

Replace into (mysql) equivalent in mongodb

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