E QUERY [thread1] SyntaxError: Does the identifier start immediately after the numeric literal @ (shell) in 'mongodb'?

my first mongodb request is doing fine and the second is generating some kind of error in the _id field?

for (var i = 1; i <= 25; i++){db.mediafiles.insert({x : i,from_email: '123@gmail.com'})}
WriteResult({ "nInserted" : 1 })

for (var i = 1; i <= 25; i++){db.mediafiles.insert({x : i,from_email: '123@gmail.com',_id:59193333aed3eb391e396a5d})}
E QUERY    [thread1] SyntaxError: identifier starts immediately after numeric literal @(shell):1:90

      

+3


source to share


1 answer


I'll just explain the difference between the first and second queries.

https://docs.mongodb.com/manual/reference/method/ObjectId/

ObjectId is the unique key of the collection. If it is not mentioned in the INSERT, mongodb fills in the data automatically. So, the first request succeeded for 25 iterations.



Whereas in the second INSERT statement you are trying to fill in the value. So, you need to provide a unique key for ObjectId

. Otherwise, you will get an error E11000 duplicate key error collection

.

The following code should fix the problem. The code below creates an ObjectId and sets an insert value.

for (var i = 1; i <= 25; i++) { 
    id = ObjectId();
    db.AbcSchema.insert({x : i, from_email: '123@gmail.com',_id: id})
};

      

+2


source







All Articles