批量更新 - BulkWriteResult.nMatched和BulkWriteResult.nModified返回0

时间:2016-03-29 06:01:48

标签: node.js mongodb mongoose

我的主服务器.js代码,我启动我的服务器并连接到mongoDb服务器

router.put("/addstud/:route", function(req,res,next) {

var custId = req.user.id;
var studList = req.body;
var route = req.params.route;

db.connection.on('open', function() {
    // Never goes inside this As i have to bulk Update Operation
    console.log('Mongoose opened.');
});

db.connection.once('open',function(err,conn) {
    // Nor this 
}

});

var bulk = Student.collection.initializeUnorderedBulkOp();

// this simple code is not working
bulk.find({_id:'56f940172681fda5c6536a4d'})
 .updateOne( {name:'test'});

bulk.execute(function(err, result) {
    if(err) {
        console.log("ERROR WHILE INSERTING "+err);
    } else {
    // This line is getting printed means no error but nothing changed in DB
        console.log("RESULT "+result.mMatched); // 0 ! HOW ????????
    }
});

现在在我的控制器/ customer / router.js控制器文件中,

ImageView drawingImageView;
drawingImageView = (ImageView)findViewById(R.id.imageViewLine);
    Bitmap bitmap = Bitmap.createBitmap((int)getWindowManager().getDefaultDisplay().getWidth(), (int)getWindowManager().getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawingImageView.setImageBitmap(bitmap);

    //Line
    Paint paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setStrokeWidth(10);
    //paint.setAlpha(0x80);


    float startX3=0.0f;
    float startY3=0.0f;
    float stopX3=451.0f;
    float stopY3=553.0f;


    canvas.drawLine(startX3, startY3, stopX3, stopY3, paint);

但是在同一个类中,我可以使用Mongoose Model轻松检索并向Db添加数据。

1 个答案:

答案 0 :(得分:1)

您必须转换对象中的ID。像字符串一样使用它是行不通的。这样做:

const restful = require('node-restful');
const mongoose = restful.mongoose;

let id = new mongoose.Types.ObjectId('56f940172681fda5c6536a4d');
let obj = {"_id": id};

// And then

bulk.find({_id:'56f940172681fda5c6536a4d'}).updateOne( {name:'test'});