MongoDB查找具有反向匹配的记录
我有以下结构可以存储用户的类似。例如,_id1用户喜欢用户_id2& _id3。 _id1用户仅与用户_id2匹配。
我只需要匹配记录。
文档结构 -
[ {'来自':'_ id1','to':'_ ID2'}, {'来自':'_ id1','to':'_ id3'}, {'来自':'_ id2','to':'_ ID1'}, 这是给出的
预期输出 -
[ {'来自':'_ id1','to':'_ ID2'}, {'来自':'_ id2','to':'_ ID1'}, 这是给出的
I have the following structure to store user's like. For example, _id1 user likes user _id2 & _id3. Where the _id1 user is having a match with user _id2 only.
I need only records with a match.
Document Structure --
[ {'from':'_id1', 'to':'_id2'}, {'from':'_id1', 'to':'_id3'}, {'from':'_id2', 'to':'_id1'}, ]
Expected Output --
[ {'from':'_id1', 'to':'_id2'}, {'from':'_id2', 'to':'_id1'}, ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在“夫妇”上
$ group
,然后匹配那些被总结为2的。我只是用$ cond
做到的,然后创建元组[_id1,_id2]
,其中较小的ID总是首先是这样,例如:You can
$group
on the "couple" and only then match those that were summed to be 2. The trick is to make sure the_id
your grouping on is the same - I just did it with$cond
and create the tuple[ _id1, _id2 ]
where the smaller id is always first, like so: