【发布时间】:2015-06-06 14:52:17
【问题描述】:
运行下面的代码时,在检查文档状态是否已更改时出现意外的标识符错误。我已经用谷歌搜索死了。试图在 Mongodb、here 和其他杂项资源中找到文档,但没有任何运气。我花了一整天的时间,现在感觉很愚蠢。
衷心感谢所有帮助。
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
if (err) throw err;
var weather = db.collection('weather');
var filter = {};
var projection ={'State':1, 'Temperature':1, _id:true};
var options = { 'skip' : 0,
'limit' : 20000 ,
'sort' : [['State', 1], ['Temperature', -1]] };
var cursor = weather.find(filter, projection, options);
var month_high = false;
var prevState = '';
var curID = '';
var operation = {'_id':curID,'$set':{'month_high':true}};
// get first document
cursor.first();
// initialize vars
prevState = doc.State;
// cycle through documents
cursor.each(function(err, doc) {
if(err) throw err;
if(doc == null) {
return db.close();
}
//Save current document Id
curID = doc._id;
// Check if State has changgd
if prevState != doc.state{
//If State has changed update the document
weather.update(operation,function(err,doc));
console.dir(doc); //we expect four documents to ouput.
}
// save current doc.state as previous state
prevState = doc.State;
;})
});
});
【问题讨论】:
-
IMO 这通常是由于缺少属性之间的逗号或代码中的大括号而发生的。
标签: javascript node.js mongodb