【发布时间】:2018-04-24 09:15:00
【问题描述】:
我使用的是 MongoDB (v3.4.10) 和 Nodejs AdonisJs(v4.1) 框架,lucid-mongo(5.0.2) 是我用来连接 MongoDB 的模块。
我的架构是:
{
"_id" : ObjectId("5ad9e8e4e4050a464b083258"),
"courseId" : ObjectId("5ad9e8cde4050a464b083256"),
"title" : "Introduction",
"status" : "Active",
"resources" : [
{
"title" : "Study Materials",
"details" : [
{
"itemCode" : "MINT-1524219357914",
"title" : "Finance ",
"format" : "Video"
},
{
"itemCode" : "MINT-1524213969212",
"title" : "Account",
"format" : "Ebook"
},
{
"itemCode" : "MINT-1524219357914",
"title" : "Finance Video",
"format" : "Video"
},
{
"itemCode" : "MINT-1524213969212",
"title" : "Tax",
"format" : "Ebook"
}
]
},
{
"title" : "Videos",
"details" : [
{
"itemCode" : "MINT-1524408901486",
"title" : "Test video",
"format" : "Video"
}
]
}
],
"createdAt" : ISODate("2018-04-20T13:19:32.317Z"),
"updatedAt" : ISODate("2018-04-22T14:55:09.073Z")
}
我想从 resources-> details 中删除文档,其中包含 itemCode MINT-1524219357914 下面是我写的查询
await mongoClient.collection('course_modules')
.update({_id : ObjectId("5ad9e8e4e4050a464b083258") }, { $pull: { 'resources.$.details': { $elemMatch: { 'itemCode': 'MINT-1524219357914' } } } }, { multi: true })
当我运行这个查询时,我得到一个错误:
{ MongoError: The positional operator did not find the match needed from the query. Unexpanded update: resources.$.details
at Function.MongoError.create (/opt/lampp/htdocs/nodeApps/l_and_ls/node_modules/mongodb-core/lib/error.js:45:10)
at toError (/opt/lampp/htdocs/nodeApps/l_and_ls/node_modules/mongodb/lib/utils.js:149:22)
at /opt/lampp/htdocs/nodeApps/l_and_ls/node_modules/mongodb/lib/collection.js:1035:39
at /opt/lampp/htdocs/nodeApps/l_and_ls/node_modules/mongodb-core/lib/connection/pool.js:544:18
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoError',
message: 'The positional operator did not find the match needed from the query. Unexpanded
update: resources.$.details',
driver: true,
index: 0,
code: 16837,
errmsg: 'The positional operator did not find the match needed from the query. Unexpanded update:
resources.$.details' }
我没有发现此查询有任何问题。谁能帮我解决这个问题...??
【问题讨论】: