我有一个查询,使用$lookup来"连接"两个模型,之后我使用$project来 Select 我需要的所有字段,但我的$project带来了一系列对象(user_detail),其中包含我需要的更多数据.我只想要结果的两个字段(scheduleStartscheduleEnd).

My query:

 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
      }
    },

Result of query:

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.com"
id: "5bfed8bd70de7a383855f09e"
name: "Chris Santos G"
phone: "11969109995"
...

Result that i need:

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.com"
id: "5bfed8bd70de7a383855f09e"
name: "Chris Santos G"
phone: "11969109995"
...

我该如何处理我的查询?

推荐答案

您可以使用$lookup 3.6语法来删除$lookup管道中的字段

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

Mongodb相关问答推荐

MongoDB—基于数组中同一文档中的另一个字段更新字段

在MogoDB中按时间间隔分组、统计文档和获取间隔时间

MongoDB合并按键更新值的对象数组

MongoDB:就地分组嵌套数组的元素

MongoDB获取所有文档谁的S子文档只包含一个特定值?

我可以在 MongoDB 中将字段值设置为对象键吗?

如何在 MongoDB 中已经存在的 slug 中添加一个随机数?

MongoDb 聚合未正确分组

从嵌套数组中 id 匹配的另一个集合中获取所有字段

在亚马逊 EC2 上托管 nodeJS/mongoose Web 应用程序

删除一对一和一对多引用 - Mongoose

findOneAndUpdate 和 findOneAndReplace 有什么区别?

Mongoose 架构引用和未定义类型ObjectID

更新 mongoengine 中的嵌入文档列表

在 Mongo 中存储嵌套类别的最有效方法?

在 GridFS、express、mongoDB、node.js 中存储来自 POST 请求的数据流

Node.js MongoDB Upsert 更新

带有排序功能的mongoose findOne

如何使用 MongoDB Compass 对数据进行排序

哪个数据库适合我的应用程序 mysql 或 mongodb ?使用 Node.js 、 Backbone 、 Now.js