Getting the last inserted document Id - mongodb

I have tried to get the last saved documentId from the mongo database. But whatever I try I don't get anything back.
Can someone please hint me how to solve this in C# i have tried doing the sorts and take the last document but i don't get anything back.

MongoDB does not have such an operation.
If you want to know the id of inserted document, generate the id on the client side before inserting.

Related

How to make a mongo query to find all documents where an array contains a given id?

I am not sure what i am doing wrong. I have a User collection which has a field "followingUsers" (array). This array contains the id's of other users. When an user gets deleted i want that every other user document no longer has a reference to the deleted user in their followUsers array. I am not experienced with mongo so i decided to make a find query first, but i get back an empty result on each attempt.
I wrote the following queries and none of them seem to work:
db.users.find({"followingUsers": "63d0522c655fa263cfe003e7"})
db.users.find({"followingUsers": {"$all": ["63d0522c655fa263cfe003e7"] } } )
In the image you can see the data i expect to get back: Data i expect to get back
What am i doing wrong?
With the help of the user with nickname ray, i found the correct queries:
db.users.find( {'followingUsers': ObjectId("63cff74e3ae933124ee20d05")} )
db.users.find( {'followingUsers': {"$all":[ ObjectId("63cff74e3ae933124ee20d05") ] }} )
I was indeed storing ObjectId's inside the array and not just strings.

MongoDB: How to get the last updated timestamp of the last updated document in a collection

Is there a simple OR elegant method (or query that I can write) to retrieve the last updated timestamp (of the last updated document) in a collection. I can write a query like this to find the last inserted document
db.collection.find().limit(1).sort({$natural:-1})
but I need information about the last updated document (it could be an insert or an update).
I know that one way is to query the oplog collection for the last record from a collection. But it seems like an expensive operation given the fact that oplog could be of very large size (also not trustworthy as it is a capped collection). Is there a better way to do this?
Thanks!
You could get the last insert time same way you mentioned in the question:
db.collection.find().sort({'_id': -1}).limit(1)
But, There isn't any good way to see the last update/delete time. But, If you are using replica sets you could get that from the oplog.
Or, you could add new field in document as 'lastModified'.
You can also checkout collection-hooks. I hope this will help
One way to go about it is to have a field that holds the time of last update. You can name it updatedAt. Every time you make an update to the document, you'll just update the value to the current time. If you use the ISO format to store the time, you'll be able to sort without issues (that's what I use).
The other way is the _id field.
Method 1
db.collection.find().limit(1).sort({updatedAt: -1})
Method 2
db.collection.find().limit(1).sort({_id: -1})
You can try with ,
db.collection.findOne().sort({$natural:-1}).limit(1);

Is ObjectId in mongodb a constant?

Is ObjectId in mongodb a constant or can it change after the creation of the document?
Can update or any other operation change it?
My ObjectId seems to have changed for a couple of documents and I cannot figure out why.
There is absolutely no way that your object ID would have changed.
The reason being is Since the object ID is created on the following :
Year ,
month,
Date ,
Hours,
Minutes,
Seconds.
So, there is Absolutely NO Way in which your _id would have changed.
Moreover refer the following , you can't even Update the object id. Its created by mongoDb for every insertion.
Nope, _id field (if that's what you mean by ObjectId) is read-only. Other fields of type ObjectId are fully mutable, of course.
My ObjectId seems to have changed for a couple of documents and I cannot figure out why.
Impossible. But, you can create a new document, which is a copy of another document, has all the same fields, but id is different. Then you delete the original document. Et voila, document appears to have id changed.
Check your code, maybe something like that can happen.

Delete a document from mongodb with _id NaN

I am not able to delete a document from MongoDB using Mongo Express when it has _id equal to NaN.
After pressing BIN button it says:
Document not found!
Any ideas how to manage it from Mongo Express?
Considering that the url and dateUpsertFromFile is not empty, I believe that the document do exist in the database. Perhaps incorrect data binding? You can do db.collection.remove({url: "Vista Room Near BBC Towers...<complete the url>"}) to delete it instead, but my previous point stands. You should be able to find the _id somewhere.

In mongo db how do I see a very specific data in a collection?

I have user collection and inserted 3 record but want to see the third record only. I did db.users.find(3), gives an error.
use db.users.find().limit(1).skip(2)
or if you want to see the last inserted document use:
db.users.find().sort({_id:-1}).limit(1)