2017-03-08 131 views

回答

0

我看到一些鏈接,並作出適當的爲您解決問題

首先,你需要得到你的所有列,你可以這樣做與MapReduce的:

mr = db.runCommand({ 
    "mapreduce" : "my_collection", 
    "map" : function() { 
    for (var key in this) { emit(key, null); } 
    }, 
    "reduce" : function(key, stuff) { return null; }, 
    "out": "my_collection" + "_keys" 
}); 

然後運行的結果集合不同這樣才能找到所有的按鍵:

columns = db[mr.result].distinct("_id") 

,並重新命名所有匹配列

columns.forEach(function(columnName) { 
    if (columnName.indexOf('$') == 0) { 
     var newColumnName = columnName.replace('$', '&'); 
     rename_query = { '$rename': {} }; 
     rename_query['$rename'][columnName] = newColumnName; 
     db.my_collection.updateMany({}, rename_query) 
    } 
}) 

參考鏈接是

MongoDB Get names of all keys in collection

MongoDB $rename javascript variable for key name

+0

其實,我不知道該字段名稱,我所知道的是,有與$開始字段。有什麼我們可以使用的運營商嗎? – ashwin

+0

我已根據您的要求更新了答案 – Gaurav

+0

感謝@gaurav,我有另外幾個字段名稱在數組中的問題。 – ashwin

相關問題