匹配Mongoose不同记录中的2个字段

时间:2022-09-11 19:27:39

Goodmorning

I am trying to match certain fields over a dataset. The dataset contains records like:

我试图匹配数据集上的某些字段。数据集包含以下记录:

{
    "token" : "17e0d95f2e2112443cfb09970ae9140e",
    "answers" : {
        "Yourname" : "Marco",
        "youremail" : "marco@me.nl",
        "recemail" : "sonja@me.nl"
    }
},


{
    "token" : "cf5ae65b05249dc6b2a0c99c4cf9688e",
    "answers" : {
        "yourname" : "Sonja",
        "youremail" : "sonja@me.nl",
        "recemail" : "marco@me.nl"
    }
}

In this case, it should match the two because the recemail (recipients email) matches someone else's email address. And vice versa. What I am trying to find is how to find these two records (in a larger dataset with more of these matches).

在这种情况下,它应匹配这两个,因为收件人(收件人电子邮件)匹配其他人的电子邮件地址。反之亦然。我想要找到的是如何找到这两个记录(在更大的数据集中有更多这些匹配)。

So, Marco's recemail should find Sonja's youremail and these two matches should then be made available to save into another collection, but that's not the important part here.

所以,Marco的收据应该找到Sonja的你的邮件,这两个匹配应该可以保存到另一个收藏中,但这不是重要的部分。

hope someone can help me with this query Thanks!

希望有人可以帮我解决这个问题谢谢!

1 个解决方案

#1


1  

The following code will return an array of 2-entry arrays. Each 2-entry array is made of the sender email and the recipient email. Each pair will appear only once. In other words, you'll get ["marco@me.nl", "sonja@me.nl"] but not ["sonja@me.nl", "marco@me.nl"].

以下代码将返回一个2项数组的数组。每个2项阵列都由发件人电子邮件和收件人电子邮件组成。每对只出现一次。换句话说,你会得到[“marco@me.nl”,“sonja@me.nl”]但不是[“sonja@me.nl”,“marco@me.nl”]。

var list = [{
    "token" : "17e0d95f2e2112443cfb09970ae9140e",
    "answers" : {
        "Yourname" : "Marco",
        "youremail": "marco@me.nl",
        "recemail" : "sonja@me.nl" }
  }, {
    "token" : "cf5ae65b05249dc6b2a0c99c4cf9688e",
    "answers" : {
        "yourname" : "Sonja",
        "youremail": "sonja@me.nl",
        "recemail" : "marco@me.nl" }
  }, {
    "token" : "fe2c2740e083dfe02cc7e96520a0e079",
    "answers" : {
        "yourname" : "Joe",
        "youremail": "joe@foo.com",
        "recemail" : "mike@bar.com" }
  }
];

var res = [], sz = list.length;

list.forEach(function(obj, pos) {
  for(var i = pos + 1; i < sz; i++) {
    if(
      list[i].answers.youremail == obj.answers.recemail &&
      list[i].answers.recemail == obj.answers.youremail
    ) {
      res.push([ obj.answers.youremail, obj.answers.recemail ]);
    }
  }
});
console.log(res);

#1


1  

The following code will return an array of 2-entry arrays. Each 2-entry array is made of the sender email and the recipient email. Each pair will appear only once. In other words, you'll get ["marco@me.nl", "sonja@me.nl"] but not ["sonja@me.nl", "marco@me.nl"].

以下代码将返回一个2项数组的数组。每个2项阵列都由发件人电子邮件和收件人电子邮件组成。每对只出现一次。换句话说,你会得到[“marco@me.nl”,“sonja@me.nl”]但不是[“sonja@me.nl”,“marco@me.nl”]。

var list = [{
    "token" : "17e0d95f2e2112443cfb09970ae9140e",
    "answers" : {
        "Yourname" : "Marco",
        "youremail": "marco@me.nl",
        "recemail" : "sonja@me.nl" }
  }, {
    "token" : "cf5ae65b05249dc6b2a0c99c4cf9688e",
    "answers" : {
        "yourname" : "Sonja",
        "youremail": "sonja@me.nl",
        "recemail" : "marco@me.nl" }
  }, {
    "token" : "fe2c2740e083dfe02cc7e96520a0e079",
    "answers" : {
        "yourname" : "Joe",
        "youremail": "joe@foo.com",
        "recemail" : "mike@bar.com" }
  }
];

var res = [], sz = list.length;

list.forEach(function(obj, pos) {
  for(var i = pos + 1; i < sz; i++) {
    if(
      list[i].answers.youremail == obj.answers.recemail &&
      list[i].answers.recemail == obj.answers.youremail
    ) {
      res.push([ obj.answers.youremail, obj.answers.recemail ]);
    }
  }
});
console.log(res);