View last N documents using MongoDB Compass

koan911 picture koan911 · Mar 13, 2017 · Viewed 13.1k times · Source

I wish to view in MongoDB Compass the last N documents in a very large collection; too many to scroll through.

I could .skip(total - N) if I knew the syntax for that within Compass.

Alternatively, I have a date field and could use $gte with a date if I knew how to express a date in a manner acceptable to Compass.

Suggestion/example how to do this, please?

Answer

Oleks picture Oleks · Mar 13, 2017

MongoDB Compass 1.6.1(Stable)

For date comparison you need to use $date operator with a string that represents a date in ISO-8601 date format.

{"date": {"$gte": {"$date": "2017-03-13T09:51:26.317Z"}}}

In my case the values of date field in Compass and mongo shell are different. So firstly I query the documents in the shell and then copy the "2017-03-13T09:51:26.317Z" from the result to the Compass filter line. In mongo shell it look like:

{
    ...
    "date" : ISODate("2017-03-13T09:51:26.317Z"), 
    ...
}

MongoDB Compass 1.7.0-beta.0 (Beta)

This version have an advanced query bar that lets you input not just the filter (as before), but also project, sort, skip and limit enter image description here