Nodejs, MongoDB -确定集合中的文档是否被更新或插入
Nodejs, MongoDB - Determine if Document in Collection was Updated or Inserted
我正在调用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"的值被计算,因为数据实际更改了
相关文章:
- 是否存在Javascript Liferay Service库的文档?如何处理错误情况
- 是否可以使用有角度的HTML文档进行$编译
- 将事件发送到javascript文档的react本机模块是否正确
- 用javascript从窗口获取文档,同时检查文档是否已加载
- Ember文档理解-模型是否链接到模板
- 我是否可以使用 Angular 将鼠标向上事件绑定到文档正文
- 是否有 jQuery 事件用于何时将元素添加到文档中
- 是否可以引用猫鼬(MongoDB)上的子文档
- 检查JS是否可以访问iframe的文档
- 检查文档是否已经存在,如果是则更新,否则创建新的Mongoose
- Rect.map():文档是否错误
- 检查数组是否在MongoDB文档中包含帖子ID
- 是否可以检测IE何时忽略xml文档中的空白文本节点
- 如何使用撤消管理器确定文档是否有未保存的更改
- 是否有用于在网页中嵌入HTML文档的JavaScript组件
- 确定元素是否由 JS 添加,还是原始 HTML 文档 *OR* 检测脚本何时通过 InnerHTML 更新节点
- 不确定文档引用器是否正常工作
- 是否可以使用 JQuery.Load() 来替换整个文档
- 文档 .getElementById.innerHTML 是否在所有 sencha 应用程序上完全兼容
- 是否可以像真实用户交互那样进行JavaScript操作来更改文档的activeElement