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.

> db.posts.insert(
... {
... _id: ObjectId(5063114bd386d8fadbd6b009),
... title: 'MongoDB OverView',
... description: 'Overview and Introduction about MongoDB',
... by: 'Sneppets',
... url: 'https://docs.mongodb.com',
... tags: ['#mongodb', '#mongo_tutor', '#tutorials'],
... likes: 20,
... comments: [
... {
... user: 'John',
... message: 'Nice tutorial',
... dateCreated: new Date(2019,1,25,7,45),
... like: 10
... },
... {
... user: 'Paul',
... message: 'Good one',
... dateCreated: new Date(2019,1,25,7,45),
... like: 12
... }
... ]
... }
... )

2019-03-06T12:17:47.819+0530 E QUERY [js] SyntaxError: identifier starts immediately after numeric literal @(shell):3:14

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.

Line 3 -> _id: ObjectId(5063114bd386d8fadbd6b009),

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

Syntax: ObjectId(<hexadecimal>)

_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.

_id: ObjectId(‘5063114bd386d8fadbd6b009’),

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

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments