Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在node.js中设置mongoDB游标?_Node.js_Mongodb_Set - Fatal编程技术网

如何在node.js中设置mongoDB游标?

如何在node.js中设置mongoDB游标?,node.js,mongodb,set,Node.js,Mongodb,Set,我有一个mongoDB集合,我想使用$set在随机位置添加一个字段,至少我很确定它是一个$set。如果我错了,请纠正我。 我包括代码。在中间,我包括了我想做的事情: var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://localhost:27017/weather', function(err, db) { // Also, what is the best way to h

我有一个mongoDB集合,我想使用
$set
在随机位置添加一个字段,至少我很确定它是一个
$set
。如果我错了,请纠正我。 我包括代码。在中间,我包括了我想做的事情:

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

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

    // Also, what is the best way to handle err in this code?
    //if(err) throw err;

    var query = { };
    var sortorder = {"State":1, "Temperature":-1}

    var xx = null;

    //var cursor = db.collection('data').find();
    var cursor = db.collection('data').find().sort(sortorder);

    cursor.each(function(err, doc) {
        //if(err) throw err;

        if(doc == null) {
            return db.close();
        }


        if (xx == doc.State){


        }else{

                console.dir("New state -----------" + doc.State);
                console.dir("Tempurature -----------" + doc.Temperature);

                // !!!!!!!!!!!!!!!!!!!!!!!!!!   this is the problem area.
                //--- this is the part I am trying to figure out...
                update_routine = $set:{"month_high---test001":true};
                doc.update =  update_routine;
                //  How do I do a $set operation on a mongoDB cursor.  which I have here.
                xx = doc.State;
                // add the field
                //doc.update();

        }

        if(doc == null) {
            return db.close();
        }

    //app.error(function(err, req, res, next){
    //  console.error(err);
    //  res.send('Fail Whale, yo.');
    //});

        //console.dir(doc.State + " is a state!");
    });
});

~~

您的代码看起来有点混乱,但下面是您可以做的。 还可以查看$set的mongodb文档:


您的代码看起来有点混乱,但下面是您可以做的。 还可以查看$set的mongodb文档:


您的代码看起来有点混乱,但下面是您可以做的。 还可以查看$set的mongodb文档:


您的代码看起来有点混乱,但下面是您可以做的。 还可以查看$set的mongodb文档:

var cursor = db.collection('data').find().sort(sortorder);

cursor.each(function(err, doc) {
    if(err) throw err;

    if(doc == null) {
        return db.close();
    }

    // until here you code makes sense, you have a cursor, 
    // you checked for errors and have the current document

   // now you want to update a record, you can do it like this:

   var myquery = {};
   myquery['_id'] = doc['_id'];

   // you were missing the surrounding {}
   var myupdate = { $set: { field1: "whatever value", field2: 500 } }; 
   // obviously you want to replace field1 and field2 with your actual field names

   // instead of creating a new update object and using $set
   // you could also just modify the 'doc' variable and pass it again 
   // in the update function below instead of myupdate


   db.collection('data').update(myquery, myupdate, function (err, updatedDoc) {
      if (err) throw err;
      console.log("successfully updated document", updatedDoc);
    });

});