如果数组中不存在值,则如何从数据库中删除值

时间:2018-01-05 11:18:50

标签: node.js postgresql postman

db.query('DELETE FROM position WHERE position <> ALL ($1)',[req.body.positionDetails.position],function(err,data){
  if (err) return callback(new Error(err));
  console.log('deleted')                            
})

这里我想删除数据库中不存在的position值。

数组如下:

{
   "positionDetails":[
      {
         "position":"developer",
         "fullTime":"true",
         "unit":"er",
         "office_country":"india",
         "office_city":"gyu",
         "technology":"ghj",
         "framework":"fgb",
         "position_start":"1-2-2012",
         "position_end":"3-12-2020",
         "company_id":"1",
         "monthly_target":"200",
         "wage":"sdfg"
      },
      {
         "position":"manager",
         "fullTime":"true",
         "unit":"er",
         "office_country":"india",
         "office_city":"gyu",
         "technology":"ghj",
         "framework":"fgb",
         "position_start":"1-2-2012",
         "position_end":"3-12-2020",
         "company_id":"1",
         "monthly_target":"200",
         "wage":"sdfg"
      }
   ]
}

但它正在删除数据库中的所有值如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

var anyvalue=""
data.positionDetails.position.map((item,i)=>{
    if(item == DELETE_ITEM) {
        anyvalue += item+","
    }     
})

注意:您必须使用修剪或任何其他功能从,删除最后anyvalue

db.query('DELETE FROM position WHERE position IN (anyvalue),[data.positionDetails.position],function(err,data){
  if (err) return callback(new Error(err));
  console.log('deleted')                            
})

由于postionDetail有位置字段,它将删除特定的位置对象。