无法在嵌套的文档阵列中删除嵌入式文档



我正在使用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")
     }

我想从资源 ->详细信息中删除该文档,该详细信息 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' }

我没有看到此查询的任何问题。谁能帮我解决这个问题... ??

尝试此查询,我得到的输出与您想要的

相同
db.pulloperations.update(
{_id :  ObjectId("5ad9e8e4e4050a464b083258"),"resources.details.itemCode" : "MINT-1524219357914"  },
{ $pull : {     
   "resources.$.details" : 
       { itemCode : "MINT-1524219357914" } 
}} ,
{ multi: true });

尝试以下查询,该查询遵循mongo db文档中的示例,用于使用$ pull in Update

db.course_modules.update(
{_id :  ObjectId("5ad9e8e4e4050a464b083258") },
{ $pull : { 
    resources : { 
       details : {
          $elemMatch : { itemCode : "MINT-1524219357914" } 
       }
    }
  }
} ,
{ multi: true });

最新更新