根据元素位置删除mongoDB中的数组元素

发布于 2024-10-16 17:52:07 字数 376 浏览 3 评论 0原文

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

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

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

Actually I need to remove an element from the array based on its position. Using $pop we can remove element from top or bottom (considering it as a stack. 0th element at top) as explained here.

We can also remove element from array based on the value of the elements in the array using $pull as explained here.

But I need to remove element from the array based on position. So is there any way I can do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明明#如月 2024-10-23 17:52:07

从文档:

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

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

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

或者尝试使用 位置运算符,我想应该是这样的:

  { $pull : "field.4" } 

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

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

更新:

似乎你无法一步到位(有这样的 jira 中的错误

但是您可以使用位置中未设置的元素来删除并拉取具有空值的元素:

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

From documentation:

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

So i suppose that you can do somethig like this for now:

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

Or try update using position operator, i suppose shoud be something like this:

  { $pull : "field.4" } 

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

It is only a suggestion, because i can't test it right now.

Update:

Seems you cant do it right know in one step(there is such bug in jira)

But you can remove using unset element in position and that pull elemets with null value:

{$unset : {"array.4" : 1 }}
{$pull : {"array" : null}}
纸短情长 2024-10-23 17:52:07

这就是如何使用最新的 mongodb v4.2 做到这一点,丑陋但有效

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

That is how possible to do that with latest mongodb v4.2, ugly but working

     _id: ObjectId("5e4539cb6aebbeaa0a6b8fe7") 
   }, [
        { $set: 
          { notes: 
            { 
              $concatArrays: [
                { $slice: ['$notes', 4] }, 
                { $slice: ['$notes', { $add: [1, 4] }, { $size: '$notes' }]}
              ] 
            } 
          } 
        }
      ])```
旧时模样 2024-10-23 17:52:07

这是您的答案: MongoDB 从集合中提取数组元素

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

{
    "_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 中使用“153579099841888257”值.view 数组

Here's your answer: MongoDB pull array element from a collection

To remove specific element from an array of some document first you need to identify this element. For instance I have a document with array and every element of this array is an object:

{
    "_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"
            }
        ]
    }
}

So let's remove profile_id with "153579099841888257" value from _permissions.view array.
Here we go

db.collection.update({_id: ObjectId("5140f34888dd50971900002d")}, {$pull:{"_permissions.view": {"profile_id": NumberLong("153579099841888257")}}});
  1. I define scope of the object (to make sure id doesn't affect any other first found document)
  2. Identify needed element to pull out: profile_id with "153579099841888257" value in the _permissions.view array
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文