Node.js MongoDB ObjectID查询更新
嘿,伙计们,我需要写一个mongodb查询来更新一个文档,我需要将docuemnt中的一个字段增加1,因此我使用$inc操作,但是我在匹配文档的键和前端发送的数据时遇到了麻烦 我的数据库看起来像这样Node.js MongoDB ObjectID查询更新,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,嘿,伙计们,我需要写一个mongodb查询来更新一个文档,我需要将docuemnt中的一个字段增加1,因此我使用$inc操作,但是我在匹配文档的键和前端发送的数据时遇到了麻烦 我的数据库看起来像这样 _id:ObjectID("5ed4a86126663308549f816b") title:svsbsbsssf description:wdwwdwd category:Object _id:ObjectID("5ebc1144dde4f2974e209526") label
_id:ObjectID("5ed4a86126663308549f816b")
title:svsbsbsssf
description:wdwwdwd
category:Object
_id:ObjectID("5ebc1144dde4f2974e209526")
label:World after Covid
category_id:1
url:world-after-covid
image:https://*****/assets/category-1590699874832.jpg
value:1
// Notice the quotes around category._id
.updateOne({ "category._id": categoryID }, { $inc: { listingViews: 1 } });
所以这个类别是一个对象,我从前端获取我的类别id,在将其转换为ObjectID后,我尝试像这样匹配它
let doc = await req.db
.collection("webinars")
.updateOne({category._id: categoryID }, { $inc: { listingViews: 1 } });
但是updateOne中的过滤器给了我一个错误,我想语法是不正确的
错误就像
意外标记,应为“,”(17:26)
15 | let doc=wait req.db 16 |收集(“网络研讨会”)
17 |.updateOne({category._id:categoryID},{$inc:{listingview:1}})
updateOne
filter对象中存在语法错误,嵌套字段路径即category.\u id
应该是字符串,类似于以下内容
_id:ObjectID("5ed4a86126663308549f816b")
title:svsbsbsssf
description:wdwwdwd
category:Object
_id:ObjectID("5ebc1144dde4f2974e209526")
label:World after Covid
category_id:1
url:world-after-covid
image:https://*****/assets/category-1590699874832.jpg
value:1
// Notice the quotes around category._id
.updateOne({ "category._id": categoryID }, { $inc: { listingViews: 1 } });