Reputation: 129
I'm trying to run this MongoDB script:
db.test_collection.insert({
_id: ObjectId(7df78ad8902c),
title: ‘Mongo Db practice’,
description: ‘this class is about MongoDB’
})
and keep getting this error:
SyntaxError: identifier starts immediately after numeric literal @(shell):1:42
I think it has to do with with the _id and the ObjectId(7df78ad8902c) because when I put ObjectId("stringliteral") the error message changes.
Any help is appreciated.
Upvotes: 3
Views: 9994
Reputation: 233
I ran into this same issue while following the MongoDB quick guide at https://www.tutorialspoint.com/mongodb/mongodb_quick_guide.htm. The example for the insert method is incorrect:
db.mycol.insert({ _id: ObjectId(7df78ad8902c), title: 'MongoDB Overview', description: 'MongoDB is no sql database', by: 'tutorials point', url: 'http://www.tutorialspoint.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 })
There are two problems with the example. First, the ObjectId needs to be enclosed in quotes. Second, the example has an invalid object id. The id should be a 12-byte hexadecimal value. For more information on what a 12-byte hexidecimal value is, see https://docs.mongodb.com/manual/reference/method/ObjectId/ and How is a MongoDB ObjectID 12 bytes?
Upvotes: 1
Reputation: 1342
try like this
db.test_collection.insert({
_id: ObjectId('7df78ad8902c'),
title: "Mongo Db practice",
description: "this class is about MongoDB"
})
For best practices don't add _id like 7df78ad8902c
it's not a correct mongoId. You can have issue with it in future.
if you have any question feel free to ask
Upvotes: 7