Q: Is there a quicker way to query _id values in Studio 3T? I hate typing out the same old query!
Welcome to the latest Studio 3T Ask Manatees Anything – #Studio3T_AMA. Want to know how to use Studio 3T to make your MongoDB life better? Just tweet and use #Studio3T_AMA or email [email protected].
A: It can be so tedious typing out the same old queries for the _id field. It’s the unique id for every document and so it becomes embedded in your daily query life. After a while typing
{ "_id": ObjectId(" the object id goes here") }
becomes second nature to you and you settle in to digging through various documents. But there is another way.
Enter Studio 3T Query _id shortcuts.
If you write a string, date, or number into the query field of the Collection Tab and hit return, Studio 3T will look at it, try and determine what type it is and create an id filter for that on the spot.
ObjectId _id
Let’s start with ObjectId; the default type for _id fields. If you enter the hexadecimal number that conforms to an ObjectId:
…and then hit return, it expands out to:
That’s wrapped the value in an ObjectId, complete with quotes and all inside a { “_id”: … }:
{ "_id" : ObjectId("57d28452ed5d4d54e8686f9d") }
Because you may be copying these values from code or configuration files, Studio 3T also recognises an ObjectId if it has single or double quotes around it.
UUID _id
Next up, UUIDs. UUIDs are an alternative to ObjectIds; they carry less information than an ObjectId, but their unique qualities make them useful as a unique id across systems. They appear as yet another hexadecimal based string, one which can be identified by Studio 3T. That means entering a UUID string
Hit return and that’s transformed into:
Integer _id
Are you old school with your _id field? Using integer values for records. We’re here to help with that too:
Hit return for a perfectly formed integer id query:
Don’t put quotes around this one though because that would force it to be interpreted as a string. Talking of which…
String _id
Not the most common of index types, but if you do use strings for your index, you should know you can put a single or double quoted string in:
And after hitting return, you’ll have an _id query using that string:
Wrapping up
The _id shortcut may look simple, but it can save a lot of time if you spend your time looking up records by their _id. It’s a Studio 3T tip everyone should know.