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();
});
});
所以,我怎麼只需要每個狀態的第一個文件和更新呢?
我會盡力把它翻譯到node.js的,目前我在節點漂亮的小白。 – 2014-11-03 14:22:58
目前我知道如何使用Aggregate並且工作,謝謝。 – 2014-11-20 13:02:28