【发布时间】:2014-04-07 15:36:44
【问题描述】:
我遇到了一个奇怪的问题,不确定我到底错过了什么。
我的收藏中有一个对象列表,如下所示..
{ "_id" : ObjectId("5340eff554f98e32c5990b4f"), "Day" : 8, "Time" : 1553, "State" : "Florida", "Airport" : "ORL", "Temperature" : 82, "Humidity" : 55, "Wind Speed" : 5, "Wind Direction" : 170, "Station Pressure" : 29.97, "Sea Level Pressure" : 196 }
{ "_id" : ObjectId("5340eff554f98e32c5990b5f"), "Day" : 9, "Time" : 1253, "State" : "Florida", "Airport" : "ORL", "Temperature" : 82, "Humidity" : 60, "Wind Speed" : 13, "Wind Direction" : 190, "Station Pressure" : 29.91, "Sea Level Pressure" : 175 }
{ "_id" : ObjectId("5340eff554f98e32c5990b60"), "Day" : 9, "Time" : 1353, "State" : "Florida", "Airport" : "ORL", "Temperature" : 82, "Humidity" : 58, "Wind Speed" : 11, "Wind Direction" : 190, "Station Pressure" : 29.88, "Sea Level Pressure" : 166 }
{ "_id" : ObjectId("5340eff554f98e32c5990b4c"), "Day" : 8, "Time" : 1253, "State" : "Florida", "Airport" : "ORL", "Temperature" : 81, "Humidity" : 54, "Wind Speed" : 4, "Wind Direction" : 180, "Station Pressure" : 30.02, "Sea Level Pressure" : 214 }
从使用 mongo db 驱动程序的节点客户端,我正在执行更新查询,如下所示。基本上它是我试图将“month_high”标志添加到包含该州最高温度的对象的实际代码的一部分。
var updateQuery = {'_id':doc['_id']};
var operator = {$set:{'month_high' : true}};
db.collection('temps').update(updateQuery,operator,callback)
问题是在更新之后,它更新了正确的对象,但它的字段重新排序如下(注意第一个对象)
{ "Airport" : "ORL", "Day" : 8, "Humidity" : 55, "Sea Level Pressure" : 196, "State" : "Florida", "Station Pressure" : 29.97, "Temperature" : 82, "Time" : 1553, "Wind Direction" : 170, "Wind Speed" : 5, "_id" : ObjectId("5340eff554f98e32c5990b4f"), "month_high" : true }
{ "_id" : ObjectId("5340eff554f98e32c5990b5f"), "Day" : 9, "Time" : 1253, "State" : "Florida", "Airport" : "ORL", "Temperature" : 82, "Humidity" : 60, "Wind Speed" : 13, "Wind Direction" : 190, "Station Pressure" : 29.91, "Sea Level Pressure" : 175 }
{ "_id" : ObjectId("5340eff554f98e32c5990b60"), "Day" : 9, "Time" : 1353, "State" : "Florida", "Airport" : "ORL", "Temperature" : 82, "Humidity" : 58, "Wind Speed" : 11, "Wind Direction" : 190, "Station Pressure" : 29.88, "Sea Level Pressure" : 166 }
{ "_id" : ObjectId("5340eff554f98e32c5990b4c"), "Day" : 8, "Time" : 1253, "State" : "Florida", "Airport" : "ORL", "Temperature" : 81, "Humidity" : 54, "Wind Speed" : 4, "Wind Direction" : 180, "Station Pressure" : 30.02, "Sea Level Pressure" : 214 }
【问题讨论】:
-
你确定是那个代码做的吗?在我看来,好像其他一些代码已经在结果更新中对所有键进行了排序。您的新字段按预期结束,但我认为其他内容首先修改了此记录。尝试重新制作。
-
好的,我将我的查询更改如下,但现在更新对象的字段顺序没有搞砸。我们真的必须将整个 doc 对象传递给 update 方法吗? var updateQuery = {}; updateQuery['_id'] = 文档['_id']; doc['month_high'] = true; db.collection('temps').update(updateQuery,doc, callback) {
-
集合是无序集合。仅当您使用运算符sort () 时,您才能期望文档按某种顺序排列。否则,文档的顺序取决于实现。
-
这是问题:jira.mongodb.org/browse/SERVER-2592,它已在 2.6 中修复
标签: mongodb