我有一个奇怪的问题,不确定我到底错过了什么。
我的收藏中有一个对象列表,如下所示。
{ "_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/