2

同じコレクションに次のようなドキュメントが多数あります。

{
  "_id": ObjectId("4d525ab2924f0000000022ad"),
  "array": [
    { id: 1, other: 23 },
    { id: 1, other: 21 },
    { id: 0, other: 235 },
    { id: 1, other: 765 }
  ],
  "zeroes": []
}

そして、私はそれらを次のようにしたい:

{
  "_id": ObjectId("4d525ab2924f0000000022ad"),
  "array": [
    { id: 1, other: 23 },
    { id: 1, other: 21 },
    { id: 1, other: 765 }
  ],
  "zeroes": [
    { id: 0, other: 235 }
  ]
}

基本的に、配列内のいくつかの要素をプルし、それを別の配列にプッシュできるようにしたいと考えています。配列から要素を条件付きで削除するために使用できることはわかっています$pullが、これらのプルされた要素を再配置することは可能ですか?

4

1 に答える 1

1

あまり。このような操作にカーソルを使用できます。

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)};
)
于 2013-11-08T13:06:03.973 に答える