Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View last N documents using MongoDB Compass

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?

like image 554
koan911 Avatar asked Mar 13 '17 08:03

koan911


People also ask

How do I get most recent files in MongoDB?

To get last inserted document, use sort() along with limit(1).

What does find () do in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents.


2 Answers

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

like image 122
Oleks Avatar answered Sep 22 '22 13:09

Oleks


(@Oleksandr I learned from your effective answer; thank you.)

I've also been shown that the Compass Schema tab allows one to drag a date range on the _id field to apply a filter query for that range. That range can be successively narrowed as desired.

enter image description here

like image 31
koan911 Avatar answered Sep 21 '22 13:09

koan911