Most recently added collection in a Mongo DB - mongodb

Within a Mongo Database, how do i get the most recently added/updated collection?
> show collections;
> collection_1
> collection_2
> collection_3
> ...........
> ...........
> collection_n
n can vary from 1 to 1000;
My application adds a new collection or might update an existing collection. How do retreive the last updated or a newly added collection within a database?
Methods that i looked in the internet,applies to within a collection,
for example the answers shown here Get the latest record from mongodb collection

there is no built in solution to it, you need implement it.
you can implement this solution diagrammaticall.
Ie.: log last operation timestamp for each collection, you can store in new
collection;

Collection's metadata doesn't have any created date.
So, A collection does not "know" when it was created.
In short it can not be possible!

by using updated column date and time you can get the result
db.collectionname.find().sort( { UpdatedOn: -1 } )

Related

Mongodb new data insert in top

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.

In MongoDB find out when last query to a collection was? (Removing stale collections)

I would like to find out how old/stale a collection is, I was wondering if there was a way to know when the last query was made to a collection, or even get a list of all collections last access date.
If your Mongodb collection document _id is of the following format "_id" : ObjectId("57bee0cbc9735bf0b80c23e0") then Mongodb stores the create document timestamp.
This can be retrieved by executing the following query
db.newcollection.findOne({"_id" : ObjectId("57bee0cbc9735bf0b80c23e0")})._id.getTimestamp();
the result would be an ISODate like this ISODate("2016-08-25T12:12:59Z")
find out how old/stale a collection
There is no predefined libraries available in mongodb to track the oldness of a collection. But it is doable by maintaining a log where we can keep an entry when we are accessing a collection.
References
ObjectID.getTimestamp()
Log messages
Rotate Log files
db.collection.stats()

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)

How to find last update/insert/delete operation time on mongodb collection without objectid field

I have some unused collections in the MongoDb database. I've to find out when the CRUD operations done against collections in the database. We have our own _id field instead of mongo's default object_id. We dont have any time filed in the collections to find out the modification time. is there any way to find out the modification time of collections in mongodb from meta data? Is there any data dictionay informations like in oracle to find out this? please give some idea/workarounds
To make a long story short: MongoDB has a flexible schema. Simply add a date field. Since older entries don't have it, they can not be the last entry.
Let's call that field mtime.
So after adding a date field to your schema definition, we generate an index in descending order on the new field:
db.yourCollction.createIndex({mtime:-1})
Finding the last mtime for a collection now is easy:
db.yourCollection.find({"mtime":{"$exists":true}}).sort({"mtime":-1}).limit(1)
Do this for every collection. When the above query does not return a value within the timeframe you defined for purging a collection, simply drop it, since it has not been modified since you introduced the mtime field.
After your collections are cleaned up, you may remove the mtime field from your schema definition. To remove it from the documents, you can run a simple query:
db.yourCollection.update(
{ "mtime":{ $exists:true} },
{ "$unset":{ "mtime":""} },
{ multi: true}
)
There is no "data dictionary" to get this information in MongoDB.
If you've enabled the profiling level in advance to log all operations (db.setProfilingLevel(2)) and you haven't had many operations to log, so that the system.profile capped collection hasn't overwritten whatever logs you are interested in, you can get the information you need there—but otherwise it's gone.

`mongo` Query To Purge Old Entries

Anyone have a handy mongo command to remove all entries from a DB that are older than X date/ X days?
Basically have a dev and production DB, I'm looking to prune the dev DB out a bit to limit size.
Thanks for the help!
You could do something like this from the mongo shell.
var older=Date.parse("2013-03-01"),collection=db.so,all=collection.find();
all.forEach(function(doc) { var ts = doc._id.getTimestamp();
if (ts < older) { collection.remove(doc); } });
The above line (which you'd paste into the shell) will delete all documents in the specified collection (collection=db.so) created before the first of March, 2013. It relies on the fact that each ObjectId has an embedded timestamp (based on the timestamp of document creation (docs)), which can be retrieved and used.
You could of course change the query to look for a specific timestamp field in a document.
if (doc.timestampField < older) { collection.remove(doc); } })
You can use the mongo concept of deleting data after some specified amount of time, Expire Data from Collections by Setting TTL.
Please refer below link to do so
http://docs.mongodb.org/manual/tutorial/expire-data/