【问题标题】:Removing the array element in mongoDB based on the position of element根据元素的位置删除mongoDB中的数组元素
【发布时间】:2011-06-25 13:57:05
【问题描述】:

实际上我需要根据它的位置从数组中删除一个元素。使用 $pop 我们可以从顶部或底部删除元素(将其视为堆栈。顶部的第 0 个元素)如 here 所述。

我们还可以使用 $pull 根据数组中元素的值从数组中删除元素,如here 所述。

但我需要根据位置从数组中删除元素。那么有什么办法可以做到这一点。

【问题讨论】:

  • 在多任务环境中,这并不是一个特别安全的操作。如果两个线程几乎同时尝试删除元素 4 会怎样?幂等或原子操作通常是所有数据库操作所需的。

标签: arrays mongodb pull


【解决方案1】:

来自文档:

{ $pull : { field : {$gt: 3} } } removes array elements greater than 3

所以我想你现在可以这样做:

{ $pull : { field : {$gt: 3, $lt: 5} } } // shoud remove elemet in 4 position 

或者尝试使用position operator 更新,我想应该是这样的:

  { $pull : "field.4" } 

  { $pull : {"field.$": 4}}

这只是一个建议,因为我现在无法测试它。

更新:

看来你不能一步到位(有这样的bug in jira

但是你可以在 position 中使用 unset 元素来移除,并使用 null 值拉取元素:

{$unset : {"array.4" : 1 }}
{$pull : {"array" : null}}

【讨论】:

  • 这样做会导致错误:“无法同时更新array.4和array”
【解决方案2】:

这就是如何使用最新的 mongodb v4.2 来做到这一点的,虽然丑陋但可以工作

     _id: ObjectId("5e4539cb6aebbeaa0a6b8fe7") 
   }, [
        { $set: 
          { notes: 
            { 
              $concatArrays: [
                { $slice: ['$notes', 4] }, 
                { $slice: ['$notes', { $add: [1, 4] }, { $size: '$notes' }]}
              ] 
            } 
          } 
        }
      ])```

【讨论】:

    【解决方案3】:

    这是你的答案:MongoDB pull array element from a collection

    要从某个文档的数组中删除特定元素,首先您需要识别该元素。例如,我有一个包含数组的文档,并且该数组的每个元素都是一个对象:

    {
        "_id" : ObjectId("5140f34888dd50971900002d"),
        "_permissions" : {
            "edit" : [
                {
                    "profile_id" : NumberLong(123),
                    "comment" : "app/project owner"
                },
                {
                    "profile_id" : NumberLong("153579099841888257"),
                    "comment" : "project admin"
                },
                {
                    "profile_id" : NumberLong("153579095869882369"),
                    "comment" : "project admin"
                }
            ],
            "view" : [
                {
                    "profile_id" : NumberLong(123),
                    "comment" : "app/project owner"
                },
                {
                    "profile_id" : NumberLong("153579099841888257"),
                    "comment" : "project admin"
                },
                {
                    "profile_id" : NumberLong("153579095869882369"),
                    "comment" : "project admin"
                }
            ]
        }
    }
    

    所以让我们从_permissions.view 数组中删除带有“153579099841888257”值的profile_id。 来了

    db.collection.update({_id: ObjectId("5140f34888dd50971900002d")}, {$pull:{"_permissions.view": {"profile_id": NumberLong("153579099841888257")}}});
    
    1. 我定义了对象的范围(以确保 id 不会影响任何其他首次找到的文档)
    2. 确定需要提取的元素:profile_id_permissions.view 数组中的值为“153579099841888257”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 2017-12-22
      • 2020-12-11
      相关资源
      最近更新 更多