mongo db update更改对象字段的顺序 [英] mongo db update changing the order of object fields

查看:242
本文介绍了mongo db update更改对象字段的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个比较棘手的问题,不确定我到底想念什么.

I am having a wiered issue, not sure what exactly i am missing.

我的收藏夹中有一个对象列表,如下所示.

I have a list of objects into my collection as follows..

{ "_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驱动程序的Node客户端中,我正在执行更新查询,如下所示.基本上,这是实际代码的一部分,在该代码中,我试图向包含该州最高温度的对象添加"month_high"标志.

from the Node client using mongo db driver, i am executing an update query as follows. Basically it is part of the actual code where i am trying to add "month_high" flag to the object containing the highest temprature for the state.

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 }

推荐答案

您可以使用

You can use MongoDB projection as well. (recommended way)

db.collection.find(<条件>,<投影>);

db.collection.find( < criteria >, < projection >);

以下是示例:

db.collection.find({},{
   "Airport":1,
   "Day":1,
   "Humidity":1,
   "Sea Level Pressure":1,
   "State":1,
   "Station Pressure":1,
   "Temperature":1,
   "Time":1,
   "Wind Direction":1,
   "Wind Speed":1,
   "_id":1,
   "month_high":1
});

为您提供预期的输出,即

Gives you the output as you expected i.e.

{
   "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
}, ...


另一种方法是,一旦将文档放入Node.js中,就可以对JSON中的字段重新排序.

示例:

var json = {"name": "David", "age" : 78, "NoOfVisits" : 4   };
console.log(json);
//outputs - Object {name: "David", age: 78, NoOfVisits: 4}
//change order to NoOfVisits,age,name

var k = JSON.parse(JSON.stringify( json, ["NoOfVisits","age","name"] , 4));
console.log(k);
//outputs - Object {NoOfVisits: 4, age: 78, name: "David"} 

将所需的键顺序放入数组中并提供给函数.然后将结果解析回JSON.


发生原因

Put the key order you want in an array and supply to the function. Then parse the result back to JSON.


Reason Why It Happens:

MongoDB根据特定的填充因子为新文档分配空间.如果您的更新使文档的大小超出了最初分配的大小,则该文档将被移至集合的末尾.相同的概念适用于文档中的字段.

MongoDB allocates space for a new document based on a certain padding factor. If your update increases the size of the document beyond the size originally allocated the document will be moved to the end of the collection. The same concept applies to fields in a document.

如果您真的有兴趣并想深入了解,请点击此处,以获取更多的链接信息

If you are really interested and want to dig into it here is the link for more information

这篇关于mongo db update更改对象字段的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