Javascript Nodejs,MongoDB-确定集合中的文档是否已更新或插入

Javascript Nodejs,MongoDB-确定集合中的文档是否已更新或插入,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,如果给定的_id不存在,我将使用{upsert:true}调用mongoDB update函数来插入新文档。我想确定文档是否已插入或更新。很像我在使用java时发现的这个问题,但只使用Nodejs 这是我的DB电话 app.post('/mongoSubmit', function(req, res) { console.log("This is the req.body" + JSON.stringify(req.body, null, 4)); var updateCustomer =

如果给定的_id不存在,我将使用{upsert:true}调用mongoDB update函数来插入新文档。我想确定文档是否已插入或更新。很像我在使用java时发现的这个问题,但只使用Nodejs

这是我的DB电话

app.post('/mongoSubmit', function(req, res) {

console.log("This is the req.body" + JSON.stringify(req.body, null, 4));

var updateCustomer = function(db, callback){

    db.collection('customers1').update(
        {_id:req.body.email},

            { first: req.body.firstName,
              last: req.body.lastName,
              phone: req.body.phone,
              email: req.body.email,
              subjectIndex: req.body.subject,
              messageIndex: req.body.message
            },

        { upsert: true},
         function(err, result){
            if(err){console.log("database error" + err)}
            callback(result);
        }
    );
}

MongoClient.connect(url, function(err, db){
    updateCustomer(db, function(result){

    console.log("these are the results" + JSON.stringify(result, null, 4));

/*
** Return Either
*
these are the results{
    "ok": 1,
    "nModified": 0,
    "n": 1,
    "upserted": [
        {
            "index": 0,
            "_id": "sjr6asdfsadfsadf28@gmail.com"
        }
    ]
}

/*
*
* or

    these are the results{
    "ok": 1,
    "nModified": 1,
    "n": 1
}

//BUT  *************** Problem using this Value *********************
    console.log("this is the value of Modified" + result.nModified);

/*
** Returns undefined
*/


            if(result.nModified == 1){
                console.log("Updated document");
            }
            else{
                console.log("Inserted document");
            }

        db.close(); 
        res.render('applications', {
            title:"Title"
        });
    });
});

});
我也试过做测试

    if(result.hasOwnProperty('upserted'){
        //log an Insert
    if(result.upserted == true {
        //log an Insert
    if(result.nModified == 1){
        // log an update
    if(result.nModified == true){
        //log an update
还将upserted作为一个参数添加到回调中,我从另一个论坛找到了这个回调

function(err, result, upserted){
   //callback function
   //upserted was undefined
})
我的结果令人困惑。我如何用属性值记录对象,但当我尝试记录特定属性时,它却没有定义

有人能解释为什么会在javascript中发生这种情况吗

建议另一种解决方案来确定集合中的文档是否已更新或插入




谢谢

结果是一个包含其自身属性“result”的结构,该属性具有子属性。因此,您需要在正确的级别进行检查:

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


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

  db.collection('uptest').update(
    { "a": 1 },
    { "$set": { "b": 2 } },
    { "upsert": true },
    function(err,result) {
      if (err) throw err;

      if (result.result.hasOwnProperty('upserted') ) {
        console.log( JSON.stringify( result.result.upserted,  undefined, 2 ) );
      }

      console.log( "matched: %d, modified: %d",
          result.result.n,
          result.result.nModified
      );
    }
  );

});
第一次运行时,您将得到如下“upserted”的“array”:

[
  {
    "index": 0,
    "_id": "55a4c3cfbe78f212535e2f6a"
  }
]
matched: 1, modified: 0
在具有相同值的第二次运行时,不会添加或修改任何内容:

matched: 1, modified: 0
更改“b”和“修改”的值是从数据实际更改开始计算的