简体   繁体   中英

Populate data from another collection mongoose

I have 3 collections users, profiles and trustedcontacts. Profiles and trustedcontacts have ref to users

My db collections

I have collection users

{
    "_id": {
        "$oid": "5c5ecaf6134fc342d4b1a9d5"
    },
    "name": "User",
    "email": "user@gmail.com",
    "password": "$2a$10$BXxwpMTFK1a0aWclaqJYve4f3SZyi/emwHKv5rY2GNzrPSEsIJhzi",
},
{
    "_id": {
        "$oid": "5c64968cae53a8202c963223"
    },
    "name": "User1",
    "email": "user1@gmail.com",
    "password": "$2a$10$BXxwpMTFK1a0aWclaqJYve4f3SZyi/emwHKv5rY2GNzrPSEsIJhzi",
},
{
    "_id": {
        "$oid": "5c69968cae53a8202c963554"
    },
    "name": "User1",
    "email": "user1@gmail.com",
    "password": "$2a$10$BXxwpMTFK1a0aWclaqJYve4f3SZyi/emwHKv5rY2GNzrPSEsIJhzi",
}

collection profiles

{
    "_id": {
        "$oid": "5c5ecb17134fc342d4b1a9d6"
    },
    "user": {
        "$oid": "5c5ecaf6134fc342d4b1a9d5"
    },
    "handle": "handle",
    "company": "test"
},
{
    "_id": {
        "$oid": "5c6496ebae53a8202c963224"
    },
    "user": {
        "$oid": "5c64968cae53a8202c963223"
    },
    "handle": "handle1",
    "company": ""
},
{
    "_id": {
        "$oid": "5c6496ebae53a8202c963224"
    },
    "user": {
        "$oid": "5c69968cae53a8202c963554"
    },
    "handle": "handle2",
    "company": ""
}

collection trustedcontacts


{
    "_id": {
        "$oid": "5d76008e4b98e63e58cb34cc"
    },
    "approvedTrustedContacts": [
        {
            "_id": {
                "$oid": "5d764e411b7476462cf6b540"
            },
            "user": {
                "$oid": "5c5ecaf6134fc342d4b1a9d5"
            }
        },
        {
            "_id": {
                "$oid": "5d764e411b7476462cf6b541"
            },
            "user": {
                "$oid": "5c64968cae53a8202c963223"
            }
        }
    ],
    "pendingApprovalContacts": [],
    "waitingForApprovalContacts": [],
    "user": {
        "$oid": "5d76008e4b98e63e58cb34cb"
    }
}

//My Schemas

const UserSchema = new mongoose.Schema({
  name: {
    type: String,
  },
  email: {
    type: String,
  }
});
export default mongoose.model('User', UserSchema);

const ProfileSchema = new mongoose.Schema({
 user: {
   type: mongoose.Schema.Types.ObjectId,
   ref: 'User'
 },
  handle: {
   type: String,
  },
  company: {
   type: String,
  },
});

export default mongoose.model('Profile', ProfileSchema);
import mongoose from 'mongoose';

const TrustedContactsSchema = new mongoose.Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  },
  approvedTrustedContacts: [
    {
      user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
      }
    }
  ],
  ...
});

export default mongoose.model('TrustedContacts', TrustedContactsSchema);

I can populate trusted contact by user

const user = await TrustedContacts.findOne({ user: req.user.id }).populate('approvedTrustedContacts.user', ['name', 'email']);

and I got

 "user": {
        "date": "2019-09-09T07:32:20.174Z",
        "_id": "5d76008e4b98e63e58cb34cc",
        "approvedTrustedContacts": [
            {
                "_id": "5d764e411b7476462cf6b540",
                "user": {
                    "_id": "5c5ecaf6134fc342d4b1a9d5",
                     "name": "User",
                      "email": "user@gmail.com",
                }
            },
             {
                "_id": "5d764e411b7476462cf6b541",
                "user": {
                    "_id": "5c64968cae53a8202c963223",
                     "name": "User1",
                      "email": "user1@gmail.com",
                }
            }
        ],
        "pendingApprovalContacts": [],
        "waitingForApprovalContacts": [],
        "user": "5d76008e4b98e63e58cb34cb",
    }

Expected Output

It is possible to get list of approvedTrustedContacts with profile data

