2016-07-28 34 views
0

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

我想匹配数据集上的特定字段。该数据集包含像记录:

{ 
    "token" : "17e0d95f2e2112443cfb09970ae9140e", 
    "answers" : { 
     "Yourname" : "Marco", 
     "youremail" : "[email protected]", 
     "recemail" : "[email protected]" 
    } 
}, 


{ 
    "token" : "cf5ae65b05249dc6b2a0c99c4cf9688e", 
    "answers" : { 
     "yourname" : "Sonja", 
     "youremail" : "[email protected]", 
     "recemail" : "[email protected]" 
    } 
} 

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

因此,Marco的recemail应该找到Sonja的youremail,然后这两个匹配应该保存到另一个集合中,但这不是重要的部分。

希望有人能帮助我解决此问题 谢谢!

+0

你需要'X.youremail == Y.recemail && X.recemail == Y.youremail'?或者只是'X.youremail == Y.recemail || X.recemail == Y.youremail'? – Arnauld

+0

喜欢这个。 '''X.recemail == Y.youremail && Y.recemail == X.youremail''' – Marco

回答

1

以下代码将返回一个2入口数组的数组。每个2入口数组由发件人电子邮件和收件人电子邮件组成。每一对只会出现一次。换句话说,你会得到["[email protected]", "[email protected]"],但不是["[email protected]", "[email protected]"]

var list = [{ 
 
    "token" : "17e0d95f2e2112443cfb09970ae9140e", 
 
    "answers" : { 
 
     "Yourname" : "Marco", 
 
     "youremail": "[email protected]", 
 
     "recemail" : "[email protected]" } 
 
    }, { 
 
    "token" : "cf5ae65b05249dc6b2a0c99c4cf9688e", 
 
    "answers" : { 
 
     "yourname" : "Sonja", 
 
     "youremail": "[email protected]", 
 
     "recemail" : "[email protected]" } 
 
    }, { 
 
    "token" : "fe2c2740e083dfe02cc7e96520a0e079", 
 
    "answers" : { 
 
     "yourname" : "Joe", 
 
     "youremail": "[email protected]", 
 
     "recemail" : "[email protected]" } 
 
    } 
 
]; 
 

 
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);

+0

非常感谢,但问题与Mongoose查询有关。这有效,我可以获取所有数据,然后在节点上获取我想要的数据,但这是我最初需要的MongoDB查询。如果我没有得到满意的答案,我一定会随着你的回答而去。 :) – Marco

+0

@Marco好啊。感谢您接受了这一点,并感到抱歉,因为无法为您提供更多帮助。 – Arnauld

相关问题