mongodb update更改对象字段的顺序

时间:2014-04-07 05:21:19

标签: 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 }

3 个答案:

答案 0 :(得分:3)

您也可以使用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

的链接

答案 1 :(得分:1)

基本上您遇到的问题归结为:https://jira.mongodb.org/browse/SERVER-2592已修复为2.6。

文档字段顺序应从2.6开始保留。目前你必须做你周围的工作。

答案 2 :(得分:0)

我的查询中的以下更改修复了该问题。现在,在更新之后,字段/属性的顺序是 NOT 搞砸了。

这段代码实际上是按州分组对象,对于每个州,我按降序排序记录,并将标志“month_high”添加到温度最高的对象。

db.collection('data').aggregate([{$group:{"_id":"$State"}}],function(err,results){
    for(var i=0;i<results.length;i++){

    var query = {'State':results[i]._id};
    var cursor = db.collection('data').find(query);
        cursor.limit(1);
        cursor.sort('Temperature',-1);

        cursor.each(function(err, doc) {
            if(err) throw err;
            if(doc == null) {
                return;
            }

 // I just had to change the following query and it worked for me
 // rest of the code remains same.
                  var updateQuery = {};
               updateQuery['_id'] = doc['_id'];
        doc['month_high'] = true;

   db.collection('data').update(updateQuery,doc, function(err, updated) {
                    if(err) throw err;

                    console.dir("Successfully updated " + updated + " document!");

                    return;
                });             
            //console.dir(doc._id);
        });
  }
});

仍然不确定我的查询是什么时候跟着它为什么不起作用

var updateQuery = {'_id':doc['_id']};
var operator = {$set:{'month_high' : true}};
db.collection('data').update(updateQuery,operator,callback)