除去基于元素的位置中的MongoDB数组元素 [英] Removing the array element in mongoDB based on the position of element
问题描述
其实我需要从基于其位置数组中删除的元素。使用$弹出我们可以从顶部或底部除去元件(考虑它作为一个堆栈。在顶部第0个元素),为解释的这里。
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.
推荐答案
从技术文档:
{ $pull : { field : {$gt: 3} } } removes array elements greater than 3
所以我想,你可以现在做somethig是这样的:
So i suppose that you can do somethig like this for now:
{ $pull : { field : {$gt: 3, $lt: 5} } } // shoud remove elemet in 4 position
或者尝试更新使用位置运营商,我想768,16是这样的:
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.
更新:
好像你不能这样做的权利知道在一个步骤(没有在JIRA 这种错误)
Seems you cant do it right know in one step(there is such bug in jira)
但你可以删除位置使用未设置的元素和拉带空值elemets:
But you can remove using unset element in position and that pull elemets with null value:
{$unset : {"array.4" : 1 }}
{$pull : {"array" : null}}
这篇关于除去基于元素的位置中的MongoDB数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!