我陷入了这样的境地。我有以下文件。
{
"_id" : ObjectId("5ceba7c419b48423e452972a"),
"userId" : ObjectId("5ceba7c419b48423e4529727"),
"providers" : [
"1689736266",
"1598763690",
"1528069614",
"1831364272",
"1548463045",
"1245301159",
"1386616399",
"1790775971",
"1629462130",
"1992169783"
],
"countByType" : {
"doctors" : 6,
"labs" : 0,
"hospitals" : 0,
"imagingCenters" : 0,
"other" : 4
}
}
最初,我从弹性搜索中获取 providerArray,添加到 providers 数组并使用以下查询增加计数。
let updateProviderId = await userProviderCollection.update(
regUserId, {
$set: {
providers: combinedProviderArray,
countByType: providerCountType
}
});
其中combinedProviderArray 是提供者的组合数组。ProviderId 在生成新报告时创建。如果报告已经存在,则上述条件将起作用。但是当新报告出现时,我需要检查提供者数组并增加计数,如果不在数组中,否则什么都不做。我怎样才能做到这一点。
当带有 providerId 的报告已在数组中时,请参阅下面的代码。
// Searching for Providers in Elastic Search
let searchForProviders = await esHelper.getProvidersById(combinedProviderArray, true);
searchForProviders.forEach((getCategory) => {
let prCategory = getCategory.category;
prCategory.forEach((cat) => {
// Combining categories
categoryArray = categoryArray.concat(cat);
});
});
let counts = {
"doctor": 0,
"laboratory": 0,
"hospital": 0,
"imagingcenter": 0,
"other": 0
};
let x;
categoryArray.forEach(function(x) {
counts[x] = (counts[x] || 0)+1;
});
let providerCountType = {
"doctors": counts.doctor,
"labs": counts.laboratory,
"hospitals": counts.hospital,
"imagingCenters": counts.imagingcenter,
"other": counts.other
}
// Updating npiId & category count
userproviderData.getDataSource().connector.connect(async (err, db) => {
let userProviderCollection = db.collection("UserProviders");
let regUserId = { userId: userId};
let updateProviderId = await userProviderCollection.update(
regUserId, {
$set: {
providers: combinedProviderArray,
countByType: providerCountType
}
});
});
请为此提供适当的解决方案。任何帮助将非常感激。我被困在如何将提供者添加到数组并据此增加计数。
您可以使用$addToSet
. 这具有将值添加到数组的效果,但前提是它尚未存在...
let updateProviderId = await userProviderCollection.update(
regUserId, {
$addToSet: {
providers: providerID
}
});
至于计数,可以使用 $inc 类似地完成。
let updateProviderId = await userProviderCollection.update(
regUserId, {
$inc: {
"countByType.doctors": 1
}
});
您可以轻松构建增量对象:
let $inc= {
"countByType.doctors": counts.doctor,
"countByType.labs": counts.laboratory,
"countByType.hospitals": counts.hospital,
"countByType.imagingCenters": counts.imagingcenter,
"countByType.other": counts.other
}
let updateProviderId = await userProviderCollection.update(regUserId, {$inc});
如果您只是添加一个,并且想同时完成所有操作,则可以只添加前两个示例:
let $inc = {};
$inc["countByType."+categoryName] = 1;
let updateProviderId = await userProviderCollection.update(
regUserId, {
$addToSet: {
providers: providerID
},
$inc
});
但即使提供者已经存在,它也会增加计数......所以你需要检查 $addToSet 是否有效,然后进行增加:
let updateProviderId = await userProviderCollection.update(
regUserId, {
$addToSet: {
providers: providerID
}
});
if(updateProviderId["nMatched"] === 1 //A document was matched
&& updateProviderId["nModified"] === 1 //A document was modified
){
let updateProviderCount = await userProviderCollection.update(regUserId, {$inc});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句