View last N documents using MongoDB Compass
Asked Answered
C

3

11

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?

Cess answered 13/3, 2017 at 8:5 Comment(0)
C
10

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

Cupo answered 13/3, 2017 at 10:17 Comment(2)
Is there no easier way - LIMIT doesn't take a count-from-the-end (last N) value?Deportee
In Oleksandr's picture you can see the skip option on the last line. Just fill in a number here to skip the first N records.Loose
D
5

Skip is descibed here https://docs.mongodb.com/compass/current/query/skip/

  1. In the Query Bar, click Options.
  2. Enter an integer representing the number of documents to skip into the Skip field
  3. Click Find to run the query and view the updated results.

enter image description here

Deflection answered 12/9, 2021 at 22:46 Comment(1)
The limit thing helped. I did not notice in my 2 years of usage that we have limit option. I knew about options because I sort data all the time. Thanks!Izmir
C
2

(@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

Cess answered 14/3, 2017 at 1:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.