语法错误:标识符在数字文字之后立即开始 – MongoDB 错误

Kam*_*rov 3 database syntax syntax-error mongodb

我正在尝试运行这个 MongoDB 脚本:

db.test_collection.insert({ 
_id: ObjectId(7df78ad8902c), 
title: ‘Mongo Db practice’, 
description: ‘this class is about MongoDB’ 
})
Run Code Online (Sandbox Code Playgroud)

并不断收到此错误:

SyntaxError: identifier starts immediately after numeric literal @(shell):1:42
Run Code Online (Sandbox Code Playgroud)

我认为它与 _id 和 ObjectId(7df78ad8902c) 有关,因为当我放置 ObjectId("stringliteral") 时,错误消息会发生变化。

任何帮助表示赞赏。

小智 6

像这样尝试

db.test_collection.insert({ 
    _id: ObjectId('7df78ad8902c'), 
    title: "Mongo Db practice", 
    description: "this class is about MongoDB" 
})
Run Code Online (Sandbox Code Playgroud)

对于最佳实践,不要添加 _id,因为7df78ad8902c它不是正确的 mongoId。您将来可能会遇到问题。

如果您有任何问题,请随时提出