How to retrieve the document that's just been added in MongoDB - 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?

Related

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

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.

Can we use callbacks in the mongo shell?

I want to insert one document into collection1 and after successfully inserting the document, I want to insert another document into collection2. One of the fields for the document in collection2 will be the _id of the document just inserted into collection1.
I am using a callback:
db.collection1.insert(<document>,function(err,doc)){
db.collection2.insert({collection1_id: doc[0]._id, <field>:<value>})
However, it seems that callback is not available without Node.js.
Is there any workaround?
Callbacks are part of the Node.js async API and are not supported in the mongo shell (as at MongoDB 4.0). However, you can always write the equivalent without callbacks.
The mongo shell's insertOne() method will return an insertedId field with the _id value of the inserted document, so you can either save or reference this value.
For example:
db.collection2.insertOne({
collection1_id: db.collection1.insertOne({}).insertedId,
field: 'value'
})

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?

Which is the best way to insert data in mongodb

While writing data to mongodb, we are checking if the data is present get the _id and using save update it else using insert add the data. Read save is the best way if you are providing _id in the query while saving it will update/insert based on if the _id is present in the db. Is the save the best method or is there any other way.
If you have all data available to save, just run update() each time but use the upsert functionality. Only one query required:
db.collection.update(
['_id' => $id],
$data,
['upsert' => true]
);
If your _id is generated by mongo you always know there is a record in the database and update is the one to use, but then again you could also save().
If you generated your id's (and thus don't know if it comes from the collection), this will always work without having to run an extra query.
From the documentation
db.collection.save()
Updates an existing document or inserts a new document, depending on its document parameter.
db.collection.insert()
Inserts a document or documents into a collection.
If you use db.collection.insert() in your case you will get duplication key error since it will try to insert new document which has same _id with an existing document. But instead of using save you should use the update method.