2017-01-21 25 views
0

---插入---更新的ArrayList大小的MongoDB集合中的計數

db.mycollection.insert({'foo':[1,2,3,4]}) 
    db.mycollection.insert({'foo':[5,6,7]}) 

--ArrayList計數---

db.mycollection.aggregate({$project: { count: { $size:"$foo" }}}) 

我想ArrayList中的計數來更新我的收藏尺寸。

回答

1

不幸的是,你不能在服務器端使用MongoDB完成目前的操作。你必須使用一些客戶端代碼。在mongo shell中你可以這樣做:

db.mycollection.find().forEach(function(doc){ 
    doc.fooSize = doc.foo.length; 
    db.mycollection.save(doc) 
) 
+0

非常感謝Ares :)它的工作就像魅力:) – Art