identifier starts immediately after numeric literal

E QUERY [js] SyntaxError: identifier starts immediately after numeric literal @(shell) in MongoDB

When you are trying to insert document in MongoDB collection you might see this Syntax Error : identifier starts immediately after numeric literal @(shell).

Problem Statement

Let  say you have database called “sneppets” and collection called “posts” and you are trying to insert the following document in the collection using the following mongo shell command.

Solution

From the above output, it is clear that there is a syntax error at line number 3 and column number 14. Let’s have a closer look.

Refer this document https://docs.mongodb.com/manual/reference/method/ObjectId/

_id is the unique  key of the collection of type ObjectId. And if it is not mentioned in the INSERT command, MongoDB will create the unique id of type ObjectId automatically.

As per the reference document, in order to generate a new ObjectId using ObjectId(<hexadecimal>) you need to specify unique hexadecimal string as shown below.

So try adding single quotes for the _id value as shown above to solve “Syntax Error: identifier starts immediately after numeric literal”. Hope this helped.

Related Posts

References

Leave a Reply

avatar
  Subscribe  
Notify of