How can i join 3 collection with aggregation? - node.js

Here is my structure
country
[
{
"studentid": [
"5bb482fdac231f1354c47aab"
],
"_id": "5bb672fb92c98117d004c733",
"name": "china",
"__v": 0
}
]
users
[
{
"books": [
"5bb657ea05bf6d0ef096e529",
"5bb6580d05bf6d0ef096e52a"
],
"_id": "5bb482fdac231f1354c47aab",
"firstName": "will",
"lastName": "smith",
"email": "user3#gmail.com",
"address": "rajkot",
"salary": 5000,
"department": "electrical",
"__v": 0
}
]
books
[
{
"_id": "5bb657ea05bf6d0ef096e529",
"name": "book 1",
"price": 100,
"pages": 200,
"__v": 0
}
]
Now i am trying to join this 3 collections each array wise
Here is code that i tried
CountryModel.aggregate([
{ $match: { name: country } },
{
$lookup: {
from: UserModel.collection.name,
let: { student_id: "_id" },
pipeline: [
{ "$match": { "_id": "$student_id" } },
{
$lookup: {
from: UserModel.collection.name,
let: { book_id: "$books" },
pipeline: [
{
$lookup: {
from: BookModel.collection.name,
let: { bookid: "$_id" },
pipeline: [
{ "$match": { "$expr": { "$in": ["$book_id", "$bookid"] } } },
],
as: "more"
}
}
],
as: "books"
}
}
],
as: "studentid"
},
},
])
can someone tell me were i am doing wrong ?

Related

How to use aggregation with nested document?

