无法使用mongodb调用nodejs中未定义的方法“更新”
cannot call method ''update" of undefind in nodejs using mogodb
这是我使用 nodejs mongodb 的示例代码,
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/weather',function(err,db){
if(err) throw err;
db.collection('data').find().sort([["State",'1'],["Temprerature",'-1']]).toArray(function(err, docs){
if(err) throw err;
var res = {};
var state = "";
var month_highs=[];
for (var i=0; i<docs.length; i++){
var temperature = docs[i]['Temperature'];
//we are transitioning to a new state
if(state != docs[i]["State"]){
month_highs.push(docs[i]);
}
state = docs[i]["State"];
}
// number of responses we have received
var numCallbacks = 0;
for(i=0; i<month_highs.length; i++){
db.collection['date'].update(month_highs[i], {"$set": {'month_high':true} }, function(err,updated){
if(err) throw err;
console.log("Update " + updated + " document month high");
if(++numCallbacks == month_highs.length){
return db.close();
}
} );
}
});
});
虽然我 运行 越来越像
抛出消息;
^
类型错误:无法调用未定义的方法 'update'
如何恢复这个错误?
我的猜测是 db.collection['date']
实际上应该是 db.collection('data')
,正如您之前在代码片段中所做的那样。
此外,您在第 4 行有错别字:Temprerature
应该是 Temperature
这是我使用 nodejs mongodb 的示例代码,
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/weather',function(err,db){
if(err) throw err;
db.collection('data').find().sort([["State",'1'],["Temprerature",'-1']]).toArray(function(err, docs){
if(err) throw err;
var res = {};
var state = "";
var month_highs=[];
for (var i=0; i<docs.length; i++){
var temperature = docs[i]['Temperature'];
//we are transitioning to a new state
if(state != docs[i]["State"]){
month_highs.push(docs[i]);
}
state = docs[i]["State"];
}
// number of responses we have received
var numCallbacks = 0;
for(i=0; i<month_highs.length; i++){
db.collection['date'].update(month_highs[i], {"$set": {'month_high':true} }, function(err,updated){
if(err) throw err;
console.log("Update " + updated + " document month high");
if(++numCallbacks == month_highs.length){
return db.close();
}
} );
}
});
});
虽然我 运行 越来越像
抛出消息; ^ 类型错误:无法调用未定义的方法 'update'
如何恢复这个错误?
我的猜测是 db.collection['date']
实际上应该是 db.collection('data')
,正如您之前在代码片段中所做的那样。
此外,您在第 4 行有错别字:Temprerature
应该是 Temperature