2014-11-02 141 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