Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose:$lookup之后的项目不显示字段_Javascript_Mongodb_Mongoose_Aggregation Framework - Fatal编程技术网

Javascript Mongoose:$lookup之后的项目不显示字段

Javascript Mongoose:$lookup之后的项目不显示字段,javascript,mongodb,mongoose,aggregation-framework,Javascript,Mongodb,Mongoose,Aggregation Framework,我有两个模型user.js和schedule.js,我需要使用一个查询聚合来连接这些模型。在$lookup之后,我使用了一个选择我希望在查询结果中显示的字段的方法,但是scheduleStart和scheduleEnd字段不会显示在我的结果中 User.js模型 Schedule.js模型 我的问题 我的查询结果: 您可以将低于$project阶段与聚合一起使用 { '$project': { '_id': 1, 'name': 1, 'cpf': 1, 'phone

我有两个模型user.js和schedule.js,我需要使用一个查询聚合来连接这些模型。在$lookup之后,我使用了一个选择我希望在查询结果中显示的字段的方法,但是scheduleStart和scheduleEnd字段不会显示在我的结果中

User.js模型

Schedule.js模型

我的问题

我的查询结果:

您可以将低于$project阶段与聚合一起使用

{ '$project': {
  '_id': 1,
  'name': 1,
  'cpf': 1,      
  'phone': 1,
  'email': 1,
  'birthday': 1,
  'totalServices': 1,
  'totalValue': { '$sum': '$user_detail.value' },
  'scheduleStart': { '$arrayElemAt': ['$user_detail.scheduleStart', 0] },
  'scheduleEnd': { '$arrayElemAt': ['$user_detail.scheduleEnd', 0] },
  'count': { '$sum': 1 }
}} 

scheduleStart是schedules集合中的字段。您的$lookup详细信息位于用户详细信息中。所以把用户的详细信息放在$project阶段。我怎么做呢?试试这个{$project:{$id:1,name:1,name:1,cpf:1,电话:1,电子邮件:1,生日:1,totalServices:1,totalValue:{$sum:$user\u detail.value},count:{$sum:1},user\u detail:1}我是否可以仅将scheduleStart和scheduleEnd放入$project中的user_详细信息中?
 service: {
    id: {
      type: String
    },
    name: {
      type: String,
      required: true
    },
    filters: [String]
  },
  info: {
    channel: {
      type: String,
      required: true,
      default: 'app'
    },
    id: String,
    name: String
  },
  scheduleDate: {
    type: String,
    required: true
  },
  scheduleStart: {
    type: String,
    required: true
  },
  scheduleEnd: {
    type: String,
    required: true
  },
 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" },
        scheduleStart: 1,
        scheduleEnd: 1,
        count: {
          $sum: 1
        }
      }
    }   
  ])...
count: 1
totalServices: 89
totalValue: 2374
_id:{
birthday: "1964-03-18",
cpf: "319335828",
email: "jdoe@gmail.com.br",
id: "5b1b1dcce1ab2a8eb580f",
name: "Jonh Doe",
phone: "11996370565"
}
{ '$project': {
  '_id': 1,
  'name': 1,
  'cpf': 1,      
  'phone': 1,
  'email': 1,
  'birthday': 1,
  'totalServices': 1,
  'totalValue': { '$sum': '$user_detail.value' },
  'scheduleStart': { '$arrayElemAt': ['$user_detail.scheduleStart', 0] },
  'scheduleEnd': { '$arrayElemAt': ['$user_detail.scheduleEnd', 0] },
  'count': { '$sum': 1 }
}}