无法删除嵌套文档数组中的嵌入文档
Cannot remove embedded document in a nested array of documents
我正在使用 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' }
我没有发现此查询有任何问题。谁能帮我解决这个问题...??
尝试以下查询,该查询遵循 Mongo 数据库文档中使用 $pull in update
的示例
db.course_modules.update(
{_id : ObjectId("5ad9e8e4e4050a464b083258") },
{ $pull : {
resources : {
details : {
$elemMatch : { itemCode : "MINT-1524219357914" }
}
}
}
} ,
{ multi: true });
试试这个查询,我得到的输出和你想要的一样
db.pulloperations.update(
{_id : ObjectId("5ad9e8e4e4050a464b083258"),"resources.details.itemCode" : "MINT-1524219357914" },
{ $pull : {
"resources.$.details" :
{ itemCode : "MINT-1524219357914" }
}} ,
{ multi: true });
我正在使用 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' }
我没有发现此查询有任何问题。谁能帮我解决这个问题...??
尝试以下查询,该查询遵循 Mongo 数据库文档中使用 $pull in update
的示例db.course_modules.update(
{_id : ObjectId("5ad9e8e4e4050a464b083258") },
{ $pull : {
resources : {
details : {
$elemMatch : { itemCode : "MINT-1524219357914" }
}
}
}
} ,
{ multi: true });
试试这个查询,我得到的输出和你想要的一样
db.pulloperations.update(
{_id : ObjectId("5ad9e8e4e4050a464b083258"),"resources.details.itemCode" : "MINT-1524219357914" },
{ $pull : {
"resources.$.details" :
{ itemCode : "MINT-1524219357914" }
}} ,
{ multi: true });