6

I have tried other similar kind of questions available but nothing seems to work for me. I have two collections:

leads:

const mongoose = require("mongoose");
const id = mongoose.Schema.Types.ObjectId;

const leadsSchema = mongoose.Schema(
  {
    _id: id,
    userId: { type: id, ref: "User", required: true },
    leadName: String,
    leads: [
      {
        _id: id,
        name: String,
        status: { type: String, required: false, default: "New" },
        leadActivity: { type: String, required: false, default: "No Campaign Set" },
        headline: { type: String, required: false },
        location: { type: String, required: false },
        leadType: { type: id, ref: "LeadsCategory", required: true },
      }
    ],
    campaignAssociated: {type: id, ref: "campaign"},
  },
  {
    timestamps: true
  }
);

module.exports = mongoose.model("lead", leadsSchema);

leadCategory

const mongoose = require("mongoose");

const leadsCategorySchema = mongoose.Schema(
  {
    _id: mongoose.Schema.Types.ObjectId,
    name: {
      type: String,
      required: false,
    },
    leadsData: [{ type: Array, ref: "lead" }],
  },
  { timestamps: true }
);

module.exports = mongoose.model("LeadsCategory", leadsCategorySchema);

I am trying to reference/populate the name of the lead from leadscategory schema into the leads

exports.get_single_lead_info = (req, res) => {
  const { userId } = req.user;

  const { leadid } = req.body;

  let idToSearch = mongoose.Types.ObjectId(leadid);

  Lead.aggregate([
    {
      $lookup: {from: 'leadscategories', localField: 'leadType', foreignField: 'name', as: 'type as'}
    },
    {
      $match: {
        userId: mongoose.Types.ObjectId(userId),
      },
    },
    {
      $unwind: "$leads",
    },
    {
      $match: {
        "leads._id": idToSearch,
      },
    },
  ])
  .exec(function (err, result) {
    if (err) {
      return res.status(400).json({ message: "Unable to fetch data", err });
    }

    if (!result.length) {
      res.status(404).json("No result found");
    } else {
      res.status(200).json({ message: "Lead info found", result });
    }
  });
};

But it outputs me the lookup result as an empty array everytime:

{
    "message": "Lead info found",
    "result": [
        {
            "_id": "5ece11cbac50c434dc4b7f2c",
            "leadName": "python",
            "leads": {
                "status": "New",
                "leadActivity": "Campaign Set",
                "name": "Hailey",
                "headline": "Machine Learning | Python",
                "location": "New Delhi Area, India",
                "_id": "5ece11cbac50c434dc4b7f29",
                "leadType": "5ebce0f81947df2fd4eb1060"
            },
            "userId": "5eba83d37d4f5533581a7d58",
            "createdAt": "2020-05-27T07:07:55.231Z",
            "updatedAt": "2020-05-27T10:47:42.098Z",
            "__v": 0,
            "type as": [] //<--- Need lead type name associated inside this
        }
    ]
}

Input: "leadid": "5ece11cbac50c434dc4b7f29" Any help appreciated.

3
  • It would be easier to answer if you had supplied input documents. Commented May 27, 2020 at 12:54
  • I see only the result you get, you need to show input sample documents for the related collections. Commented May 27, 2020 at 13:03
  • I have already added complete related codes. Both the schemas plus controller plus output before. Commented May 27, 2020 at 13:08

1 Answer 1

2
[
  {
    $match: {
      userId: mongoose.Types.ObjectId(userId),
    },
  },
  {
    $unwind: "$leads",
  },
  {
    $match: {
      'leads._id': idToSearch,
    },
  },
  {
    $lookup: {
      from: 'leadscategories',
      localField: 'leads.leadType',
      foreignField: '_id',
      as: 'type as'
    }
  },
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.