如何检查mongodb中的值是否相同

时间:2017-08-19 15:19:25

标签: node.js mongodb mongodb-query

我想用mongodb聚合检查值是否是相同的外部值。我想使用聚合方法查找结果。非常感谢你。

var id = 5995cc31f36d28126e46e670

{ _id: 5995cc31f36d28126e46e670,
  income: [ '23', '23', '232', '', '', '', '' ],
  incomedetail: [ '23', '23', 'ew', '', '', '', '' ],
   outcome: [ '23', '23', '43', '', '', '', '' ],
   outcomedetail: [ '23', '23', 'fsdf', '', '', '', '' ],
  date:
    [ '2017-08-18',
     '2017-08-18',
     '2017-08-17',
     '2017-08-11',
     '2017-08-26',
     '2017-08-26',
     '11/02/2345' ] }
{ _id: 5995d9f8f36d28126e46ed17,
  income: [ '', '', '' ],
  incomedetail: [ '', '', '' ],
  outcome: [ '40', '40', '72' ],
  outcomedetail: [ '23', '32', '32' ],
  date: [ '2017-08-18', '2017-08-18', '2017-08-18' ] }

输出

{ _id: 5995cc31f36d28126e46e670,
  income: [ '23', '23', '232', '', '', '', '' ],
  incomedetail: [ '23', '23', 'ew', '', '', '', '' ],
   outcome: [ '23', '23', '43', '', '', '', '' ],
   outcomedetail: [ '23', '23', 'fsdf', '', '', '', '' ],
  date:
    [ '2017-08-18',
     '2017-08-18',
     '2017-08-17',
     '2017-08-11',
     '2017-08-26',
     '2017-08-26',
     '11/02/2345' ] }

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解你的问题,但也许这会帮助你解决问题:

function exampleCheck(JSONObj, theID){
 if( JSONObj._id == theID){ return "match"; }
 else{ return "no match"; }
}

var testObj = { 
  _id: 5995d9f8f36d28126e46ed17,
  income: [ '', '', '' ],
  incomedetail: [ '', '', '' ],
  outcome: [ '40', '40', '72' ],
  outcomedetail: [ '23', '32', '32' ],
  date: [ '2017-08-18', '2017-08-18', '2017-08-18' ] 
};

console.log( exampleCheck(testObj, '5995cc31f36d28126e46e670') );