具有$ lookup的MongoDB聚合限制了一些要从查询返回的字段

编程入门 行业动态 更新时间:2024-10-28 19:26:01
本文介绍了具有$ lookup的MongoDB聚合限制了一些要从查询返回的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在mongo中,对$lookup执行aggregation后,我希望请求仅返回某些字段,而不返回整个文档.

我有以下查询:

db.somecollection.aggregate([{ $lookup: { from: "campaigns", localField: "campId", foreignField: "_id", as: "campaign" } }, { $unwind: "$campaign" }, { $lookup: { from: "entities", localField: "campaign.clientid", foreignField: "_id", as: "campaign.client" } }]);

此请求将向我返回此:

{ "_id" : ObjectId("56cc7cd1cc2cf62803ebfdc7"), "campId" : ObjectId("56c740e4479f46e402efda84"), "articleId" : ObjectId("56c742c06094640103ba3843"), "campaign" : { "_id" : ObjectId("56c740e4479f46e402efda84"), "clientid" : ObjectId("56c740b8479f46e402efda83"), "client" : [ { "_id" : ObjectId("56c740b8479f46e402efda83"), "username" : "someusername", "shhh" : "somehashedpassword", "email" : "mail@mail", } ] }

该请求运行良好,但我想过滤campaign.client中的字段,以仅获取例如_id和username的字段.有没有办法在MongoDB aggregate请求中做到这一点?

解决方案

@SiddhartAjmera只是为了帮助他人,我的答案是正确的,我只需要为"campaign.clientid"之类的嵌套值添加双引号即可. >

最终代码应为:

db.somecollection.aggregate([ { "$lookup": { "from": "campaigns", "localField": "campId", "foreignField": "_id", "as": "campaign" } }, { "$unwind": "$campaign" }, { "$lookup": { "from": "entities", "localField": "campaign.clientid", "foreignField": "_id", "as": "campaign.client" } }, { "$project": { "_id": 1, "campId": 1, "articleId": 1, "campaign._id": 1, "campaign.clientid": 1, "campaign.client._id": 1, "campaign.client.username": 1 } } ]);

In mongo, after doing an aggregation with $lookup, I would like the request to return only some fields and not the whole document.

I have the following query :

db.somecollection.aggregate([{ $lookup: { from: "campaigns", localField: "campId", foreignField: "_id", as: "campaign" } }, { $unwind: "$campaign" }, { $lookup: { from: "entities", localField: "campaign.clientid", foreignField: "_id", as: "campaign.client" } }]);

This request will return me this :

{ "_id" : ObjectId("56cc7cd1cc2cf62803ebfdc7"), "campId" : ObjectId("56c740e4479f46e402efda84"), "articleId" : ObjectId("56c742c06094640103ba3843"), "campaign" : { "_id" : ObjectId("56c740e4479f46e402efda84"), "clientid" : ObjectId("56c740b8479f46e402efda83"), "client" : [ { "_id" : ObjectId("56c740b8479f46e402efda83"), "username" : "someusername", "shhh" : "somehashedpassword", "email" : "mail@mail", } ] }

The request works well, but I would like to filter the fields in campaign.client to only get for example _id and username. Is there a way to do this in a MongoDB aggregate request?

解决方案

Just to help others with this, @SiddhartAjmera has the right answer, I only needed to add double quotes for nested values like "campaign.clientid".

The final code should be:

db.somecollection.aggregate([ { "$lookup": { "from": "campaigns", "localField": "campId", "foreignField": "_id", "as": "campaign" } }, { "$unwind": "$campaign" }, { "$lookup": { "from": "entities", "localField": "campaign.clientid", "foreignField": "_id", "as": "campaign.client" } }, { "$project": { "_id": 1, "campId": 1, "articleId": 1, "campaign._id": 1, "campaign.clientid": 1, "campaign.client._id": 1, "campaign.client.username": 1 } } ]);

更多推荐

具有$ lookup的MongoDB聚合限制了一些要从查询返回的字段

本文发布于:2023-11-22 10:03:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616981.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   lookup   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!