Mongo Shell SyntaxError Identifier Directly After Number

Working with MongoDB 6.0 and mongosh 1.10.0.

The Problem

By the looks of it, mongosh interprets its input as Javascript, which renders the below syntax invalid.

mydb> db.5cf6cb51f461500001617292.find({},{_id:1}).hint({_id:1});
Uncaught:
SyntaxError: Identifier directly after number. (1:4)

> 1 | db.5cf6cb51f461500001617292.find({},{_id:1}).hint({_id:1});
| ^
2 |

The Solution

Use getCollection() method instead:

mydb> db.getCollection("5cf6cb51f461500001617292").find({},{_id:1}).hint({_id:1});
[
{ _id: ObjectId("6107ce94b2e302000f8816a7") },
{ _id: ObjectId("64380deca3e32f000f203b13") }
]

Leave a Reply

Your email address will not be published. Required fields are marked *