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

MongoDB插入文档触发SyntaxError,请求协助排查错误原因

MongoDB插入文档语法错误解决

问题出在带空格的字段名上:JavaScript对象的属性名如果包含空格,必须用引号(单引号或双引号)包裹起来,否则会触发SyntaxError。

你代码里的vegetarian options和vegan options这两个字段名带有空格,却没有用引号包裹,导致解析失败。

修正方案一:给带空格的字段名加引号

db.Eateries.insertOne({
   id: "R0001",
   name: "La Farina",
   location: "Melbourne",
   cuisine: ["Italian", "Vegetarian"],
   owner: "Jaime Holiber",
   seating: 35,
   byo: "wine",
   style: "cafe",
   "vegetarian options": true,
   "vegan options": false
})

修正方案二:改用驼峰命名(更符合JS编码习惯)

把带空格的字段名改成驼峰格式,不用加引号也能正常解析:

db.Eateries.insertOne({
   id: "R0001",
   name: "La Farina",
   location: "Melbourne",
   cuisine: ["Italian", "Vegetarian"],
   owner: "Jaime Holiber",
   seating: 35,
   byo: "wine",
   style: "cafe",
   vegetarianOptions: true,
   veganOptions: false
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 19:53:16