如何使 mongodb 'upsert' 插入新文档以及更新现有文档
how to make mongodb 'upsert' to insert new documents as well as update existing ones
在我的 MEAN 应用程序中,我有一种情况,我正在逐行读取一些 csv 数据,并使用 'upsert' 更新现有记录,并插入这些记录,这确实不匹配 'CSCOpportunityID' 字段值:
if(metaFields.subjectCategory === 'SalesforceData'){
//Here we will use the upsert logic to either insert new, or update existing records
for (var i = 0; i < result.length; i++) {
//console.log(result[i]);
var inserted = 0;
for (var i = 0; i < result.length; i++) {
var dataRecord = result[i];
dataRecord.OriginalDocumentName = fileName;
dataRecord.DocumentAuthor = metaFields.documentAuthor;
dataRecord.TabName = tabName;
dataRecord.SubjectCategory = metaFields.subjectCategory;
dataRecord.Subject = metaFields.subject;
dataRecord.DateDocumentProduced = metaFields.dateDocumentProduced;
dataRecord.DateDocumentReceived = metaFields.dateDocumentReceived;
dataRecord.DocumentSubmitter = metaFields.documentSubmitter;
dataRecord.DocumentReviewer = metaFields.documentReviewer;
dataRecord.OriginalSource = metadataFields.originalSource,
dataRecord.DataVersion = metadataFields.dataVersion,
dataRecord.DataFields = newCsvLines[0];
collection.update(
{
CSCOpportunityId: dataRecord.CSCOpportunityID
},
{
$set: {
OriginalDocumentName: dataRecord.OriginalDocumentName,
DocumentAuthor: dataRecord.DocumentAuthor,
TabName: dataRecord.TabName,
SubjectCategory: dataRecord.SubjectCategory,
Subject: dataRecord.Subject,
DateDocumentProduced: dataRecord.DateDocumentProduced,
DateDocumentReceived: dataRecord.DateDocumentReceived,
DocumentSubmitter: dataRecord.DocumentSubmitter,
DocumentReviewer: dataRecord.DocumentReviewer,
OriginalSource: dataRecord.OriginalSource,
DataVersion: dataRecord.DataVersion,
DataFields: dataRecord.DataFields,
CSCOpportunityID : dataRecord.CSCOpportunityID,
OpportunityName: dataRecord.OpportunityName ,
AccountName : dataRecord.AccountName,
OpportunityOwner : dataRecord.OpportunityOwner,
Stage : dataRecord.Stage,
Industry : dataRecord.Industry,
ACV : dataRecord.ACV,
RevenueStartDate : dataRecord.RevenueStartDate,
RevenueTerm : dataRecord.RevenueTerm,
ProbabilityPct : dataRecord.ProbabilityPct,
DealRegion : dataRecord.DealRegion
}
},
{upsert: true},
{multi:true}
//if (++inserted == result.length) {
// console.log("finished");
// //db.close();
//}
);
}
}
}
更新部分有效,但插入部分无效,因为我不知道如何插入,当 'CSCOpportunityID' 的值找不到匹配项时。
处理这个问题的正确方法是什么?
update
方法将第 4 个参数作为回调函数。由于您提供了一个对象 {multi: true}
,它不会进行任何数据库调用,只会 return 一个承诺。正确的语法是:
collection.update(query, fieldsToSet, {upsert: true, multi: true}, function(err,doc){
})
在我的 MEAN 应用程序中,我有一种情况,我正在逐行读取一些 csv 数据,并使用 'upsert' 更新现有记录,并插入这些记录,这确实不匹配 'CSCOpportunityID' 字段值:
if(metaFields.subjectCategory === 'SalesforceData'){
//Here we will use the upsert logic to either insert new, or update existing records
for (var i = 0; i < result.length; i++) {
//console.log(result[i]);
var inserted = 0;
for (var i = 0; i < result.length; i++) {
var dataRecord = result[i];
dataRecord.OriginalDocumentName = fileName;
dataRecord.DocumentAuthor = metaFields.documentAuthor;
dataRecord.TabName = tabName;
dataRecord.SubjectCategory = metaFields.subjectCategory;
dataRecord.Subject = metaFields.subject;
dataRecord.DateDocumentProduced = metaFields.dateDocumentProduced;
dataRecord.DateDocumentReceived = metaFields.dateDocumentReceived;
dataRecord.DocumentSubmitter = metaFields.documentSubmitter;
dataRecord.DocumentReviewer = metaFields.documentReviewer;
dataRecord.OriginalSource = metadataFields.originalSource,
dataRecord.DataVersion = metadataFields.dataVersion,
dataRecord.DataFields = newCsvLines[0];
collection.update(
{
CSCOpportunityId: dataRecord.CSCOpportunityID
},
{
$set: {
OriginalDocumentName: dataRecord.OriginalDocumentName,
DocumentAuthor: dataRecord.DocumentAuthor,
TabName: dataRecord.TabName,
SubjectCategory: dataRecord.SubjectCategory,
Subject: dataRecord.Subject,
DateDocumentProduced: dataRecord.DateDocumentProduced,
DateDocumentReceived: dataRecord.DateDocumentReceived,
DocumentSubmitter: dataRecord.DocumentSubmitter,
DocumentReviewer: dataRecord.DocumentReviewer,
OriginalSource: dataRecord.OriginalSource,
DataVersion: dataRecord.DataVersion,
DataFields: dataRecord.DataFields,
CSCOpportunityID : dataRecord.CSCOpportunityID,
OpportunityName: dataRecord.OpportunityName ,
AccountName : dataRecord.AccountName,
OpportunityOwner : dataRecord.OpportunityOwner,
Stage : dataRecord.Stage,
Industry : dataRecord.Industry,
ACV : dataRecord.ACV,
RevenueStartDate : dataRecord.RevenueStartDate,
RevenueTerm : dataRecord.RevenueTerm,
ProbabilityPct : dataRecord.ProbabilityPct,
DealRegion : dataRecord.DealRegion
}
},
{upsert: true},
{multi:true}
//if (++inserted == result.length) {
// console.log("finished");
// //db.close();
//}
);
}
}
}
更新部分有效,但插入部分无效,因为我不知道如何插入,当 'CSCOpportunityID' 的值找不到匹配项时。
处理这个问题的正确方法是什么?
update
方法将第 4 个参数作为回调函数。由于您提供了一个对象 {multi: true}
,它不会进行任何数据库调用,只会 return 一个承诺。正确的语法是:
collection.update(query, fieldsToSet, {upsert: true, multi: true}, function(err,doc){
})