MongoDB – 大文字に更新する
ドキュメントであり、すべての ‘source’値を大文字に更新したいとします。
whois.json
{ "__id" : NumberLong(1), "country" : "au", "source" : "apnic" } { "__id" : NumberLong(2), "country" : "cn", "source" : "apnic" } { "__id" : NumberLong(3), "country" : "us", "source" : "arin" }
解決策
準備関数があるかどうかは不明ですが、値を大文字に更新するスクリプトを書くことができます:
db.whois.find({ "source": { "$exists": true } }).forEach(function(doc) { db.whois.update( { "__id": doc.__id }, { "$set": { "source": doc.source.toUpperCase() } } ); });
出力
whois.json
{ "__id" : NumberLong(1), "country" : "au", "source" : "APNIC", } { "__id" : NumberLong(2), "country" : "cn", "source" : "APNIC", } { "__id" : NumberLong(3), "country" : "us", "source" : "ARIN", }
完了しました。