Reputation: 299
Looking for help querying MongoDB for the latest inserted documents, to be run on a minute-to-minute basis.
Upvotes: 1
Views: 463
Reputation: 9504
Thilo had the gist.
If you need to get the most recently inserted "top level documents" (those you have table/collections of) you can use either ObjectId
values since they are timestamped, or you can add a timestamp field to the document itself.
If you need to timestamp embedded documents (ex. Items in an Order) then you would not be able to use ObjectIds as your embedded collection are not "top level documents".
I'm not sure how accurate you need to be, but you may want to look into the issue of how records get inserted, as the timestamps may not always match the "real order" the items were submitted...
Also, look in to seeing if the timestamp on the ObjectId of top-level documents is altered when updated (a new item to an embedded collection) as that would affect things too.
Bottom line, when in doubt, add timestamp fields and write queries for them.
Upvotes: 2