"approvedTrustedContacts": [
            {
                "_id": "5d764e411b7476462cf6b540",
                "user": {
                    "_id": "5c5ecaf6134fc342d4b1a9d5",
                     "name": "User",
                      "email": "user@gmail.com",
                },
                 "handle": "handle",
                 "company": "test"
            },
             {
                "_id": "5d764e411b7476462cf6b541",
                "user": {
                    "_id": "5c64968cae53a8202c963223",
                     "name": "User1",
                      "email": "user1@gmail.com",
                },
                "handle": "handle1",
                "company": "test1"
            }
        ],

Also I have joined 2 collections like this

 let result1 = await TrustedContacts.aggregate([
        { $lookup: { from: "profiles", localField: "user", foreignField: "user", as: "approvedTrustedContacts"  } },
      ]);

And I got

 {
            "_id": "5d76008e4b98e63e58cb34cc",
          "approvedTrustedContacts": [
                {
                    "_id": "5d764f551b7476462cf6b542",
                   "user": "5d76008e4b98e63e58cb34cb",
                    "handle": "handle",
                    "company": "test",
                },
                {
                    "_id": "5c5ecb17134fc342d4b1a9d6",
                    "user": "5c5ecaf6134fc342d4b1a9d5",
                    "handle": "handle1",
                    "company": "test1",
                }
            ],
            "pendingApprovalContacts": [],
            "waitingForApprovalContacts": [],
            "user": "5d76008e4b98e63e58cb34cb",
        }
    ],

Now I don't know how to polute user to have an output like this:

 {
            "_id": "5d76008e4b98e63e58cb34cc",
          "approvedTrustedContacts": [
                {
                    "_id": "5d764f551b7476462cf6b542",
                  "user": {
                    "_id": "5c5ecaf6134fc342d4b1a9d5",
                     "name": "User",
                      "email": "user@gmail.com",
                },
                    "handle": "handle"
                    "company": "test",
                },
              {
                    "_id": "5c5ecb17134fc342d4b1a9d6",
                   "user": {
                    "_id": "5c64968cae53a8202c963223",
                     "name": "User1",
                      "email": "user1@gmail.com",
                },
                    "handle": "handle1",
                    "company": "test1",
                }
            ],
            "pendingApprovalContacts": [],
            "waitingForApprovalContacts": [],
            "user": "5d76008e4b98e63e58cb34cb",
        }
    ],

My solution

let result = await TrustedContacts.aggregate([
        { $lookup: { from: "profiles", localField: "approvedTrustedContacts.user", foreignField: "user", as: "approvedTrustedContacts.profile"  } },
        { $unwind: "$approvedTrustedContacts.profile" },
        { $lookup: { from: "users", localField: "approvedTrustedContacts.profile.user", foreignField: "_id", as: "approvedTrustedContacts.profile.user"  } },
        { $unwind: "$approvedTrustedContacts.profile.user" },
        { $group :{
          _id: "$_id",
            "date": {"$first":  "$date"},
            "approvedTrustedContacts": {"$push":  "$approvedTrustedContacts"},
        }}
      ]);

As an output I have

{
            "_id": "5d76008e4b98e63e58cb34cc",
            "date": "2019-09-09T07:32:20.174Z",
            "approvedTrustedContacts": [
                {
                    "profile": {
                        "_id": "5c5ecb17134fc342d4b1a9d6",
                        "skills": [
                            "test"
                        ],
                        "date": "2019-02-09T12:42:48.969Z",
                        "user": {
                            "_id": "5c5ecaf6134fc342d4b1a9d5",
                            "data": "2019-02-09T12:42:48.716Z",
                            "name": "User",
                            "email": "user@gmail.com",
                        },
                        "handle": "handle",
                        "company": "test",
                    }
                },
                {
                    "profile": {
                        "_id": "5c6496ebae53a8202c963224",
                        "skills": [
                            "qwqwqwqwqw"
                        ],
                        "date": "2019-02-13T22:11:04.119Z",
                        "user": {
                            "_id": "5c64968cae53a8202c963223",
                            "data": "2019-02-13T22:11:03.807Z",
                            "name": "User1",
                            "email": "user1@gmail.com",
                        },
                        "handle": "handle1",
                        "company": "test1",
                    }
                }
            ]
        }
    ```


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM