Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更新提供程序阵列&;仅当文档中不存在providerId时增加计数_Javascript_Node.js_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Javascript 更新提供程序阵列&;仅当文档中不存在providerId时增加计数

Javascript 更新提供程序阵列&;仅当文档中不存在providerId时增加计数,javascript,node.js,mongodb,mongodb-query,aggregation-framework,Javascript,Node.js,Mongodb,Mongodb Query,Aggregation Framework,我陷入了这样的境地。我有以下文件 { "_id" : ObjectId("5ceba7c419b48423e452972a"), "userId" : ObjectId("5ceba7c419b48423e4529727"), "providers" : [ "1689736266", "1598763690", "1528069614", "1831364272", "154846

我陷入了这样的境地。我有以下文件

{
    "_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是在生成新报告时创建的。如果报告已经存在,则上述条件将起作用。 但是,当一个新报告出现时,我需要签入providers数组并增加计数(如果不在数组中),否则什么也不做。我怎样才能做到这一点

当具有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});
}

请告诉我一种使用$addToSet在单个更新查询中更新providerArray和更新计数的方法。我没给你带路。你能解释一下吗?对不起,如果你不介意的话,你能看一下我的文件吗?或者我们可以直接聊天吗?对不起,不是真的。。。没时间这么做。倒数第二个代码段不同时更新计数和设置。能否将代码作为一个文件提供?对我来说,理解是件好事。不,我不会为你把一切都粘在一起。主要原因是如果有人总是为你做,你将如何学习?