$lookup mongodb 中的 $project

编程入门 行业动态 更新时间:2024-10-28 07:19:52
本文介绍了$lookup mongodb 中的 $project的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个查询,使用 $lookup 到加入"两个模型,在此之后我使用 $project 选择 olny我需要的字段,但我的 $project 带来了一组对象 (user_detail),其中包含我需要的更多数据.我只需要结果的两个字段(scheduleStart 和 scheduleEnd).

I have a query, that use $lookup to "join" two models, after this i use $project to select olny the fields that i need, but my $project brings an arrray of objects (user_detail) that contains more data that i need. I want only two fields (scheduleStart and scheduleEnd) of my result.

我的查询:

User.aggregate([{ $match: { storeKey: req.body.store, } }, { $group: { _id: { id: "$_id", name: "$name", cpf: "$cpf", phone: "$phone", email: "$email", birthday: "$birthday", lastName: "$lastname" }, totalServices: { $sum: "$services" }, } }, { $lookup: { from: "schedules", localField: "_id.phone", foreignField: "customer.phone", as: "user_detail" } }, { $project: { _id: 1, name: 1, name: 1, cpf: 1, phone: 1, email: 1, birthday: 1, totalServices: 1, totalValue: { $sum : "$user_detail.value" }, count: { $sum: 1 }, user_detail: 1 } },

查询结果:

count: 1 totalServices: 0 totalValue: 73 user_detail: Array(2) 0: ... paymentMethod: 0 paymentValue: "0" scheduleDate: "2018-10-02" scheduleEnd: "2018-10-02 08:40" scheduleStart: "2018-10-02 08:20" status: 3 store: "5b16cceb56a44e2f6cd0324b" updated: "2018-11-27T13:30:21.116Z" 1: ... paymentMethod: 0 paymentValue: "0" scheduleDate: "2018-11-27" scheduleEnd: "2018-11-27 00:13" scheduleStart: "2018-11-27 00:03" status: 2 store: "5b16cceb56a44e2f6cd0324b" updated: "2018-11-27T19:33:39.498Z" _id: birthday: "1992-03-06" email: "csantosgrossi@gmail" id: "5bfed8bd70de7a383855f09e" name: "Chris Santos G" phone: "11969109995" ...

我需要的结果:

count: 1 totalServices: 0 totalValue: 73 user_detail: Array(2) 0: scheduleEnd: "2018-10-02 08:40" scheduleStart: "2018-10-02 08:20" 1: scheduleEnd: "2018-11-27 00:13" scheduleStart: "2018-11-27 00:03" _id: birthday: "1992-03-06" email: "csantosgrossi@gmail" id: "5bfed8bd70de7a383855f09e" name: "Chris Santos G" phone: "11969109995" ...

我该如何处理我的查询?

How can i do this with my query?

推荐答案

您可以使用 $lookup 3.6 语法到 $project $lookup 管道

You can use $lookup 3.6 syntax to $project the fields inside the $lookup pipeline

User.aggregate([ { "$lookup": { "from": "schedules", "let": { "id": "$_id.phone" }, "pipeline": [ { "$match": { "$expr": { "$eq": ["$customer.phone", "$$id"] }}}, { "$project": { "scheduleStart": 1, "scheduleEnd": 1 }} ], "as": "user_detail" }} ])

更多推荐

$lookup mongodb 中的 $project

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

发布评论

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

>www.elefans.com

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