I'm new to mongoose, I'm confuse while create the query. Can you help me?
I have a movie document like this :
"movies": [
{
"id": "635611395a71beb6c5bf0b4d",
"title": "Mission: Impossible - Fallout",
"createdAt": "2022-10-24T04:14:54.445Z",
"cast": [
{
"actor": "635350c581d5b60383f0c4be",
"roleAs": "Ethan Hunt",
"leadActor": true,
"_id": "635658c18b9e50facd7f1fd1"
},
{
"actor": "63560bf55a71beb6c5bf0b1f",
"roleAs": "Ilsa Faust",
"leadActor": false,
"_id": "635658c18b9e50facd7f1fd2"
}
],
"poster": {
"url": "https://res.cloudinary.com/debfn35m1/image/upload/v1666603204/vczxb7lgbonjn8ydsyep.jpg",
"public_id": "vczxb7lgbonjn8ydsyep",
"responsive": [
"https://res.cloudinary.com/debfn35m1/image/upload/c_scale,w_640/v1666603204/vczxb7lgbonjn8ydsyep.jpg",
"https://res.cloudinary.com/debfn35m1/image/upload/c_scale,w_50/v1666603204/vczxb7lgbonjn8ydsyep.jpg"
]
},
"reviews": {
"ratingAvg": "5.0",
"reviewCount": 2
}
},
{
"id": "635614855a71beb6c5bf0bdc",
"title": "Spider-Man: No Way Home",
"createdAt": "2022-10-24T04:28:58.286Z",
"cast": [
{
"actor": "635350a881d5b60383f0c4b8",
"roleAs": "Peter Parker",
"leadActor": true,
"_id": "636a2d6520cf4cf14a11ef95"
},
{
"actor": "635611eb5a71beb6c5bf0b99",
"roleAs": "MJ",
"leadActor": false,
"_id": "636a2d6520cf4cf14a11ef96"
}
],
"poster": {
"url": "https://res.cloudinary.com/debfn35m1/image/upload/v1667902823/tajzr9hpulvzqoytgpxu.jpg",
"public_id": "tajzr9hpulvzqoytgpxu",
"responsive": [
"https://res.cloudinary.com/debfn35m1/image/upload/c_scale,w_640/v1667902823/tajzr9hpulvzqoytgpxu.jpg",
"https://res.cloudinary.com/debfn35m1/image/upload/c_scale,w_470/v1667902823/tajzr9hpulvzqoytgpxu.jpg",
"https://res.cloudinary.com/debfn35m1/image/upload/c_scale,w_50/v1667902823/tajzr9hpulvzqoytgpxu.jpg"
]
},
"reviews": {
"ratingAvg": "8.0",
"reviewCount": 2
}
},
This is my desired result:
{
"id": "635611395a71beb6c5bf0b4d",
"title": "Mission: Impossible - Fallout",
"createdAt": "2022-10-24T04:14:54.445Z",
"cast": [
{
"actor": "635350c581d5b60383f0c4be",
"roleAs": "Ethan Hunt",
"leadActor": true,
"_id": "635658c18b9e50facd7f1fd1"
},
{
"actor": "63560bf55a71beb6c5bf0b1f",
"roleAs": "Ilsa Faust",
"leadActor": false,
"_id": "635658c18b9e50facd7f1fd2"
}
],
"poster": {
"url": "https://res.cloudinary.com/debfn35m1/image/upload/v1666603204/vczxb7lgbonjn8ydsyep.jpg",
"public_id": "vczxb7lgbonjn8ydsyep",
"responsive": [
"https://res.cloudinary.com/debfn35m1/image/upload/c_scale,w_640/v1666603204/vczxb7lgbonjn8ydsyep.jpg",
"https://res.cloudinary.com/debfn35m1/image/upload/c_scale,w_50/v1666603204/vczxb7lgbonjn8ydsyep.jpg"
]
},
"reviews": {
"ratingAvg": "5.0",
"reviewCount": 2
}
},
Im trying to write a filter function to get all movie that has Tom Cruise with actorId as a parameter but cannot get the result.
Can someone please help me with this ?
Since your actor id is inside a cast array with key actor you must specify your key while match, check the updated code below
exports.actorAggregation = (actorId) => {
return [
{
$lookup: {
from: "Movie",
localField: "cast",
foreignField: "_id",
as: "actorFilter",
},
},
{ $unwind: "$cast" },
{
$match: {
"cast.actor._id": { $in: [actorId] }, // or "cast.actor": { $in: [actorId] },
status: { $eq: "public" },
},
},
{
$project: {
title: 1,
poster: "$poster.url",
responsivePosters: "$poster.responsive",
createdAt: "$createdAt",
},
},
];
};

Mongo Query to get a result

My mongo collection name tests and whose having the following documents in it.
[
{
"title": "One",
"uid": "1",
"_metadata": {
"references": [
{
"uid": "2"
},
{
"asssetuid": 10
}
]
}
},
{
"title": "Two",
"uid": "2",
"_metadata": {
"references": [
{
"uid": "3"
},
{
"asssetuid": 11
}
]
}
},
{
"title": "Three",
"uid": "3",
"_metadata": {
"references": []
}
}
]
And I want the result in the following format (for uid:1)
[
{
"title": "One",
"uid": 1,
"_metadata": {
"references": [
{
"asssetuid": 10
},
{
"asssetuid": 11
},
{
"title": "Two",
"uid": "2",
"_metadata": {
"references": [
{
"title": "Three",
"uid": "3"
}
]
}
}
]
}
}
]
for uid:2 I want the following result
[
{
"title": "Two",
"uid": 2,
"_metadata": {
"references": [
{
"asssetuid": 11
},
{
"title": "Three",
"uid": "3"
}
]
}
}
]
Which query I used here to get a respected result. according to its uid. here I want the result in the parent-child relationship. is this possible using MongoDB graph lookup query or any other query that we can use to get the result. Please help me with this.
New Type Output
[{
"title": "One",
"uid": 1,
"_metadata": {
"assets": [{
"asssetuid": 10,
"parent": 1
}, {
"asssetuid": 11,
"parent": 2
}],
"entries": [{
"title": "Two",
"uid": "2",
"parent": 1
}, {
"title": "Three",
"uid": "3",
"parent": 2
}]
}
}]
Mongo supports the automatic reference resolution using $ref but for that, you need to change your schema a little and resolve resolution is only supported by some drivers.
You need to store your data in this format:
[
...
{
"_id": ObjectId("5a934e000102030405000000"),
"_metadata": {
"references": [
{
"$ref": "collection",
"$id": ObjectId("5a934e000102030405000001"),
"$db": "database"
},
{
"asssetuid": 10
}
]
},
"title": "One",
"uid": "1"
},
....
]
For more details on $ref refer to official documentation: label-document-references
OR
you can resolve the reference using the $graphLookup but the only problem with the $graphlookup is that you will lose the assetuid. Here is the query and it will resolve references and give output in flat map
db.collection.aggregate([
{
$match: {
uid: "1"
}
},
{
$graphLookup: {
from: "collection",
startWith: "$_metadata.references.uid",
connectFromField: "_metadata.references.uid",
connectToField: "uid",
depthField: "depth",
as: "resolved"
}
},
{
"$addFields": {
"references": "$resolved",
"metadata": [
{
"_metadata": "$_metadata"
}
]
}
},
{
"$project": {
"references._metadata": 0,
}
},
{
"$project": {
"references": "$references",
"merged": {
"$concatArrays": [
"$metadata",
"$resolved"
]
}
}
},
{
"$project": {
results: [
{
merged: "$merged"
},
{
references: "$references"
}
]
}
},
{
"$unwind": "$results"
},
{
"$facet": {
"assest": [
{
"$match": {
"results.merged": {
"$exists": true
}
}
},
{
"$unwind": "$results.merged"
},
{
"$unwind": "$results.merged._metadata.references"
},
{
"$match": {
"results.merged._metadata.references.asssetuid": {
"$exists": true
}
}
},
{
"$project": {
_id: 0,
"asssetuid": "$results.merged._metadata.references.asssetuid"
}
}
],
"uid": [
{
"$match": {
"results.references": {
"$exists": true
}
}
},
{
"$unwind": "$results.references"
},
{
$replaceRoot: {
newRoot: "$results.references"
}
}
]
}
},
{
"$project": {
"references": {
"$concatArrays": [
"$assest",
"$uid"
]
}
}
}
])
Here is the link to the playground to test it: Mongo Playground

Mongodb, mongoose - Sorting by _id and date using aggregate and group

I am trying to sort by the task._id & date in desc order. I am able to sort by task._id but sortibg bydate doesnt work, I tried changing the order in aggregate still no luck. I get the response but just the order by usertasks were added in collection and not by the usertask.date
User(name, address, etc)
Task(name, icon, assignee)
UserTask(User.ObjectId, Task.ObjectId, date)
User Collection:
{
"users": [
{
"name": "Bill",
"phone": "345"
},
{
"name": "Steve",
"phone": "123"
},
{
"name": "Elon",
"phone": "567"
}
]
}
Task collection:
{
"tasks": [
{
"name": "Run 100m",
"icon": "run",
"assignee": "Elon"
},
{
"name": "Walk 1 hour",
"icon": "walk",
"assignee": "Bill"
},
{
"name": "Jog 30 minutes",
"icon": "jog",
"assignee": "Steve"
}
]
}
UserTasks:
{
"_id": "5e72fec..",
"user": "5e72fa4..",
"task": "5e72fbac..",
"date": "2020-03-03T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:37.027Z",
"updatedAt": "2020-03-19T05:10:37.027Z",
"__v": 0
},
{
"_id": "5e72fed3..",
"user": "5e72fa4e..",
"task": "5e72fbac..",
"date": "2020-03-12T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:43.296Z",
"updatedAt": "2020-03-19T05:10:43.296Z",
"__v": 0
},
{
"_id": "5e72fed6..",
"user": "5e72fa..",
"task": "5e72fb..",
"date": "2020-03-15T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:46.057Z",
"updatedAt": "2020-03-19T05:10:46.057Z",
"__v": 0
},
{
"_id": "5e72feda...",
"user": "5e72fa4..",
"task": "5e72fb..",
"date": "2020-03-07T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:50.785Z",
"updatedAt": "2020-03-19T05:10:50.785Z",
"__v": 0
}
This is the Aggregate that needs changing
UserTask.aggregate([
{
$lookup: {
from: "tasks",
localField: "task",
foreignField: "_id",
as: "matchedTask"
}
},
{
$unwind: "$matchedTask"
},
{
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "matchedUser"
}
},
{
$unwind: "$matchedUser"
},
{
$group: {
_id: "$matchedTask._id",
name: {$first: "$matchedTask.name"},
icon: {$first: "$matchedTask.icon"},
assignee: { $first: "$matchedTask.assignee" },
userdata: {
$push: {
name: "$matchedUser.name",
date: "$date"
}
}
}
},
{
$sort: { _id: 1, "userdata.date": -1 }
}
])
.exec()
.then(doc => res.status(200).json(doc))
.catch(err => res.status(400).json("Error: " + err));
The response is shown below, please note the usertask.date. it is NOT sorted
{
"_id": "5e...",
"name": "Run 100m",
"icon": "run",
"assignee": "Elon",
"userdata": [
{
"name": "Elon",
"date": "2020-03-21T20:02:38.143Z"
},
{
"name": "Bill",
"date": "2020-03-11T20:02:38.000Z"
},
{
"name": "Steve",
"date": "2020-03-19T20:02:38.000Z"
}
]
}
As you can see the it is not sorted by date - desc order. The result should be like shown below
"userdata": [
{
"name": "Elon",
"date": "2020-03-21T20:02:38.143Z"
},
{
"name": "Steve",
"date": "2020-03-19T20:02:38.000Z"
},
{
"name": "Bill",
"date": "2020-03-11T20:02:38.000Z"
}
]
$sort will use the last object which comes out from the aggregate pipe and theres no field "date" in this object:
{
$group: {
_id: "$matchedTask._id",
name: {$first: "$matchedTask.name"},
icon: {$first: "$matchedTask.icon"},
assignee: { $first: "$matchedTask.assignee" },
userdata: {
$push: {
name: "$matchedUser.name",
execDate: "$date"
}
}
}
},
your group has to returned a field named date in order to be able to sort it
{
$group: {
_id: "$matchedTask._id",
name: ....,
date: ....
}
}
{ $sort: {date: -1}}
if the value you want to sort is indide another object you must specify it on sort:
{$sort: {"userdata.date": -1}}
I fixed it, had to use sort two times, now I am able to get the result as I want
Solution provided below
UserTask.aggregate([
{
$lookup: {
from: "tasks",
localField: "task",
foreignField: "_id",
as: "matchedTask"
}
},
{
$unwind: "$matchedTask"
},
{
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "matchedUser"
}
},
{
$unwind: "$matchedUser"
},
{
$sort: { date: -1 }
},
{
$group: {
_id: "$matchedTask._id",
name: { $first: "$matchedTask.name" },
icon: { $first: "$matchedTask.icon" },
assignee: { $first: "$matchedTask.assignee" },
userdata: {
$push: {
name: "$matchedUser.name",
execDate: "$date"
}
}
}
},
{
$sort: { _id: 1 }
}
])
.exec()
.then(doc => res.status(200).json(doc))
.catch(err => res.status(400).json("Error: " + err));

