回答:
以下を使用できます。
db.foo.update({}, {$rename:{"name.additional":"name.last"}}, false, true);
または、プロパティを含むドキュメントを更新するには:
db.foo.update({"name.additional": {$exists: true}}, {$rename:{"name.additional":"name.last"}}, false, true);
false, true
上記の方法では、次のとおりです{ upsert:false, multi:true }
。すべてのレコードmulti:true
を更新する必要があります。
または、前者の方法を使用できます。
remap = function (x) {
if (x.additional){
db.foo.update({_id:x._id}, {$set:{"name.last":x.name.additional}, $unset:{"name.additional":1}});
}
}
db.foo.find().forEach(remap);
MongoDB 3.2では、次も使用できます。
db.students.updateMany( {}, { $rename: { "oldname": "newname" } } )
これの一般的な構文は
db.collection.updateMany(filter, update, options)
https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/
upsert:true
、フィールド名が存在しない場合に、デフォルト値をフィールド名を作成しますfalse
。
"table.field" : "table.field"
構文を使用した場合、これは私にとってはうまくいきませんでした。"field" : "field"
構文を使用しただけではうまくいきました。
name.last
は違いtable.field
ます。質問を読むと、name
フィールドにオブジェクトが保持されていることがわかります。
db.foo.update({}, {$rename:{"name.0.additional":"name.0.last"}}, false, true)
か?
してみてください
db.collectionName.update({}, { $rename : { 'name.additional' : 'name.last' } }, { multi: true } )
そしてこれを読んでください:) http://docs.mongodb.org/manual/reference/operator/rename/#_S_rename
upsert
およびmulti
オプションについて何も記載されていないようです。
mongoidで同じことをする必要がある場合:
Model.all.rename(:old_field, :new_field)
更新
の構文に変更がありますmonogoid 4.0.0
:
Model.all.rename(old_field: :new_field)
Model.all.rename(old_field: :new_field)
このnodejsコードはそれを行うだけです。@ Felix Yanが以前の方法はうまく機能するように見えるので、他のスニペットにいくつかの問題があり、これが役立つことを願っています。
これにより、列「oldColumnName」の名前がテーブル「documents」の「newColumnName」に変更されます
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
//var url = 'mongodb://localhost:27017/myproject';
var url = 'mongodb://myuser:mypwd@myserver.cloud.com:portNumber/databasename';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
renameDBColumn(db, function() {
db.close();
});
});
//
// This function should be used for renaming a field for all documents
//
var renameDBColumn = function(db, callback) {
// Get the documents collection
console.log("renaming database column of table documents");
//use the former way:
remap = function (x) {
if (x.oldColumnName){
db.collection('documents').update({_id:x._id}, {$set:{"newColumnName":x.oldColumnName}, $unset:{"oldColumnName":1}});
}
}
db.collection('documents').find().forEach(remap);
console.log("db table documents remap successfully!");
}
Mongo 3.4.0を使用しています
$ rename演算子はフィールドの名前を更新し、次の形式になります。
{$rename: { <field1>: <newName1>, <field2>: <newName2>, ... } }
例えば
db.getCollection('user').update( { _id: 1 }, { $rename: { 'fname': 'FirstName', 'lname': 'LastName' } } )
新しいフィールド名は、既存のフィールド名とは異なる必要があります。埋め込みドキュメントでを指定するには、ドット表記を使用します。
この操作により、フィールドnmaeの名前がコレクション内のすべてのドキュメントの名前に変更されます。
db.getCollection('user').updateMany( {}, { $rename: { "add": "Address" } } )
db.getCollection('user').update({}, {$rename:{"name.first":"name.FirstName"}}, false, true);
上記のメソッドのfalse、trueは次のとおりです。{upsert:false、multi:true}。すべてのレコードを更新するには、multi:trueが必要です。
埋め込みドキュメントのフィールドの名前を変更する
db.getCollection('user').update( { _id: 1 }, { $rename: { "name.first": "name.fname" } } )
リンクを使用:https : //docs.mongodb.com/manual/reference/operator/update/rename/
false, true
のupdate
メソッドの$rename
は:{ upsert:false, multi:true }
です。multi:true
すべてのレコードを更新する必要があります。