将元素从$ pull移动到另一个数组 [英] Move elements from $pull to another array
本文介绍了将元素从$ pull移动到另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在同一集合中有很多文档,如下所示:
I have many documents in the same collection that look like this:
{
"_id": ObjectId("4d525ab2924f0000000022ad"),
"array": [
{ id: 1, other: 23 },
{ id: 1, other: 21 },
{ id: 0, other: 235 },
{ id: 1, other: 765 }
],
"zeroes": []
}
我希望他们看起来像这样:
and I want them to look like this:
{
"_id": ObjectId("4d525ab2924f0000000022ad"),
"array": [
{ id: 1, other: 23 },
{ id: 1, other: 21 },
{ id: 1, other: 765 }
],
"zeroes": [
{ id: 0, other: 235 }
]
}
基本上,我希望能够提取数组中的某些元素并将其推到另一个数组中.我知道我可以使用$pull
有条件地从数组中删除元素,但是可以重新放置这些拉出的元素吗?
Basically, I want to be able to pull some elements in an array, and push it to another array. I know I can use $pull
to conditionally remove elements from an array, but is it possible to relocate these pulled elements?
推荐答案
不是.您可以使用光标进行这样的操作.
Not really. You can use cursor for operation like this.
db.foo.find({}).forEach(function(doc) {
doc.zeros = doc.array.filter(function(x) { return x.id == 0 });
doc.array = doc.array.filter(function(x) { return x.id != 0 });
db.foo.save(doc)};
)
这篇关于将元素从$ pull移动到另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文