0

mailbox

mailbox:{ _id:"2", title:"test" }

mailbox_details

mailbox_details:{ _id:"1", mailbox_id:2, is_read : false }

These are the tables.

I have attached the image of code please check. enter image description here

-Unfortunately I did not get the result I expected

Below is the output I intended

output

{ title:"test", mailbox_id:2, is_read : false }.

Once I get this, I can take the next step. please help me.

3
  • You need to check your $match object. you are matching true. This will not fulfill your result. You can use $project to filter the response Commented Jul 20, 2020 at 14:18
  • Thanks @harpreetcheema .I have already tried this. But I did not get the result I expected. db.collection("mailbox_details").aggregate([ { $lookup: { localField: "mailbox_id", from: "mailboxes", foreignField: "_id", as: "mailinfo" } }, { $match : { "mailinfo.is_read" : true } }, { $unwind: "$mailinfo" }, { $project: { "is_read": 1, "mailbox_id": 1, "mailinfo.title": 1, } } ]).toArray((err: any, result: any) => { if (err) { throw err; } console.log("result", result); }); Commented Jul 21, 2020 at 3:53
  • please help me. very urgent Commented Jul 21, 2020 at 9:49

1 Answer 1

1

Try with this. I am using mailbox_details as a parent collection.

db.collection("mailboxes").aggregate([
  { $match : {
      "is_read": true
    }
  },
  { $lookup: { localField: "_id", from: "mailbox_details", foreignField: "mailbox_id", as: "mailinfo"
    }
  },
  { $unwind: "$mailinfo"
  },
  { $project: {
      "is_read": 1,
      "mailbox_id": 1,
      "mailinfo.title": 1,
    }
  }
]).toArray((err: any, result: any) => { if (err) { throw err;
  } console.log("result", result);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @HermenpreetSingh. Let me try and see if it works
Hi @HermenpreetSingh . This is the output of this code. result []. Not Working
@ArunDas Please share your collection data , I will try on my local.

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.