mongodb: update


このような明るい春の光は、ちょうど良い時節を眺めて、ちょうど週末に階下の周辺をぶらぶらして、暑くて、帰ってきました.私のmongodb学習計画を振り回します.今はupdateの部分を見ているはずです.『MongoDB権威ガイド』を参照してください.
update update({},{},boolean,boolean)shellコマンドupdateは、4つのパラメータ1、クエリードキュメントを受け取ります.更新が必要なドキュメントを見つけます.2、モディファイヤ:ドキュメントに対する変更を説明します.3、upsert:ドキュメントが更新条件を満たしていない場合は、変更レコードを作成します.デフォルトfalseは4をサポートしていません.デフォルトを更新すると、条件を満たす最初のレコードのみが更新され、ロット設定trueがサポートされます.
 
1、shellコマンドで更新し、まずクエリー結果を取得し、結果を修正してから操作する
documentオブジェクトの取得
> var user = db.users.findOne()
> user
{
	"_id" : ObjectId("4f62ef1cb678c1a3326549e6"),
	"name" : "robin",
	"age" : 30,
	"friends" : 32
}

addressプロパティを追加し、操作を更新
> user.address = {"city":"chengdu","street":"asdasdfas"}
{ "city" : "chengdu", "street" : "asdasdfas" }
> db.users.update({"name":"robin"},user)
> db.users.findOne()
{
	"_id" : ObjectId("4f62ef1cb678c1a3326549e6"),
	"name" : "robin",
	"age" : 30,
	"friends" : 32,
	"address" : {
		"city" : "chengdu",
		"street" : "asdasdfas"
	}
}

その他の操作
> delete user.age
true
> user
{
	"_id" : ObjectId("4f62ef1cb678c1a3326549e6"),
	"name" : "robin",
	"friends" : 32,
	"address" : {
		"city" : "chengdu",
		"street" : "asdasdfas"
	}
}

本書では、バッチ・データの変更について言及し、クエリー時に複数のレコードが返され、updateを直接呼び出すことができないのは、update時に複数のレコードが一致し、実際に更新されるのはレコードの他の「_id」と一致しないためである.しかし、私がテストしたバージョン2.0.2では、このようなエラーは発生していないようです.updateポリシーを変更しましたか.
#  name       
> db.users.find({"name":"robin"})
{ "_id" : ObjectId("4f62ef1cb678c1a3326549e6"), "name" : "robin", "age" : 31, "friends" : 32, "address" : { "city" : "chengdu", "street" : "asdasdfas" } }
{ "_id" : ObjectId("4f62f5b8b678c1a3326549e7"), "name" : "robin", "age" : 20, "foo" : "bar" }
{ "_id" : ObjectId("4f62f5c6b678c1a3326549e8"), "name" : "robin", "age" : 30, "foo" : "bar" }
{ "_id" : ObjectId("4f62f5cdb678c1a3326549e9"), "name" : "robin", "age" : 40, "foo" : "bar" }
#     ,         
> robin = db.users.findOne({"name":"robin"})
{
	"_id" : ObjectId("4f62ef1cb678c1a3326549e6"),
	"name" : "robin",
	"age" : 31,
	"friends" : 32,
	"address" : {
		"city" : "chengdu",
		"street" : "asdasdfas"
	}
}
> robin.age++
31
> db.users.update({"name":"robin"}, robin)
#             ,           
> db.users.find()
{ "_id" : ObjectId("4f62ef1cb678c1a3326549e6"), "name" : "robin", "age" : 32, "friends" : 32, "address" : { "city" : "chengdu", "street" : "asdasdfas" } }
{ "_id" : ObjectId("4f62f5b8b678c1a3326549e7"), "name" : "robin", "age" : 20, "foo" : "bar" }
{ "_id" : ObjectId("4f62f5c6b678c1a3326549e8"), "name" : "robin", "age" : 30, "foo" : "bar" }
{ "_id" : ObjectId("4f62f5cdb678c1a3326549e9"), "name" : "robin", "age" : 40, "foo" : "bar" }
 
