0

I have two schema namely Username and UserOrganization schema which is of follows:

const UsernameSchema = new Schema ({
username: {type: String, unique: true},
displayUsername: {type: String},
type: {type: String},
refId: {type: String},
current: {type: Boolean},
forward: {type: Date}
});
module.exports = mongoose.model('Username', UsernameSchema);

const UserOrganizationSchema = new Schema({
userId: {type: String},
orgId: {type: String},
roles: [{type: String}],
});
module.exports = mongoose.model('UserOrganization', UserOrganizationSchema);

here orgId of UserOrganization schema is equal to refId of Username schema. Now when i try the following code:

Username.aggregate([
  {
    $lookup: {
      from: 'UserOrganization.collection.name',
      localField: 'refId',
      foreignField: 'orgId',
      as: 'test',
    },
  }])
  .then((data) => {
    console.log(data);
  })
  .catch((error) => {
    console.log(error)
)}

console.log(data) returns

[{ _id: 5c64f9914c22010ee00fb47f,
 username: 'sub',
 displayUsername: 'sub_syd',
 refId: '5c64f9914c22010ee00fb47c',
 type: 'org',
 current: false,
 __v: 0,
 test: [] }]   

where test field is empty. Is there any problem with my code?
Thanks In Advance.

PS: Solutions from previous similar StackOverflow question did not work for me.

Edit

UserOrganization Collection

{
"_id" : ObjectId("5c663050dfc59c2428a6662a"),
"roles" : [
    "member"
],
"userId" : "5c62600a63c3c52e3135d20b",
"orgId" : "5c628478ca7cc030c86a1a6c",
"__v" : 0
}

Username Collection:

{
"_id" : ObjectId("5c628478ca7cc030c86a1a6e"),
"username" : "test1",
"displayUsername" : "test_1",
"refId" : "5c628478ca7cc030c86a1a6c",
"type" : "organization",
"current" : false,
"__v" : 0
}
Madhavan Sundararaj
  • 267
  • 1
  • 5
  • 17

0 Answers0