'Mongodb mongoose join two collection and fetch data

schema:

var UserSchema = new Schema({
  id: { type: String, unique: true },
  username: { type: String, unique: true },
  password: String,
  first_name: String,
  last_name: String,
  email: { type: String, unique: true },
  phone: { type: String, unique: true },
  status: { type: Boolean, default: false },
  role: {
    type: String,
    enum: ["REGULAR", "ADMIN", "SUPER-ADMIN", "OPERATOR"],
    default: "REGULAR",
  },
  tenantRef: { type: Schema.Types.String, ref: "tenant" },
  teamRef: { type: Schema.Types.String, ref: "team" },
  customerRef: { type: Schema.Types.String, ref: "customer" },
  created: { type: Date, default: Date.now },
});


var CustomerSchema = new Schema({
  id: { type: String, unique: true },
  name: String,
  plan: String,
  billing: String,
  status: { type: Boolean, default: true },
  created: { type: Date, default: Date.now },
});

controller:

 userController.getUsers = async function (req, res) {
  const users = await UserSchema.find({}).populate({
     path: "teamRef",
   });
  console.log(users);
   return res.status(200).send(users);
 };

Here i am trying to join user and customer so that i can get customer name along with user data . I am using above way but it is not working. Please take a look how can i do it



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source