112

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?

starball
  • 20,030
  • 7
  • 43
  • 238
aamirl
  • 1,420
  • 2
  • 12
  • 18

5 Answers5

196

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.

helmy
  • 9,068
  • 3
  • 32
  • 31
  • See my note in my edited answer regarding clicking on the _id. That will make your life even easier. :-) – helmy Apr 20 '17 at 20:08
  • @helmy does this still work? I'm clicking on `_id` but nothing happens – nachocab Oct 05 '17 at 18:52
  • 1
    @nachocab you have to click on the values you want to query for, on the `SCHEMA` section. Here's the relevant section in the manual: https://docs.mongodb.com/compass/current/schema/#build-query – danii Oct 06 '17 at 14:03
  • I just wonder - why `$oid` got deprecated? In source code, having to import ObjectId as a class to build the id seems like an unnecessary step. – zerefel Feb 23 '19 at 00:19
  • It seems not working: the FIND button is not clickable :( No one of the two solutions proposed... – Aerendir Apr 12 '19 at 18:04
  • {_id: new BSON.ObjectId(“58f8085dc1840e050034d98f”)} – Tony Tullemans Feb 06 '23 at 04:54
32

The current version of MongoDB Compass (1.10.5) no longer appears to support $oid, but it does work with a standard query: {"_id":ObjectId("5a028baa2dc80f2e26a8ed63")}

jpoveda
  • 2,137
  • 1
  • 25
  • 34
4

{"_id":ObjectId("5f8344e2a00ed7f6172a7184")}

hosam hemaily
  • 412
  • 5
  • 17
3
{module_id: ObjectId('5cee65f283774d3470fc01cc')}

Search Result: search result

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Kishan
  • 31
  • 3
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 20 '21 at 06:31
  • In my case replaced "module_id" with only "_id" – HandyPawan Dec 25 '21 at 14:31
0

On MongoDB compass Version 1.36.3 (1.36.3) the following query is working for me to search for a ObjectId in a collection

{ _id: ObjectId('99228b69ebefdf6599311190e') }
Nivethan
  • 2,339
  • 19
  • 22