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

Nodejs, MongoDB - Determine if Document in Collection was Updated or Inserted

本文关键字:是否 文档 更新 插入 MongoDB 集合 Nodejs      更新时间:2023-09-26

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

如何检查文档是否在MongoDB中被更新或插入

这是我的电话。

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是一个包含自己的属性"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"的"数组",如下所示:

[
  {
    "index": 0,
    "_id": "55a4c3cfbe78f212535e2f6a"
  }
]
matched: 1, modified: 0

使用相同的值第二次运行时,没有添加或修改任何内容:

matched: 1, modified: 0

更改"b"的值和"modified"的值被计算,因为数据实际更改了