Searching by ObjectId on Mongo Compass

aamirl picture aamirl · Apr 20, 2017 · Viewed 33.4k times · Source

How does one use Mongo Compass and search by ObjectID? I've been searching for the documentation for this but haven't been successful with anything. I have tried:

{ "_id" : "58f8085dc1840e050034d98f" }

{ "$oid" : "58f8085dc1840e050034d98f" }

{ "id" : "58f8085dc1840e050034d98f" }

None of those seem to work and it's getting quite frustrating. Also, sidenote - is it possible to set the skip/limit when displaying documents in Compass?

Thanks in advance!

Answer

helmy picture helmy · Apr 20, 2017

UPDATE Newer versions of Compass now support querying ObjectId similar to how they would be queried via the mongo shell (the $oid syntax will not work in these newer versions):

{_id: ObjectId('58f8085dc1840e050034d98f')}

If you're using an older version before 1.10.x you, enter the following into the query box:

{"_id":{"$oid":"58f8085dc1840e050034d98f"}}

It's also worth pointing out that in the UI you can click on one of the _ids and it will auto-populate the query box with the query based on what you clicked. You can also shift+click on multiple fields to create compound (and-ed) query criteria, or you can click and drag to select a range.

Skip and Limit are support for versions >= 1.8.x does support skip and limit when browsing under the Documents tab. Click the "Options" button on the right side of the Query Bar. See the Query Bar docs for illustration and details.

The Schema tab only supports limit, as this will do a sampling of documents and skip doesn't really make sense in that context.

In order to click on the _ids you need to be on the Schema tab. If your _ids are of type ObjectId, the visualization of the distribution will appear as a date range and you can drag over one or more lines to populate the query based on _id. If your _ids are some other type, some portion of them will display individually and you can click, drag, or shift-click over them.