Mongodb new data insert in top - mongodb

Usually mongodb saves previous data on top. Can we save data as new at the top?
Mean i want to insert my new data on 0 no index every time and pervious data will go down as Array.unshift() method...

Not sure if I got you right.
Within a document:
This could be helpful $position
Within a collection:
You could just sort by _id descending when accessing the data to get new documents first.

Related

Firestore delete an index inside an array

i am trying to delete an index inside array
this is the data structure
I want the user to delete the selected car so it could be index 0 or whatever
My opinion is fetch the document and delete the item at which index you want from the local array and update the remote document. Or you can use .document('docID').updateData('cars': FieldValue.arrayRemove([{the-item-map}]));. Maybe there are other options but these two options are acceptable, I prefer to use first one.

MongoDb get position of an item in the collection with sorting

I use a table with sorting and pagination.
To get data I use find with sort, skip, limit, to get data for the current page of the table.
Question: When I add a new item to the collection, I need to open the page in the table.
That is, I need to get the position of the item in the collection, taking into account the sorting.

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);

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)

Inserting new data into a record in MongoDB

I'm trying to insert data collected from a html form into an existing document. These will all be new fields and these are a python dict. However when I perform the insert or update it seems to override the original data.
I initially store the data as:
self.settings['db'].userInfo.insert(userData)
Then performed the update/insert as:
self.settings['db'].userInfo.update({'_id':DBid},locData)
My record now just holds data for the latter update.
How can I keep the original data in the document and just add in new data later on?
EDIT
I was able to add new values into the document by:
self.settings['db'].userInfo.update({'_id':DBid},{"$set":userData})
I used the same dict as before now and used the $set operator. This appended the document with the new fields and thier values.
You should be doing another insert, not an update.
An update changes the original value... That's what it's designed to do, so another insert will add a new record to the collection.
To keep all the old fields of the document, and add some more use the Operator $set.