2017-03-02 75 views
0

移動項目我有這樣MongoDB中添加屬性,並從其他

{ 
name : "test" 
data : [{"itemcode":"a1"},{"itemcode":"a2"}] 
} 

我需要改變,以

{ 
name : "test" 
shops :[ 
    { "shopid" : 1, 
    "data" : [{"itemcode":"a1"},{"itemcode":"a2"}] 
}] 
} 

我嘗試更新的對象,但我不能複製的陣列仍然ramain空。更新我使用

db.getCollection("shop").find({}).forEach(function(doc){ 
var a = doc.data; 
var s = [{ 
    "shopid":NumberInt(1), 
    "data": a}] 

db.collection.save(doc); 
}); 

回答

2

您需要指定數組到doc.shops。

db.getCollection("shop").find({}).forEach(function(doc){ 
    var a = doc.data; 
    doc.shops = [{ 
     "shopid":NumberInt(1), 
     "data": a}]; 

    delete doc.data; 

    db.collection.save(doc); 
}); 
相關問題