问题描述
我是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应该是唯一的,以便如果存在,则将其附加到位置数组
- You supply a trip name and thecurrent location
- If the trip does not exist already, itneeds to be created
- trips.name should be unique so thatif it exists, you append to thelocation array
这是我写的结合$ 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" is not an array
- it took "$" literally and created akey with that (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-涉及列表的upsert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!