MongoDB - 涉及列表的更新插入 [英] MongoDB - upsert involving lists
问题描述
我是一个 MongoDB 新手,想问一下如何编写一个涉及 upsert 和 list 的更新命令.
I'm a MongoDB newbie and wanted to ask how to write an update command involving upsert and list.
基本上我想完成这样的事情:
Basically I want to accomplish something like this:
{"_id" : ObjectId("4c28f62cbf8544c60506f11d"),
"some_other_data":"goes here",
"trips": [
{"name": "2010-05-10",
"loc": [{"lat":21.321231, "lng": 16.8783234, "updated_at": "Mon May 10 2010 15:24:35"},
{"lat":21.321231, "lng": 16.8783234, "updated_at": "Mon May 10 2010 15:24:24"}]
},
{"name": "2010-05-08",
"loc": [{"lat":21.324239, "lng": 16.8735234, "updated_at": "Mon May 8 2010 11:18:05"},
{"lat":21.311234, "lng": 16.8743271, "updated_at": "Mon May 8 2010 11:17:55"},
{"lat":21.321238, "lng": 16.8782219, "updated_at": "Mon May 8 2010 11:17:45"}]
}
]}
注意:
- 您提供行程名称和当前位置
- 如果行程尚不存在,则需要创建
- trips.name 应该是唯一的,这样如果存在,则附加到位置数组
这是我结合位置运算符和 $push 编写的查询.
This is the query I wrote combining the positional operator with $push.
db.mycollection.update({"application_id": "MyTestApp",
"trips.name": "2010-05-10"},
{$push: {'trips.$.loc': {"lat":11, "lng":11} }},
true);
但这会导致这样的数据:
But this results in data like this:
> db.mycollection.find({"application_id":"MyTestApp"})
{ "_id" : ObjectId("4c29044ebf8544c60506f11f"),
"application_id" : "MyTestApp",
"trips" : { "$" : { "loc" : [ { "lat" : 11, "lng" : 11 } ] },
"name" : "2010-05-10" }
}
你可以看到
- trips"不是数组
- 它从字面上理解了$"并创建了一个用那个键(doh!)
到目前为止,我对 MongoDB 非常满意,但是编写复杂的查询肯定有一条陡峭的学习曲线.
So far I've been pretty happy with MongoDB, but there's definitely a steep learning curve with writing complicated queries.
我们将不胜感激任何反馈.
Any feedback will be appreciated.
提前致谢,艾米
推荐答案
你不能混合使用位置操作符 ("$") 和 upsert;在插入过程中,$"将被视为字段名称.您不能对新文档执行此操作,只能对现有文档执行此操作.
You can't mix the positional operator ("$") and an upsert; the "$" will be treated as a field name during the insert. You can't do this for new documents, only existing one.
我建议的结构更像这样:
I suggested a structure more like this:
{"_id" : ObjectId("4c28f62cbf8544c60506f11d"),
"some_other_data":"goes here",
"trips": {
"2010-05-10":
[{"lat":21.321231, "lng": 16.8783234, "updated_at": "Mon May 10 2010 15:24:35"},
{"lat":21.321231, "lng": 16.8783234, "updated_at": "Mon May 10 2010 15:24:24"}],
"2010-05-08":
[{"lat":21.324239, "lng": 16.8735234, "updated_at": "Mon May 8 2010 11:18:05"},
{"lat":21.311234, "lng": 16.8743271, "updated_at": "Mon May 8 2010 11:17:55"},
{"lat":21.321238, "lng": 16.8782219, "updated_at": "Mon May 8 2010 11:17:45"}]
}
}
然后你可以像这样发布更新:
Then you can issue an update like this:
db.mycollection.update({application_id: "MyTestApp", "trips.2010-05-10":{$exists:true}},
{$push: {"trips.2010-05-10": {lat:11, lng:11} }},
true);
导致这个被插入.
> db.mycollection.find()
{ "_id" : ObjectId("4c2931d17b210000000045f0"),
"application_id" : "MyTestApp",
"trips" : { "2010-05-10" : [ { "lat" : 11, "lng" : 11 } ] } }
再次运行它会给你这个:
and running it again give you this:
> db.mycollection.find()
{ "_id" : ObjectId("4c2932db7b210000000045f2"),
"application_id" : "MyTestApp",
"trips" : { "2010-05-10" :
[ { "lat" : 11, "lng" : 11 },
{ "lat" : 11, "lng" : 11 } ] } }
这篇关于MongoDB - 涉及列表的更新插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!