1

I know this question has been answered here, however every single answer doesn't work in my script, asking this was my very last resort.

I am trying to find new data uploaded to a database, this is the logic it follows:

Read data from my Mongo database store as a >> Pull new data from sql server, store as b >> filter the data and overwrite my Mongo database >> compare a and b to find new documents uploaded.

Here is the code I have so far:

function comparer(otherArray){
  return function(current){
    return otherArray.filter(function(other){
      return other.value == current.value && other.display == current.display
    }).length == 0;
  }
}

var onlyInA = a.filter(comparer(b));
var onlyInB = b.filter(comparer(a));

result = onlyInA.concat(onlyInB);

console.log(result);

I obtain a and b like so:

a = await dbo.collection("master").find({ }, {projection:{_id: 0}}).toArray();

//upload all new data here, so b contains more objects than a

b = await dbo.collection("master").find({ }, {projection:{_id: 0}}).toArray();

I am expecting this to return all objects in b except if the objects exist in a, so I should see new data.

Here is a brief sample of my database;

a = [{
            "Process": "Process1",
            "Num": "000000",
        }]

b = [{
            "Process": "Process1",
            "Num": "000000",
        },
        {
            "Process": "Process2",
            "Num": "000107",
        }]

Therefore b contains more objects, so finding the difference should show the object containing process 2. Thanks for any help!

EcSync
  • 842
  • 1
  • 6
  • 20

1 Answers1

2

The question/answer you took comparer from referred to properties called display and value, but your objects have properties Process and Num.

When you change to the correct properties, the result is as expected.

var a = [{
            "Process": "Process1",
            "Num": "000000",
        }]

var b = [{
            "Process": "Process1",
            "Num": "000000",
        },
        {
            "Process": "Process2",
            "Num": "000107",
        }]
        
function comparer(otherArray){
  return function(current){
    return otherArray.filter(function(other){
      return other.Process == current.Process && other.Num == current.Num
    }).length == 0;
  }
}

var onlyInA = a.filter(comparer(b));
var onlyInB = b.filter(comparer(a));

result = onlyInA.concat(onlyInB);

console.log(result);
Jamiec
  • 133,658
  • 13
  • 134
  • 193