我正在使用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")
}
我想从资源中移除文档 - >详细信息,其中包含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' }
我没有看到此查询有任何问题。任何人都可以帮我解决这个问题...... ??
答案 0 :(得分:0)
尝试以下查询,该查询遵循Mongo DB文档中使用$pull in update
的示例db.course_modules.update(
{_id : ObjectId("5ad9e8e4e4050a464b083258") },
{ $pull : {
resources : {
details : {
$elemMatch : { itemCode : "MINT-1524219357914" }
}
}
}
} ,
{ multi: true });
答案 1 :(得分:0)
尝试此查询我输出的内容与您想要的相同
db.pulloperations.update(
{_id : ObjectId("5ad9e8e4e4050a464b083258"),"resources.details.itemCode" : "MINT-1524219357914" },
{ $pull : {
"resources.$.details" :
{ itemCode : "MINT-1524219357914" }
}} ,
{ multi: true });