What is difference between deleteOne() and findOneAndDelete() operation in MongoDB? - mongodb

This both operations delete one object. Is there any performance difference between them or both are same?

findOneAndDelete has the sort parameter which can be used to influence which document is updated. It also has a TimeLimit parameter which can control within which operation has to complete.
From the docs :
https://docs.mongodb.com/v3.2/reference/method/db.collection.findOneAndDelete/
Deletes a single document based on the filter and sort criteria,
returning the deleted document.
https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/
Removes a single document from a collection.

Related

which branches of a mongodb $or query were satisfied to include a document in the set returned?

Say I have a mongo $or query, something like { $or: [query1, query2, ... queryN] }, where each embedded query could be complex. Upon executing the query, a set of documents matching one or more of the embedded queries is returned. I would like to know which of the N embedded queries was satisfied for each document in the returned set, perhaps by adding a new field that I specify, eg. marks, into each returned document that would hold a list of the indexes of whichever of the queries was satisfied. I need this information to indicate how each document was identified in my application's interface.
I realize I could inspect the set once it is returned and determine the queries that were satisfied, but these queries could be arbitrarily complex and expensive to inspect - besides, this must have already been done inside mongo itself while doing the search.
I also realize I could run each of the N queries sequentially and then mark and merge the results into a growing set, but I want to save that overhead by running a single query instead of N queries.
And I realize that Mongo will certainly stop once the first satisfying query is found for each document, so I may not be able to get the complete set, but then I would at least like some assurance that the queries are executed in a certain order, say 1...N, and each document could be marked with its first satisfying index.
Does anyone know if there's a mechanism in Mongo that can do this?
You can use aggregation.
Use $addFields to add a new field for each query.
You could either $match first, and then add the fields, or add the fields first and on the added fields.

Replace operation: is its write stage atomic with regard to its filter stage?

We're using the replaceOne operation on the collection which uses the "filter" clause to check that it's about to update a document with its certain field having some specific value.
All is good but since the documents in the collection are updated by multiple parties working in parallel, I'd like to know whether it is guaranteed that between the time the filter has found a document to be replaced — that is, the filter has matched, — and that very document gets replaced, it is impossible for that document to be replaced by a concurrently running operation. I failed to find any statement on this in the MongoDB docs.
We're using MongoDB 3.6 (please don't ask why) so using transactions is out of the question.

MongoDB bulk API: Finding out whch bulk updates matched

I'm doing multiple updates in a single bulk. Note: they are updates, not upserts. The problem doesn't allow it. Is there a way to find out which commands form the bulk matched (or didn't)?
From what I saw in the manual, you can only find the number of matches from BulkWriteResult, not which one matched, but I thought I'd ask anyway. Thanks for the help.
The BulkWriteResult doesn't contain this information and, as of MongoDB 2.6.3, there's no way to obtain it from the execution of the bulk operation. Of course, since you specify the criteria to determine which documents are updated, you can find out which documents are updated from the results of a find query with the same criteria. as long as the documents don't change in between. During a multistage bulk operation, you might change what documents match the update.

How can i retrieve modified documents after an update operation in mongodb with pymongo?

I'm using an update operation with upsert. I want to retrieve all documents that have been modified after an update.
for key in categories_links:
collection.update({"name" : key}, {"name": key ,"url" : categories_links[key]}, True)
You should use a timestamp field in your documents if you ever need to find which ones where updated and when. There is a BSON type for that.
To my knowledge, pymongo will not return a list of all of the records which have been modified by an update.
However, if you are using a replicaset, you might be able to accomplish this by looking at the oplog.
According to the documentation:
The oplog must translate multi-updates into individual operations in
order to maintain idempotency. This can use a great deal of oplog
space without a corresponding increase in data size or disk use.
If you want to keep track of each element being updated, you might instead do a find(), and then loop through those to do an individual update() on each. Obviously this would be much slower, but perhaps a tradeoff for your specific use case.

List of updated documents

Is there a way to update (or delete) many documents matching a certain criteria and get the list of IDs of actually updated/deleted documents (or some other fields of those documents)? I cannot simply query the documents matching my criteria beforehand because I need kinda atomicity for this operation. And I can't use findAndModify because it can only process one document at a time which is too slow because of round-trips. Suggestions?
MongoDB only supports atomic operations on a single document.
http://www.mongodb.org/display/DOCS/Atomic+Operations
The only way to do this is to do what you said you didn't one to:
First query the collection to find id's for our query:
db.things.find({"name":"john"}, {_id:1});
Then, use the same query to remove:
db.things.remove({"name":"john"}, {_id:1});
Not ideal, and not atomic, but it's as good as you're going to get in this scenario.