如何比较MongoDB中不同集合的字段
在这里,我有来自多个表的多个字段,这些值需要比较,需要显示所需的结果。
SQL查询:
select pd.service_id,ps.service_id from player pd, service ps where pd.subject_id=ps.subject_id and pd.service_id = ps.service_id
Mongo查询:
db.player.aggregate([
{
"$lookup":{
"from":"service",
"localField":"player.subject_id",
"foreignField":"subject_id",
"as":"ps"
}
},
{
"$unwind":"$ps"
},
{
"$match":{
"service_id":{
"$eq": "ps.service_id"
}
}
}
];
示例输入记录:
播放器:
[{subject_id:23,service_id:1},{subject_id:76,service_id:9}]
服务:
[{subject_id:76,service_id:9},{subject_id:99,service_id:10}]
比赛不起作用。我必须匹配两个集合的service_id。需要获得匹配的记录。但看不到任何结果。任何人都可以帮我找出错误...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在查询中,如果要比较文档本身的2个值,则需要使用 $ expr 操作员
mongoplay
' >您需要使用 to“*加入”带2 o更多条件
In your query, if you want to compare 2 values from the document itself, you need to use $expr operator
MongoPlayground
Alternative solution: You need to use Uncorrelated sub-query to "* join" with 2 o more conditions
MongoPlayground