mongodb - mongodb更新更改对象字段的顺序

标签 mongodb

我有一个奇怪的问题,不确定我到底错过了什么。

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

{ "_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”标志添加到包含该州最高温度的对象的实际代码的一部分。

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 }

最佳答案

您可以使用 MongoDB projection (推荐方式)

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
});

为您提供预期的输出,即

{
   "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。


发生的原因:

MongoDB 根据一定的填充因子为新文档分配空间。如果您的更新增加了文档的大小,超出了最初分配的大小,文档将被移动到集合的末尾。相同的概念适用于文档中的字段。

如果您真的有兴趣并想深入了解这里是更多的链接 information

关于mongodb - mongodb更新更改对象字段的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22904167/

相关文章:

node.js - 使用 MongoDB 在 Node.js 中扩展,我应该什么时候一次查询所有 ID,还是并行查询每个 ID?

java - 基于嵌套文档的可用部分数据进行查询(在morphia、mongoDB中)

javascript - 如何在 MongoDB-Shell 中正确迭代搜索结果?

node.js - 单独文件上的 Mongoose 子文档,如何嵌入它们

python - pymongo 插入 vs pymysql 插入

MongoDB 聚合 : Add each array value to each object inside array from another field. 合并/组合来自不同字段的两个数组

javascript - Mongoose 不创建 TTL 索引

javascript - 无法读取未定义的 mongoDB NodeJS 的 'insert' 属性

mongodb - 无法使用 golang 将结构保存到 mongodb 中(仅创建空记录)

java - 当我在 mongodb 中手动设置 _id 时,@CreatedDate 不起作用