MongoDB $lookup on one document's array of object

I have searched online but could not find any match my case. Here is the situation.
I am using aggregate to combine one collection and one document which is from another collection together
restaurants.aggregate([
{
$match: {
_id: {
$in: idList
}
}
},
{
$lookup: {
from: "tags",
localField: "details.restaurantType",
foreignField: "details.restaurantType._id",
as: "types"
}
},
{
$project: {
restaurantName: "$details.restaurantName",
restaurantType: "$details.restaurantType",
type: {
$filter: {
input: "$types",
as: "type",
cond: {
$eq: ["$$type._id", "$details.restaurantType"]
}
}
},
currency: "$details.currency",
costPerPax: "$details.costPerPax"
}
}
]);
current result
The 'type' field in my current result is [], I need a matched value instead
[
{
"id": "5c20c7a0036dda80a8baabcc",
"restaurantName": "Villagio Restaurant Sutera Mall",
"type": [],
"currency": "RM",
"costPerPax": 22,
},
{
"id": "5c20ceb07715216d3c217b7a",
"restaurantName": "Thai Food Thai Now Sutera Mall",
"type": [],
"currency": "RM",
"costPerPax": 16,
}
]
expected result
I need the 'type' fields has match tag name from another collection like this
[
{
"id": "5c20c7a0036dda80a8baabcc",
"restaurantName": "Villagio Restaurant Sutera Mall",
"type": "Western",
"currency": "RM",
"costPerPax": 22,
},
{
"id": "5c20ceb07715216d3c217b7a",
"restaurantName": "Thai Food Thai Now Sutera Mall",
"type": "Thai",
"currency": "RM",
"costPerPax": 16,
}
]
Extra Information
two document from restaurants collection
{
"details": {
"restaurantName": "Villagio Restaurant Sutera Mall",
"restaurantType": "5c01fb57497a896d50f498a8"
},
"_id": "5c20c7a0036dda80a8baabcc",
"status": "OP",
"__v": 0
},
{
"details": {
"restaurantName": "Kingshahi Japanese Shop",
"restaurantType": "5c01fb57497a896d50f49879"
},
"_id": "5c20cb4fb7e75180480690c2",
"status": "OP",
"__v": 0
}
One document from tag collection
{
"_id": "5c01fb57497a896d50f49876",
"details": {
"restaurantTypeId": "5c01fb57497a896d50f49877",
"restaurantTypes": [
{
"_id": "5c01fb57497a896d50f49879",
"name": "Asian",
"counter": 1
},
{
"_id": "5c01fb57497a896d50f4987a",
"name": "Bakery",
"counter": 0
}
]
}
}
You can use below optimised aggregation pipeline
db.restaurants.aggregate([
{ "$lookup": {
"from": "tags",
"let": { "restaurantType": "$details.restaurantType" },
"pipeline": [
{ "$match": {
"$expr": { "$in": ["$$restaurantType", "$details.restaurantTypes._id"] }
}},
{ "$unwind": "$details.restaurantTypes" },
{ "$match": {
"$expr": { "$eq": ["$details.restaurantTypes._id", "$$restaurantType"] }
}}
],
"as": "types"
}},
{ "$project": {
"restaurantName": "$details.restaurantName",
"restaurantType": "$details.restaurantType",
"type": { "$arrayElemAt": ["$types.details.restaurantTypes.name", 0] },
"currency": "$details.currency",
"costPerPax": "$details.costPerPax"
}}
])

