如何在MongoDB中组合小组和项目
在这里,我有一个SQL查询,需要转换为Mongo查询。 SQL查询:
select p.producer_id,p.name from producer p join
(select distinct ps.service_id,ps.value from service ps where ps.service_id=p.service_id) join
(select distinct pp.property_id,pp.property from property pp where pp.service_id=p.service_id)
我的Mongo查询:
db.producer.aggregate([
{
"$lookup": {
"from": "service",
"localField": "producer_id",
"foreignField": "service_id",
"as": "ps"
}
},
{
"$unwind": "$ps"
},
{
"$lookup": {
"from": "property",
"localField": "producer_id,
"foreignField": "property_id",
"as": "pp"
}
},
{
"$unwind": "$pp"
},
{
"$group": {
"_id": {
"property_id": "$pp.property_id",
"service_id": "$ps.service_id"
}
}
},
{
"$project": {
"producer_id": "$p.producer_id",
"name": "$p.name",
"service_id":"$ps.service_id",
"value":"$ps.value",
"property_id":"$pp.property_id",
"property":"$pp.property",
"_id":0
}
}
]);
示例输入记录: 生产者:
[{producer_id:1,name:'test'},{producer_id:2,name:'test2'}]
服务:
[{service_id:1,value:12},{service_id:1,value:13},{service_id:2,value:14}]
属性:
[{property_id:1,property:12},{property_id:1,property:56},{property_id:2,property:34}]
但是在输出中,我可以看到组结果。当我尝试尝试从各个集合中投射剩余的列(“名称”,“值”,“属性”字段)时,这些值未显示在输出中。在这里,我必须选择不同的记录,并显示不同的记录以及其他记录。当我尝试在组中添加所有字段时('name','value','属性'字段),我可以看到所有记录,但性能很慢。有人可以帮我吗?
here I have an sql query which needs to converted to mongo query.
sql query:
select p.producer_id,p.name from producer p join
(select distinct ps.service_id,ps.value from service ps where ps.service_id=p.service_id) join
(select distinct pp.property_id,pp.property from property pp where pp.service_id=p.service_id)
My mongo query:
db.producer.aggregate([
{
"$lookup": {
"from": "service",
"localField": "producer_id",
"foreignField": "service_id",
"as": "ps"
}
},
{
"$unwind": "$ps"
},
{
"$lookup": {
"from": "property",
"localField": "producer_id,
"foreignField": "property_id",
"as": "pp"
}
},
{
"$unwind": "$pp"
},
{
"$group": {
"_id": {
"property_id": "$pp.property_id",
"service_id": "$ps.service_id"
}
}
},
{
"$project": {
"producer_id": "$p.producer_id",
"name": "$p.name",
"service_id":"$ps.service_id",
"value":"$ps.value",
"property_id":"$pp.property_id",
"property":"$pp.property",
"_id":0
}
}
]);
sample input records:
producer:
[{producer_id:1,name:'test'},{producer_id:2,name:'test2'}]
service:
[{service_id:1,value:12},{service_id:1,value:13},{service_id:2,value:14}]
property:
[{property_id:1,property:12},{property_id:1,property:56},{property_id:2,property:34}]
But in output, I am able to see group result. When I trying to trying to project the remaining columns ('name','value','property' fields) from respective collections those values are not displaying in the output. Here I have to select distinct records and display distinct records along with other records. When I try to add all fields in group like ('name','value','property' fields) I am able to see all records but the performance is slow. Can anyone please help me on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在查询中,在
$ group
阶段中,您需要使用累加器操作员进行额外字段累加器操作员
尝试此方法:
mongoplayground
In your query, in the
$group
stage, you need to use accumulator operator for your extra fieldsAccumulator Operator
Try this one:
MongoPlayground