【问题标题】:Cannot remove embedded document in a nested array of documents无法删除嵌套文档数组中的嵌入文档
【发布时间】: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' }

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

【问题讨论】:

    标签: mongodb adonis.js


    【解决方案1】:

    试试这个查询我得到的输出和你想要的一样

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

    【讨论】:

      【解决方案2】:

      尝试下面的查询,该查询遵循 Mongo DB 文档中使用 $pull in update 的示例

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

      【讨论】:

        猜你喜欢
        • 2018-07-24
        • 1970-01-01
        • 1970-01-01
        • 2014-03-30
        • 1970-01-01
        • 1970-01-01
        • 2013-09-03
        相关资源
        最近更新 更多