Mongodb apply condition on $lookup fetched data

I am having data from two different collections on which I am applying $lookup with $match which is working fine. What I am trying to do is on lookup's result I am applying $match again which is having some conditions. Here is the collection of User on which I am applying lookup.
Users collection :-
{
"_id": {
"$oid": "5b714631faaae220d7cc07cf"
},
"name": "abc",
"surname": "xyz",
"email": "abc#gmail.com",
"hotel_data": [
{
"location": {
"type": "Point",
"coordinates": [
30.7052881,
76.84470799999997
]
},
"_id": {
"$oid": "5b7fb8559849fd485dc47240"
},
"status": true,
"activityname": "Sparrow",
},
{
"location": {
"type": "Point",
"coordinates": [
30.733315,
76.779419
]
},
"_id": {
"$oid": "5b7f9ecb9960053dac7ce6f1"
},
"status": true,
"activityname": "Raj Hotel",
},
]
}
and this is my availabilities collection on which I am applying $lookup with $match.
{
"_id": {
"$oid": "5b867766d63c4e2cdd5534d2"
},
"businessid": {
"$oid": "5b7fb8559849fd485dc47240"
},
"userid": {
"$oid": "5b714631faaae220d7cc07cf"
},
"hotel_filters": [
{
"_id": {
"$oid": "5b867766d63c4e2cdd5534d3"
},
"hotelservice": [
{
"service_id": "5b472fff25556c3f02a875aa",
"service_name": "Pool",
"_id": {
"$oid": "5b867767d63c4e2cdd5534d7"
}
},
{
"service_id": "5b472fdb25556c3f02a875a9",
"service_name": "AIR",
"_id": {
"$oid": "5b867767d63c4e2cdd5534d8"
}
}
],
"location_type": [
{
"locationtype_id": "5b18f4d08c63f42019763b12",
"locationtype_name": "Scenic View",
"_id": {
"$oid": "5b867767d63c4e2cdd5534d5"
}
},
{
"locationtype_id": "5b18f4e38c63f42019763b13",
"locationtype_name": "Central",
"_id": {
"$oid": "5b867767d63c4e2cdd5534d6"
}
}
],
"hotel_type": [
{
"hoteltype_id": "5b081452edefe23318834a28",
"hoteltype_name": "3 Star",
"_id": {
"$oid": "5b867767d63c4e2cdd5534d4"
}
}
]
}
]
}
and what I am trying to do is- after $lookup I only want those availabilities data which are having "hotelservice.service_id" and "location_type.locationtype_id" that I will pass. And I have tried this by applying this query to it(In this query I have only applied "hotelservice.service_id" but later on I also want to apply query with $and on "location_type.locationtype_id").
User.aggregate([
{
$match:{
"hotel_data.location": {
"$geoWithin": {
"$centerSphere": [
[30.7052881, 76.84470799999997], 50/ 6371
]
}
}
}
},{
"$unwind": "$hotel_data"
},
{
$match:{
"hotel_data.location": {
"$geoWithin": {
"$centerSphere": [
[30.7052881, 76.84470799999997], 50/ 6371
]
}
}
}
},
{
$lookup: {
from: "availabilities",
localField: "hotel_data._id",
foreignField: "businessid",
as: "availabilitiesdata"
}
},
{$match:{$and: [{"availabilitiesdata.hotel_filters.hotelservice.service_id":{$in:[5b472fb725556c3f02a875a8]}}]}}
], function(err, data) {
if (err) {
return res.send({data: err, status: false, msg:"No Hotel Found" });
}else{
return res.send({status: true, msg:"Hotel Found", data:data });
}
});
This query gives me result as follow.
{
"status": true,
"msg": "Hotel Found",
"data": [
{
"_id": "5b714631faaae220d7cc07cf",
"name": "abc",
"surname": "xyz",
"email": "abc#gmail.com",
"hotel_data": {
"location": {
"type": "Point",
"coordinates": [
30.7052881,
76.84470799999997
]
},
"_id": "5b7fb8559849fd485dc47240",
"status": true,
"activityname": "Sparrow",
},
"availabilitiesdata": [
{
"_id": "5b864fe68ab0b71f4f28021e",
"businessid": "5b7fb8559849fd485dc47240",
"userid": "5b714631faaae220d7cc07cf",
"hotel_filters": [
{
"_id": "5b864fe78ab0b71f4f28021f",
"hotelservice": [
{
"service_id": "5b472fb725556c3f02a875a8",
"service_name": "Parking",
"_id": "5b864fe78ab0b71f4f280223"
},
{
"service_id": "5b472fff25556c3f02a875aa",
"service_name": "Pool",
"_id": "5b864fe78ab0b71f4f280224"
}
],
"location_type": [
{
"locationtype_id": "5b18f4798c63f42019763b11",
"locationtype_name": "Quiet",
"_id": "5b864fe78ab0b71f4f280221"
},
{
"locationtype_id": "5b18f4e38c63f42019763b13",
"locationtype_name": "Central",
"_id": "5b864fe78ab0b71f4f280222"
}
],
"hotel_type": [
{
"hoteltype_id": "5b0813e2edefe23318834a27",
"hoteltype_name": "5 Star",
"_id": "5b864fe78ab0b71f4f280220"
}
]
}
]
},
{
"_id": "5b867766d63c4e2cdd5534d2",
"businessid": "5b7fb8559849fd485dc47240",
"userid": "5b714631faaae220d7cc07cf",
"hotel_filters": [
{
"_id": "5b867766d63c4e2cdd5534d3",
"hotelservice": [
{
"service_id": "5b472fff25556c3f02a875aa",
"service_name": "Pool",
"_id": "5b867767d63c4e2cdd5534d7"
},
{
"service_id": "5b472fdb25556c3f02a875a9",
"service_name": "AIR",
"_id": "5b867767d63c4e2cdd5534d8"
}
],
"location_type": [
{
"locationtype_id": "5b18f4d08c63f42019763b12",
"locationtype_name": "Scenic View",
"_id": "5b867767d63c4e2cdd5534d5"
},
{
"locationtype_id": "5b18f4e38c63f42019763b13",
"locationtype_name": "Central",
"_id": "5b867767d63c4e2cdd5534d6"
}
],
"hotel_type": [
{
"hoteltype_id": "5b081452edefe23318834a28",
"hoteltype_name": "3 Star",
"_id": "5b867767d63c4e2cdd5534d4"
}
]
}
]
}
]
}
]}
but what I want is something like this.
{
"status": true,
"msg": "Hotel Found",
"data": [
{
"_id": "5b714631faaae220d7cc07cf",
"name": "abc",
"surname": "xyz",
"email": "abc#gmail.com",
"hotel_data": {
"location": {
"type": "Point",
"coordinates": [
30.7052881,
76.84470799999997
]
},
"_id": "5b7fb8559849fd485dc47240",
"status": true,
"activityname": "Sparrow",
},
"availabilitiesdata": [
{
"_id": "5b864fe68ab0b71f4f28021e",
"businessid": "5b7fb8559849fd485dc47240",
"userid": "5b714631faaae220d7cc07cf",
"hotel_filters": [
{
"_id": "5b864fe78ab0b71f4f28021f",
"hotelservice": [
{
"service_id": "5b472fb725556c3f02a875a8",
"service_name": "Parking",
"_id": "5b864fe78ab0b71f4f280223"
},
{
"service_id": "5b472fff25556c3f02a875aa",
"service_name": "Pool",
"_id": "5b864fe78ab0b71f4f280224"
}
],
"location_type": [
{
"locationtype_id": "5b18f4798c63f42019763b11",
"locationtype_name": "Quiet",
"_id": "5b864fe78ab0b71f4f280221"
},
{
"locationtype_id": "5b18f4e38c63f42019763b13",
"locationtype_name": "Central",
"_id": "5b864fe78ab0b71f4f280222"
}
],
"hotel_type": [
{
"hoteltype_id": "5b0813e2edefe23318834a27",
"hoteltype_name": "5 Star",
"_id": "5b864fe78ab0b71f4f280220"
}
]
}
]
}
]
}
]}
I want only those availabilities which match the condition on my $lookup availability data. can someone help me out.
You can use $lookup pipeline variant to apply the $match inside the joined collection in 3.6
Something like ( replace $lookup & $match stage with below lookup pipeline )
{"$lookup":{
"from":"availabilities",
"let":{"hotel_data_id":"$hotel_data._id"},
"pipeline":[
{"$match":{
"hotel_filters.hotelservice.service_id":{"$in":["5b472fb725556c3f02a875a8"]},
"hotel_filters.location_type.locationtype_id":{"$in":["5b18f4798c63f42019763b11"]},
"$expr":{"$eq":["$$hotel_data_id","$businessid"]}
}}
],
"as":"availabilitiesdata"
}}

Resources