You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB插入inspections集合报SyntaxError,如何解决?

解决MongoDB insertOne语法错误的方案

以下是你的代码中存在的语法问题及修正方案:

  • _id值格式错误:20032-2020-ACME未用引号包裹,会被解析为减法表达式,必须用引号标记为字符串。
  • 日期构造函数拼写错误:newDate应为new Date(中间需加空格),这是MongoDB/JavaScript的标准日期构造方式。
  • 嵌套对象缺少键名:地址部分的{...}没有指定父键,需添加address:明确这是嵌套的地址对象。
  • 嵌套对象键值对格式错误:"number->1721"这类写法不合法,需改为键名: 值的标准格式,且每个键值对之间用逗号分隔。

修正后的完整代码:

db.inspections.insertOne({
  "_id": "20032-2020-ACME",
  certificate_number: 9998888,
  business_name: "ACME Explosives",
  date: new Date("09-05-2022"),
  result: "Business Padlocked",
  sector: "Explosive Retail Dealer-999",
  address: {
    number: "1721",
    street: "Boom Road",
    city: "BRONX",
    zip: "10463"
  }
})

内容的提问来源于stack exchange,提问作者GuillyStudiesCode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 14:05:20