1
文書を更新し、特定のルールのサブ文書にキー値を挿入するにはどうすればよいですか?特定のルールのサブ文書にUpsert KVペア
MongoDBのバージョン:3.4
使用このCLIは、どのようにこの結果を得るために
db.country.insertMany([{"_id":"us","groups":[{"group":"1"},{"group":"2"} ]},{"_id":"eu","groups":[{"group":"1"},{"group":"2"}]}, {"_id":"jp","groups":[{"group":"2"}]}])
元データ
db.country.find()
{
"_id": "us", "groups": [ { "group" : "1" }, { "group": "2" } ]
}
{
"_id": "eu", "groups": [ { "group" : "1" }, { "group" : "2" } ]
}
{
"_id": "jp", "groups": [ { "group" : "2" } ]
}
シミュレーションデータを挿入するには?私はグループ= 1
db.country.aggregate([
{'$unwind': '$groups'},
{'$match': {'groups.group': '1'}} ,
{'$project': {'group': '$groups.group', _id:0 }}
])
{ "group" : "1" }
{ "group" : "1" }
に一致するすべてのグループを選択しても、この
ようupdate + $set
を使用する方法を知っている方法を知ってい
{
"_id": "us", "groups": [ { "group" : "1", "status": "happy" }, { "group": "2" } ]
}
{
"_id": "eu", "groups": [ { "group" : "1", "status": "happy" }, { "group" : "2" } ]
}
{
"_id": "jp", "groups": [ { "group" : "2" } ]
}
:(グループ1に幸せなだけの状態を追加)
// { "_id": 1, "people": {"name": "tony" } }
db.test.update({_id: 1}, { $set: {'people.country': 'taiwan'}})
// { "_id": 1, "people": {"name": "tony" , "country": "taiwan" } }
update + $set
とaggregate function
をどのようにマージするのですか?私を助けてください。
pymongoは私にとっては問題ありません。
を見ます –