从两个连接的文档获取电话和电子邮件的管道是什么?

问题描述 投票:0回答:1

我正在尝试将从其他两个文档获取的电子邮件和电话号码合并到为用户返回的单个文档中。我有一条“几乎”有效的管道...但不太正确。

这是我的架构:

用户

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
  fName: { type: String, required: true },
  lName: { type: String, required: true },

});

module.exports = User = mongoose.model('users', UserSchema);

一些用户数据:

{
  "_id":"65d7e9a82bd4a4e423570962",
  "fName": "Mike",
  "lName": "Liss",

}

用户联系信息架构

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// create the schema
const UserContactInfoSchema = new Schema({
  REF_UserID: { type: Schema.Types.ObjectId, required: true, ref: 'users' },
  REF_ContactInfoID: { type: Schema.Types.ObjectId, required: true, ref: 'contactinfos' },
});

module.exports = UserContactInfo = mongoose.model('usercontactinfos', UserContactInfoSchema);

用户联系信息数据:

{
  "_id": "65d7e9a82bd4a4e423570965",
  "REF_UserID": "65d7e9a82bd4a4e423570962",
  "REF_ContactInfoID":"65d7e9a72bd4a4e423570959"
},
{
  "_id":"65d7e9a92bd4a4e42357096a",
  "REF_UserID":"65d7e9a82bd4a4e423570962",
  "REF_ContactInfoID":"65d7e9a82bd4a4e42357095e"
}

联系信息架构

const mongoose = require('mongoose');
const Schema = mongoose.Schema;


// create the schema
const ContactInfoSchema = new Schema({
  value: { type: String, required: true },// either phone number or email
  type: { type: Number, required: true },
});

module.exports = ContactInfo = mongoose.model('contactinfos', ContactInfoSchema);

联系信息数据:

{
  "_id": "65d7e9a72bd4a4e423570959",
  "value": "[email protected]",
  "type": 0
},
{
  "_id":"65d7e9a82bd4a4e42357095e",
  "value": "5102071234",
  "type": 1
}

这是我的管道:

[
  {
    $lookup: {
      from: "usercontactinfos",
      localField: "_id",
      foreignField: "REF_UserID",
      as: "uci",
    },
  },
  {
    $unwind: {
      path: "$uci",
      preserveNullAndEmptyArrays: true,
    },
  },
  {
    $lookup: {
      from: "contactinfos",
      localField: "uci.REF_ContactInfoID",
      foreignField: "_id",
      as: "ci",
    },
  },
  {
    $unwind: {
      path: "$ci",
      preserveNullAndEmptyArrays: true,
    },
  },
  {
    $project: {
      _id: 1,
      fName: 1,
      lName: 1,
      email: {
        $cond: {
          if: {
            $eq: ["$ci.type", 0],
          },
          then: "$ci.value",
          else: null,
        },
      },
      phone: {
        $cond: {
          if: {
            $eq: ["$ci.type", 0],
          },
          then: "$ci.value",
          else: null,
        },
      },
    },
  },
  {
    $group: {
      _id: "$_id",
      fName: {
        $first: "$fName",
      },
      lName: {
        $first: "$lName",
      },
      email: {
        $first: "$email",
      },
      phone: {
        $first: "$phone",
      },
    },
  },
]

我得到了什么:

{
  "_id": "65d7e9a82bd4a4e423570962",
  "fName": "Mike",
  "lName": "Liss",
  "email": "[email protected]",
  "phone": "[email protected]"
}

我想要实现的目标:

{
  "_id": "65d7e9a82bd4a4e423570962",
  "fName": "Mike",
  "lName": "Liss",
  "email": "[email protected]",
  "phone": "510-207-1234"
}
mongodb pipeline aggregation
1个回答
0
投票

你的

$project
有一个小错误。您似乎正在检查
type == 0
的电子邮件和电话。之后,为了避免在取
null
时出现
$first
,您可以使用
$max
代替,这样它将取该字段的最大值而不是第一个

  {
    $project: {
      _id: 1,
      fName: 1,
      lName: 1,
      email: {
        $cond: {
          if: {
            $eq: [
              "$ci.type",
              0
            ]
          },
          then: "$ci.value",
          else: null
        }
      },
      phone: {
        $cond: {
          if: {
            $eq: [
              "$ci.type",
              1
            ]
          },
          then: "$ci.value",
          else: null
        }
      }
    }
  },
  {
    $group: {
      _id: "$_id",
      fName: {
        $first: "$fName"
      },
      lName: {
        $first: "$lName"
      },
      email: {
        $max: "$email"
      },
      phone: {
        $max: "$phone"
      }
    }
  }

游乐场

© www.soinside.com 2019 - 2024. All rights reserved.