2014-11-02 79 views
1

我工作的MongoDB,我有一个收集这种结构:选择顶层的每一个国家

{ 
"_id" : 1, 
"State": "Vermont", 
"Temperature" : 20, 
"Day" : 1 
} 
{ 
"_id" : 2, 
"State": "Vermont", 
"Temperature" : 50, 
"Day" : 2 
} 
{ 
"_id" : 1, 
"State": "Vermont", 
"Temperature" : 40, 
"Day" : 2 
} 
{ 
"_id" : 4, 
"State": "Texas", 
"Temperature" : 20, 
"Day" : 1 
} 
{ 
"_id" : 5, 
"State": "Texas", 
"Temperature" : 50, 
"Day" : 2 
} 
{ 
"_id" : 6, 
"State": "Texas", 
"Temperature" : 40, 
"Day" : 2 
} 

而且我需要Node.js的筛选与状态高温的文件,并更新只有与最高温度将密钥文件:值"Max_Value": "true",结果将是:

{ 
"_id" : 1, 
"State": "Vermont", 
"Temperature" : 20, 
"Day" : 1 
} 
{ 
"_id" : 2, 
"State": "Vermont", 
"Temperature" : 50, 
"Day" : 2, 
"Max_Value": "true" 
} 
{ 
"_id" : 1, 
"State": "Vermont", 
"Temperature" : 40, 
"Day" : 2 
} 
{ 
"_id" : 4, 
"State": "Texas", 
"Temperature" : 20, 
"Day" : 1 
} 
{ 
"_id" : 5, 
"State": "Texas", 
"Temperature" : 50, 
"Day" : 2, 
"Max_Value": "true" 
} 
{ 
"_id" : 6, 
"State": "Texas", 
"Temperature" : 40, 
"Day" : 2 
} 

我的节点代码:

var MongoClient = require('mongodb').MongoClient; 

MongoClient.connect('mongodb://localhost:27017/weather', function(err, db) { 
    if(err) throw err; 


    var options = { 
     "sort": [['State','1'], ['Temperature','-1']] 
    } 

    db.collection('data').find({}, options).toArray(function(err, doc) { 
     if(err) throw err; 

     //Here I sorted the collection, but how I take only the first documents for each state? 



     return db.close(); 
    }); 
}); 

所以,我怎么只需要每个状态的第一个文件和更新呢?

回答

0

最后我做了Node.js的一些帮助上蒙戈大学讨论:

var MongoClient = require('mongodb').MongoClient; 

MongoClient.connect('mongodb://localhost:27017/weather', function(err, db) { 
    if(err){ 
    console.log(err.message); 
    return db.close(); 
} 

//filter the data 
db.collection("data").find({},{},{ 'sort':[[ 'State',1 ], ['Temperature',-1]]}).toArray(function(err,docs){ 
    if(err) throw err ; 

    var new_state = ""; 

    //do the forEach() 
    docs.forEach(function(doc){ 

     //if State is not equeal that I have saved, so It's the first state with the maximun temperature 
     if(new_state !== doc.State){ 
      new_state = doc.State; 
      var query = { '_id' : doc._id}; 
      var operator = { '$set' : { 'month_high' : true}}; 
      var options = { 'multi' : true}; 

      //update the value 
      db.collection("data").update(query,operator,options,function(err,data){ 
       if(err){ 
        console.log(err.message); 
        //return db.close(); removed becouse the conection canceling 
       } 
       console.log("Success"); 
       return db.close(); 

      }); 
     } 

    }) 

}); 

}); 
1

看来你还没有并发麻烦。所以我尝试按照需求获取所有id并逐一更新它们。

db.collection.aggregate([ { 
    $sort : { 
     "$Temperature" : -1 
    } 
}, { 
    $group : { 
     _id : "$State", 
     myId : { 
      $first : "$_id" 
     } 
    } 
} ]).forEach(function(doc) { 
    db.collection.update({ 
     _id : doc.myId 
    }, { 
     $set : { 
      "Max_Value" : "true" 
     } 
    }); 
}); 

啊,你必须根据你的语言环境来翻译它。 :)

+0

我会尽力把它翻译到node.js的,目前我在节点漂亮的小白。 – 2014-11-03 14:22:58

+0

目前我知道如何使用Aggregate并且工作,谢谢。 – 2014-11-20 13:02:28