2、モディファイヤで修正する
前のupdateの2番目のパラメータを指し、ここでは一連のコマンドが含まれています.それぞれのネーミングについて説明します
  • $set

  • $setキーの値を指定し、存在しない場合に作成します.
    > db.users.findOne()
    { "_id" : 1, "name" : "robin", "age" : 30, "gender" : "male" }
    #  age       foo
    >  db.users.update({"name":"robin"},{"$set":{"age":40, "foo":"bar"}})
    > db.users.findOne()
    {
    	"_id" : 1,
    	"age" : 40,
    	"foo" : "bar",
    	"gender" : "male",
    	"name" : "robin"
    }
    #       
    > db.users.update({"name":"robin"},{"$set":{"age":"zzzz", "foo":["aaaa","bbbbb"]}})
    > db.users.findOne()
    {
    	"_id" : 1,
    	"age" : "zzzz",
    	"foo" : [
    		"aaaa",
    		"bbbbb"
    	],
    	"gender" : "male",
    	"name" : "robin"
    }
    #      
    > db.users.update({"name":"robin"},{"$set":{"address":{"city":"chengdu", "street":"aaaaaaaaaa"}}})
    > db.users.findOne()
    {
    	"_id" : 1,
    	"address" : {
    		"city" : "chengdu",
    		"street" : "aaaaaaaaaa"
    	},
    	"age" : "zzzz",
    	"foo" : [
    		"aaaa",
    		"bbbbb"
    	],
    	"gender" : "male",
    	"name" : "robin"
    }
    > db.users.update({"name":"robin"},{"$set":{"address.street":"bbbbbbbbbbbbbbbb"}})
    > db.users.findOne()
    {
    	"_id" : 1,
    	"address" : {
    		"city" : "chengdu",
    		"street" : "bbbbbbbbbbbbbbbb"
    	},
    	"age" : "zzzz",
    	"foo" : [
    		"aaaa",
    		"bbbbb"
    	],
    	"gender" : "male",
    	"name" : "robin"
    }
     
  • $unset

  • ドキュメントのプロパティを削除する
    > db.users.findOne()
    {
    	"_id" : 1,
    	"address" : {
    		"city" : "chengdu",
    		"street" : "bbbbbbbbbbbbbbbb"
    	},
    	"age" : "zzzz",
    	"foo" : [
    		"aaaa",
    		"bbbbb"
    	],
    	"gender" : "male",
    	"name" : "robin"
    }
    > db.users.update({"name":"robin"},{"$unset":{"foo":1,"address":-1}})
    > db.users.findOne()
    { "_id" : 1, "age" : "zzzz", "gender" : "male", "name" : "robin" }
    

    ここの属性の後ろの数字は何なのか、制限はないようです.
    #        
    > db.users.update({"name":"robin"},{"$unset":{"age":0,"address":-1}})
    > db.users.findOne()
    { "_id" : 1, "gender" : "male", "name" : "robin" }
    #       unset 
    > db.users.update({"name":"robin"},{"$unset":{"name":0,"address":-1}})
    > db.users.findOne()
    { "_id" : 1, "gender" : "male" }
    
     
  • $inc

  • これは、ドキュメントタイプが整数、長整数、または二重精度浮動小数点数である場合にのみ増加または減少します.変更されたキーが存在しない場合に作成され、指定した値になります.そうでなければ、このキーに対応する増加または減少を行います.
    > db.users.find()
    { "_id" : ObjectId("4f630319b678c1a3326549ea"), "address" : { "city" : "chengdu", "street" : "bbbbbbbbbbbbbbbb" }, "age" : "zzzz", "gender" : "male", "name" : "robin" }
    # age          
    > db.users.update({"name":"robin"},{"$inc":{"age":10}})
    Cannot apply $inc modifier to non-number
    #       
    > db.users.update({"name":"robin"},{"$set":{"age":10}})
    > db.users.find({"name":"robin"})
    { "_id" : ObjectId("4f630319b678c1a3326549ea"), "address" : { "city" : "chengdu", "street" : "bbbbbbbbbbbbbbbb" }, "age" : 10, "gender" : "male", "name" : "robin" }
    #    ,age     10,foo    ,  foo    30。     ,        
    > db.users.update({"name":"robin"},{"$inc":{"age":10, "foo":30}})
    > db.users.find({"name":"robin"})
    { "_id" : ObjectId("4f630319b678c1a3326549ea"), "address" : { "city" : "chengdu", "street" : "bbbbbbbbbbbbbbbb" }, "age" : 20, "foo" : 30, "gender" : "male", "name" : "robin" }
    
     
  • $push

  • 配列モディファイヤは、既存の配列の末尾に要素を追加し、なければ新しい配列を作成します(配列に注意して、最初に見たときはsetでも実現したいと思っていましたが、もちろんsetの値自体が配列例外です).
    > db.blog.insert({"title":"mongodb learn","owner":"duuuu","content":"content....."})
    > db.blog.findOne()
    {
    	"_id" : ObjectId("4f636a2b3c82ae09f9857f29"),
    	"title" : "mongodb learn",
    	"owner" : "duuuu",
    	"content" : "content....."
    }
    
    #      ,    comments ,      
    > db.blog.update({"owner":"duuuu"},{"$push":{"comments":{"name":"joe", "email":"mongodb@sample","content":"good"}}})
    > db.blog.findOne()
    {
    	"_id" : ObjectId("4f636a2b3c82ae09f9857f29"),
    	"comments" : [
    		{
    			"name" : "joe",
    			"email" : "mongodb@sample",
    			"content" : "good"
    		}
    	],
    	"content" : "content.....",
    	"owner" : "duuuu",
    	"title" : "mongodb learn"
    }
    
    #    
    > db.blog.update({"owner":"duuuu"},{"$push":{"comments":{"name":"ace", "email":"ace@sample","content":"nice"}}})
    > db.blog.find()
    { "_id" : ObjectId("4f636a2b3c82ae09f9857f29"), "comments" : [ 	{ 	"name" : "joe", 	"email" : "mongodb@sample", 	"content" : "good" }, 	{ 	"name" : "ace", 	"email" : "ace@sample", 	"content" : "nice" } ], "content" : "content.....", "owner" : "duuuu", "title" : "mongodb learn" }
    
    #    
    > db.users.insert({"name":"robin", "address":"chengdu", "age":30})
    > db.users.update({"name":"robin"},{"$push":{"emails":{"$each":["[email protected]","[email protected]","[email protected]"]}}})
    > db.users.find()
    { "_id" : ObjectId("4f6421def1c05dbbfa7b6ff7"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]", "[email protected]", "[email protected]" ], "name" : "robin" }
    

     
  • $addToSet

  • saveandupdateのように更新されず、あれば繰り返し追加されません.配列タイプに対してのみ操作できることに注意してください.
    > db.users.insert({"name":"robin", "address":"chengdu", "age":30})
    > db.users.find()
    { "_id" : ObjectId("4f64179ef1c05dbbfa7b6ff5"), "name" : "robin", "address" : "chengdu", "age" : 30 }
    > db.users.update({"name":"robin"},{"$addToSet":{"age":33}})
    Cannot apply $addToSet modifier to non-array
    
    #       
    > db.users.find()
    { "_id" : ObjectId("4f641b65f1c05dbbfa7b6ff6"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]" ], "name" : "robin" }
    > db.users.update({"name":"robin"},{"$push":{"emails":"[email protected]"}})
    > db.users.find()
    { "_id" : ObjectId("4f641b65f1c05dbbfa7b6ff6"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]", "[email protected]" ], "name" : "robin" }
    #             
    > db.users.update({"name":"robin"},{"$addToSet":{"emails":"[email protected]"}})
    > db.users.find()
    { "_id" : ObjectId("4f641b65f1c05dbbfa7b6ff6"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]", "[email protected]", "[email protected]" ], "name" : "robin" }
    #         
    > db.users.update({"name":"robin"},{"$addToSet":{"emails":"[email protected]"}})
    > db.users.find()
    { "_id" : ObjectId("4f641b65f1c05dbbfa7b6ff6"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]", "[email protected]", "[email protected]" ], "name" : "robin" }
    #    
    > db.users.update({"name":"robin"},{"$addToSet":{"emails":{"$each":["[email protected]","[email protected]","[email protected]"]}}})
    > db.users.find()
    { "_id" : ObjectId("4f641b65f1c05dbbfa7b6ff6"), "address" : "chengdu", "age" : 30, "emails" : [ 	"[email protected]", 	"[email protected]", 	"[email protected]", 	"[email protected]", 	"[email protected]" ], "name" : "robin" }
    

     
  • 配列から要素
  • を削除する.
    $pop{"$pop":{key:1}}配列末尾から{"$pop":{key:-1}}配列ヘッダから削除
    > db.users.find()
    { "_id" : ObjectId("4f6421def1c05dbbfa7b6ff7"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]", "[email protected]", "[email protected]" ], "name" : "robin" }
    #      
    > db.users.update({"name":"robin"},{"$pop":{"emails":1}})
    > db.users.find()
    { "_id" : ObjectId("4f6421def1c05dbbfa7b6ff7"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]", "[email protected]" ], "name" : "robin" }
    #      
    > db.users.update({"name":"robin"},{"$pop":{"emails":-1}})
    > db.users.find()
    { "_id" : ObjectId("4f6421def1c05dbbfa7b6ff7"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]" ], "name" : "robin" }

     
    $pull
    指定した要素を削除します.要素が存在しない場合は、エラーを報告しません.
    > db.users.find()
    { "_id" : ObjectId("4f64244ff1c05dbbfa7b6ff8"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]", "[email protected]", "[email protected]" ], "name" : "robin" }
    > db.users.update({"name":"robin"},{"$pull":{"emails":"[email protected]"}})
    > db.users.find()
    { "_id" : ObjectId("4f64244ff1c05dbbfa7b6ff8"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]", "[email protected]" ], "name" : "robin" }
    > db.users.update({"name":"robin"},{"$pull":{"emails":"[email protected]"}})
    > db.users.find()
    { "_id" : ObjectId("4f64244ff1c05dbbfa7b6ff8"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]", "[email protected]" ], "name" : "robin" }
    #       ,         
    > db.users.update({"name":"robin"},{"$pull":{"emails":{"$each":["[email protected]","[email protected]"]}}})
    > db.users.find()
    { "_id" : ObjectId("4f64244ff1c05dbbfa7b6ff8"), "address" : "chengdu", "age" : 30, "emails" : [ "[email protected]", "[email protected]" ], "name" : "robin" }
     
  • 配列位置決め修正
  • ここでは配列の一部のデータを操作し、下付きまたはロケータ$で操作できます.
    > db.blog.find()
    { "_id" : ObjectId("4f636a2b3c82ae09f9857f29"), "comments" : [ 	{ 	"name" : "joe", 	"email" : "mongodb@sample", 	"content" : "good" }, 	{ 	"name" : "ace", 	"email" : "ace@sample", 	"content" : "nice" }, 	{ 	"name" : "sam", 	"email" : "sam@sample", 	"content" : "ding" } ], "content" : "content.....", "owner" : "duuuu", "title" : "mongodb learn" }
    #   comments       foo  。      0  
    > db.blog.update({"owner":"duuuu"},{"$set":{"comments.0.foo":"bar"}})
    > db.blog.find()
    { "_id" : ObjectId("4f636a2b3c82ae09f9857f29"), "comments" : [ 	{ 	"content" : "good", 	"email" : "mongodb@sample", 	"foo" : "bar", 	"name" : "joe" }, 	{ 	"name" : "ace", 	"email" : "ace@sample", 	"content" : "nice" }, 	{ 	"name" : "sam", 	"email" : "sam@sample", 	"content" : "ding" } ], "content" : "content.....", "owner" : "duuuu", "title" : "mongodb learn" }
    
    #  comments   name    
    > db.blog.update({"owner":"duuuu"},{"$set":{"comments.1.name":"joe"}})
    #     ($),              
    > db.blog.update({"comments.name":"joe"},{"$set":{"comments.$.name":"jim"}})
    > db.blog.find()
    { "_id" : ObjectId("4f636a2b3c82ae09f9857f29"), "comments" : [ 	{ 	"content" : "good", 	"email" : "mongodb@sample", 	"foo" : "bar", 	"name" : "jim" }, 	{ 	"name" : "joe", 	"email" : "ace@sample", 	"content" : "nice" }, 	{ 	"name" : "sam", 	"email" : "sam@sample", 	"content" : "ding" } ], "content" : "content.....", "owner" : "duuuu", "title" : "mongodb learn" }
    
    #       ,       
    > db.blog.update({"owner":"duuuu"},{"$set":{"comments.0.name":"joe"}})
    > db.blog.update({"comments.name":"joe"},{"$set":{"comments.$.name":"jim"}},false,true)
    > db.blog.find()
    { "_id" : ObjectId("4f636a2b3c82ae09f9857f29"), "comments" : [ 	{ 	"content" : "good", 	"email" : "mongodb@sample", 	"foo" : "bar", 	"name" : "jim" }, 	{ 	"name" : "joe", 	"email" : "ace@sample", 	"content" : "nice" }, 	{ 	"name" : "sam", 	"email" : "sam@sample", 	"content" : "ding" } ], "content" : "content.....", "owner" : "duuuu", "title" : "mongodb learn" }

     
  • upsert

  • 条件を満たすレコードがない場合は、この条件と更新されたdocumentで新しいレコードが作成され、ある場合は正常に更新されます.updateの3番目のパラメータをtrueに設定して呼び出す
    > db.users.update({"name":"robinn"},{"$set":{"foo":"bar"}},true)
    > db.users.find()
    { "_id" : 1, "age" : 30, "foo" : "bar", "name" : "robin" }
    { "_id" : 2, "age" : 31, "foo" : "bar", "name" : "robin" }
    { "_id" : 3, "age" : 32, "foo" : "bar", "name" : "robin" }
    { "_id" : ObjectId("4f6459f52ba49049835032c5"), "foo" : "bar", "name" : "robinn" }
     
  • 複数のdocument
  • を更新
    updateでは、複数の一致する更新結果がある場合、デフォルトでは1つ目のみが更新され、updateの4番目のパラメータにtrueを設定すると一致する結果が同時に更新されます.
    > db.users.find({"name":"robin"})
    { "_id" : 1, "name" : "robin", "age" : 30 }
    { "_id" : 2, "name" : "robin", "age" : 31 }
    { "_id" : 3, "name" : "robin", "age" : 32 }
    #               
    > db.users.update({"name":"robin"},{"$set":{"foo":"bar"}})
    > db.users.find({"name":"robin"})
    { "_id" : 1, "age" : 30, "foo" : "bar", "name" : "robin" }
    { "_id" : 2, "name" : "robin", "age" : 31 }
    { "_id" : 3, "name" : "robin", "age" : 32 }
    #        true,              
    > db.users.update({"name":"robin"},{"$set":{"foo":"bar"}},false,true)
    > db.users.find({"name":"robin"})
    { "_id" : 1, "age" : 30, "foo" : "bar", "name" : "robin" }
    { "_id" : 2, "age" : 31, "foo" : "bar", "name" : "robin" }
    { "_id" : 3, "age" : 32, "foo" : "bar", "name" : "robin" }

     
    モディファイヤの速度については、documentのサイズを変更する必要がない場合は、$incがdocumentの値を変更するだけでは急速にパフォーマンスが低下し、$setがdocumentに属性を追加するとパフォーマンスが低下し、値を変更するだけでは急速になります.配列モディファイヤは一般的にdocumentのサイズを変更するため、パフォーマンスが低下します.