2017-03-02 117 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); 
}); 
相关问题