3 table joins in mongodb












0















Considering the below SQL query, when i need to achieve data values from 3 different tables. i am simulating requirement in SQL as



create table h(id int, hname varchar(20));
create table r(id int, rname varchar(20), hid int, facid int);
create table f(id int, fname varchar(20));

insert into h values(1, 'Hotel1');
insert into h values(2, 'Hotel2');

insert into r values(1, 'room1', 1, 1);
insert into r values(1, 'room1', 1, 2);
insert into r values(1, 'room1', 1, 3);
insert into r values(2, 'room2', 1, 1);
insert into r values(2, 'room2', 1, 2);

insert into f values(1, 'fac1');
insert into f values(2, 'fac2');
insert into f values(3, 'fac3');

select rname, facid, hname from r
inner join f
on f.id = r.facid
inner join h
on r.hid= h.id


I am getting as:



room1|1|Hotel1
room1|2|Hotel1
room1|3|Hotel1
room2|1|Hotel1
room2|2|Hotel1


Now, i require to have the same understanding built in MongoDB. i tried 2 collection join as



rooms.aggregate([ {   "$match" : { "_id" : ObjectId("5c11fb6fc7daa1c774fb4e67"),  }
},
{
"$lookup":
{
"from": "facility",
"localField": "facilities",
"foreignField": "_id",
"as": "facilities"
}

},
{ "$project" : {"facilities":1}}
])



How can i add another collection to this query to get values for each
facilities for a room in a hotel.




Facilites:



{
"_id" : ObjectId("5123123123123scsd2"),
"facility_price" : 2132,
"facility_serves" : 2,
"facility_name" : "Mini Bar",
"facility_type" : "Room Related",
"facility_desc" : "Minibar for in room fine dine."
}


Rooms:



{
"_id" : ObjectId("234234jhd98234jhfw23"),
"room_len" : 10,
"room_name" : "Single",
"hotel_id" : ObjectId("5c01323213123295a"),
"facilities" : [
ObjectId("5123123123123scsd2"),
ObjectId("58237h9a8723h4239a")
],
"room_desc" : "good room for family and friends",
"room_capacity" : 6,
"room_price" : 1234,
"total_rooms" : 13,
"room_brd" : 12,
"room_type" : "Executive Suite"
}


Hotel:



{
"_id" : ObjectId("5c01323213123295a"),
"estd" : "20160808",
"rating" : "4",
"long_desc" : "Hotel Name is situated in posh locality of ,
"chain" : "",
"locality" : "good locality",
"country" : "country code",
"hoteladdress" : "some hotel address",
"hoteltype" : "family",
"short_desc" : "This elegant 4-star boutique hotel is just a 5-minute walk ",
"longitude" : "74.82260099999996",
"plus_code" : "8J6P3R8F+V2",
"city_center_distance" : 6,
"hotelname" : "Hotel Name id",
"floors" : 3,
"postal_code" : 122321,
"rooms" : 23.,
"latitude" : "12.312",
"airport_distance" : 14,
"city" : "SCS",
"phno" : NumberLong(4234234234)
}









share|improve this question

























  • Could you post the sample documents from all the collections

    – Anthony Winzlet
    Jan 3 at 6:17











  • added sample collection document structure @AnthonyWinzlet

    – Saqib Mujtaba
    Jan 7 at 7:31











  • I think the answer provided below is correct what is the issue?

    – Anthony Winzlet
    Jan 7 at 17:25
















0















Considering the below SQL query, when i need to achieve data values from 3 different tables. i am simulating requirement in SQL as



create table h(id int, hname varchar(20));
create table r(id int, rname varchar(20), hid int, facid int);
create table f(id int, fname varchar(20));

insert into h values(1, 'Hotel1');
insert into h values(2, 'Hotel2');

insert into r values(1, 'room1', 1, 1);
insert into r values(1, 'room1', 1, 2);
insert into r values(1, 'room1', 1, 3);
insert into r values(2, 'room2', 1, 1);
insert into r values(2, 'room2', 1, 2);

insert into f values(1, 'fac1');
insert into f values(2, 'fac2');
insert into f values(3, 'fac3');

select rname, facid, hname from r
inner join f
on f.id = r.facid
inner join h
on r.hid= h.id


I am getting as:



room1|1|Hotel1
room1|2|Hotel1
room1|3|Hotel1
room2|1|Hotel1
room2|2|Hotel1


Now, i require to have the same understanding built in MongoDB. i tried 2 collection join as



rooms.aggregate([ {   "$match" : { "_id" : ObjectId("5c11fb6fc7daa1c774fb4e67"),  }
},
{
"$lookup":
{
"from": "facility",
"localField": "facilities",
"foreignField": "_id",
"as": "facilities"
}

},
{ "$project" : {"facilities":1}}
])



How can i add another collection to this query to get values for each
facilities for a room in a hotel.




Facilites:



{
"_id" : ObjectId("5123123123123scsd2"),
"facility_price" : 2132,
"facility_serves" : 2,
"facility_name" : "Mini Bar",
"facility_type" : "Room Related",
"facility_desc" : "Minibar for in room fine dine."
}


Rooms:



{
"_id" : ObjectId("234234jhd98234jhfw23"),
"room_len" : 10,
"room_name" : "Single",
"hotel_id" : ObjectId("5c01323213123295a"),
"facilities" : [
ObjectId("5123123123123scsd2"),
ObjectId("58237h9a8723h4239a")
],
"room_desc" : "good room for family and friends",
"room_capacity" : 6,
"room_price" : 1234,
"total_rooms" : 13,
"room_brd" : 12,
"room_type" : "Executive Suite"
}


Hotel:



{
"_id" : ObjectId("5c01323213123295a"),
"estd" : "20160808",
"rating" : "4",
"long_desc" : "Hotel Name is situated in posh locality of ,
"chain" : "",
"locality" : "good locality",
"country" : "country code",
"hoteladdress" : "some hotel address",
"hoteltype" : "family",
"short_desc" : "This elegant 4-star boutique hotel is just a 5-minute walk ",
"longitude" : "74.82260099999996",
"plus_code" : "8J6P3R8F+V2",
"city_center_distance" : 6,
"hotelname" : "Hotel Name id",
"floors" : 3,
"postal_code" : 122321,
"rooms" : 23.,
"latitude" : "12.312",
"airport_distance" : 14,
"city" : "SCS",
"phno" : NumberLong(4234234234)
}









share|improve this question

























  • Could you post the sample documents from all the collections

    – Anthony Winzlet
    Jan 3 at 6:17











  • added sample collection document structure @AnthonyWinzlet

    – Saqib Mujtaba
    Jan 7 at 7:31











  • I think the answer provided below is correct what is the issue?

    – Anthony Winzlet
    Jan 7 at 17:25














0












0








0








Considering the below SQL query, when i need to achieve data values from 3 different tables. i am simulating requirement in SQL as



create table h(id int, hname varchar(20));
create table r(id int, rname varchar(20), hid int, facid int);
create table f(id int, fname varchar(20));

insert into h values(1, 'Hotel1');
insert into h values(2, 'Hotel2');

insert into r values(1, 'room1', 1, 1);
insert into r values(1, 'room1', 1, 2);
insert into r values(1, 'room1', 1, 3);
insert into r values(2, 'room2', 1, 1);
insert into r values(2, 'room2', 1, 2);

insert into f values(1, 'fac1');
insert into f values(2, 'fac2');
insert into f values(3, 'fac3');

select rname, facid, hname from r
inner join f
on f.id = r.facid
inner join h
on r.hid= h.id


I am getting as:



room1|1|Hotel1
room1|2|Hotel1
room1|3|Hotel1
room2|1|Hotel1
room2|2|Hotel1


Now, i require to have the same understanding built in MongoDB. i tried 2 collection join as



rooms.aggregate([ {   "$match" : { "_id" : ObjectId("5c11fb6fc7daa1c774fb4e67"),  }
},
{
"$lookup":
{
"from": "facility",
"localField": "facilities",
"foreignField": "_id",
"as": "facilities"
}

},
{ "$project" : {"facilities":1}}
])



How can i add another collection to this query to get values for each
facilities for a room in a hotel.




Facilites:



{
"_id" : ObjectId("5123123123123scsd2"),
"facility_price" : 2132,
"facility_serves" : 2,
"facility_name" : "Mini Bar",
"facility_type" : "Room Related",
"facility_desc" : "Minibar for in room fine dine."
}


Rooms:



{
"_id" : ObjectId("234234jhd98234jhfw23"),
"room_len" : 10,
"room_name" : "Single",
"hotel_id" : ObjectId("5c01323213123295a"),
"facilities" : [
ObjectId("5123123123123scsd2"),
ObjectId("58237h9a8723h4239a")
],
"room_desc" : "good room for family and friends",
"room_capacity" : 6,
"room_price" : 1234,
"total_rooms" : 13,
"room_brd" : 12,
"room_type" : "Executive Suite"
}


Hotel:



{
"_id" : ObjectId("5c01323213123295a"),
"estd" : "20160808",
"rating" : "4",
"long_desc" : "Hotel Name is situated in posh locality of ,
"chain" : "",
"locality" : "good locality",
"country" : "country code",
"hoteladdress" : "some hotel address",
"hoteltype" : "family",
"short_desc" : "This elegant 4-star boutique hotel is just a 5-minute walk ",
"longitude" : "74.82260099999996",
"plus_code" : "8J6P3R8F+V2",
"city_center_distance" : 6,
"hotelname" : "Hotel Name id",
"floors" : 3,
"postal_code" : 122321,
"rooms" : 23.,
"latitude" : "12.312",
"airport_distance" : 14,
"city" : "SCS",
"phno" : NumberLong(4234234234)
}









share|improve this question
















Considering the below SQL query, when i need to achieve data values from 3 different tables. i am simulating requirement in SQL as



create table h(id int, hname varchar(20));
create table r(id int, rname varchar(20), hid int, facid int);
create table f(id int, fname varchar(20));

insert into h values(1, 'Hotel1');
insert into h values(2, 'Hotel2');

insert into r values(1, 'room1', 1, 1);
insert into r values(1, 'room1', 1, 2);
insert into r values(1, 'room1', 1, 3);
insert into r values(2, 'room2', 1, 1);
insert into r values(2, 'room2', 1, 2);

insert into f values(1, 'fac1');
insert into f values(2, 'fac2');
insert into f values(3, 'fac3');

select rname, facid, hname from r
inner join f
on f.id = r.facid
inner join h
on r.hid= h.id


I am getting as:



room1|1|Hotel1
room1|2|Hotel1
room1|3|Hotel1
room2|1|Hotel1
room2|2|Hotel1


Now, i require to have the same understanding built in MongoDB. i tried 2 collection join as



rooms.aggregate([ {   "$match" : { "_id" : ObjectId("5c11fb6fc7daa1c774fb4e67"),  }
},
{
"$lookup":
{
"from": "facility",
"localField": "facilities",
"foreignField": "_id",
"as": "facilities"
}

},
{ "$project" : {"facilities":1}}
])



How can i add another collection to this query to get values for each
facilities for a room in a hotel.




Facilites:



{
"_id" : ObjectId("5123123123123scsd2"),
"facility_price" : 2132,
"facility_serves" : 2,
"facility_name" : "Mini Bar",
"facility_type" : "Room Related",
"facility_desc" : "Minibar for in room fine dine."
}


Rooms:



{
"_id" : ObjectId("234234jhd98234jhfw23"),
"room_len" : 10,
"room_name" : "Single",
"hotel_id" : ObjectId("5c01323213123295a"),
"facilities" : [
ObjectId("5123123123123scsd2"),
ObjectId("58237h9a8723h4239a")
],
"room_desc" : "good room for family and friends",
"room_capacity" : 6,
"room_price" : 1234,
"total_rooms" : 13,
"room_brd" : 12,
"room_type" : "Executive Suite"
}


Hotel:



{
"_id" : ObjectId("5c01323213123295a"),
"estd" : "20160808",
"rating" : "4",
"long_desc" : "Hotel Name is situated in posh locality of ,
"chain" : "",
"locality" : "good locality",
"country" : "country code",
"hoteladdress" : "some hotel address",
"hoteltype" : "family",
"short_desc" : "This elegant 4-star boutique hotel is just a 5-minute walk ",
"longitude" : "74.82260099999996",
"plus_code" : "8J6P3R8F+V2",
"city_center_distance" : 6,
"hotelname" : "Hotel Name id",
"floors" : 3,
"postal_code" : 122321,
"rooms" : 23.,
"latitude" : "12.312",
"airport_distance" : 14,
"city" : "SCS",
"phno" : NumberLong(4234234234)
}






sql mongodb join






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 7 at 7:30







Saqib Mujtaba

















asked Jan 2 at 16:47









Saqib MujtabaSaqib Mujtaba

356214




356214













  • Could you post the sample documents from all the collections

    – Anthony Winzlet
    Jan 3 at 6:17











  • added sample collection document structure @AnthonyWinzlet

    – Saqib Mujtaba
    Jan 7 at 7:31











  • I think the answer provided below is correct what is the issue?

    – Anthony Winzlet
    Jan 7 at 17:25



















  • Could you post the sample documents from all the collections

    – Anthony Winzlet
    Jan 3 at 6:17











  • added sample collection document structure @AnthonyWinzlet

    – Saqib Mujtaba
    Jan 7 at 7:31











  • I think the answer provided below is correct what is the issue?

    – Anthony Winzlet
    Jan 7 at 17:25

















Could you post the sample documents from all the collections

– Anthony Winzlet
Jan 3 at 6:17





Could you post the sample documents from all the collections

– Anthony Winzlet
Jan 3 at 6:17













added sample collection document structure @AnthonyWinzlet

– Saqib Mujtaba
Jan 7 at 7:31





added sample collection document structure @AnthonyWinzlet

– Saqib Mujtaba
Jan 7 at 7:31













I think the answer provided below is correct what is the issue?

– Anthony Winzlet
Jan 7 at 17:25





I think the answer provided below is correct what is the issue?

– Anthony Winzlet
Jan 7 at 17:25












1 Answer
1






active

oldest

votes


















0














Try somthing like as below:



rooms.aggregate([ {   "$match" : { "_id" : ObjectId("5c11fb6fc7daa1c774fb4e67")}},
{
"$lookup":
{
"from": "facility",
"localField": "facilities",
"foreignField": "_id",
"as": "facilities"
}
},
{
"$lookup":
{
"from": "h", // name of your collection
"localField": "hid",
"foreignField": "_id",
"as": "hotel"
}
},
{
"$project" : {"facilities":1, "hotel":1}
}

])





share|improve this answer
























  • i should have all the fields of collection room and collection facilities

    – Saqib Mujtaba
    Jan 2 at 17:17











  • You can directly append field from room collection in your "$project". like { "$project": { "roomNumber":1,"roomKeyNumber":1 } } along with facilities and hotel.

    – Jitendra
    Jan 2 at 17:26













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010110%2f3-table-joins-in-mongodb%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try somthing like as below:



rooms.aggregate([ {   "$match" : { "_id" : ObjectId("5c11fb6fc7daa1c774fb4e67")}},
{
"$lookup":
{
"from": "facility",
"localField": "facilities",
"foreignField": "_id",
"as": "facilities"
}
},
{
"$lookup":
{
"from": "h", // name of your collection
"localField": "hid",
"foreignField": "_id",
"as": "hotel"
}
},
{
"$project" : {"facilities":1, "hotel":1}
}

])





share|improve this answer
























  • i should have all the fields of collection room and collection facilities

    – Saqib Mujtaba
    Jan 2 at 17:17











  • You can directly append field from room collection in your "$project". like { "$project": { "roomNumber":1,"roomKeyNumber":1 } } along with facilities and hotel.

    – Jitendra
    Jan 2 at 17:26


















0














Try somthing like as below:



rooms.aggregate([ {   "$match" : { "_id" : ObjectId("5c11fb6fc7daa1c774fb4e67")}},
{
"$lookup":
{
"from": "facility",
"localField": "facilities",
"foreignField": "_id",
"as": "facilities"
}
},
{
"$lookup":
{
"from": "h", // name of your collection
"localField": "hid",
"foreignField": "_id",
"as": "hotel"
}
},
{
"$project" : {"facilities":1, "hotel":1}
}

])





share|improve this answer
























  • i should have all the fields of collection room and collection facilities

    – Saqib Mujtaba
    Jan 2 at 17:17











  • You can directly append field from room collection in your "$project". like { "$project": { "roomNumber":1,"roomKeyNumber":1 } } along with facilities and hotel.

    – Jitendra
    Jan 2 at 17:26
















0












0








0







Try somthing like as below:



rooms.aggregate([ {   "$match" : { "_id" : ObjectId("5c11fb6fc7daa1c774fb4e67")}},
{
"$lookup":
{
"from": "facility",
"localField": "facilities",
"foreignField": "_id",
"as": "facilities"
}
},
{
"$lookup":
{
"from": "h", // name of your collection
"localField": "hid",
"foreignField": "_id",
"as": "hotel"
}
},
{
"$project" : {"facilities":1, "hotel":1}
}

])





share|improve this answer













Try somthing like as below:



rooms.aggregate([ {   "$match" : { "_id" : ObjectId("5c11fb6fc7daa1c774fb4e67")}},
{
"$lookup":
{
"from": "facility",
"localField": "facilities",
"foreignField": "_id",
"as": "facilities"
}
},
{
"$lookup":
{
"from": "h", // name of your collection
"localField": "hid",
"foreignField": "_id",
"as": "hotel"
}
},
{
"$project" : {"facilities":1, "hotel":1}
}

])






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 16:56









JitendraJitendra

1,086622




1,086622













  • i should have all the fields of collection room and collection facilities

    – Saqib Mujtaba
    Jan 2 at 17:17











  • You can directly append field from room collection in your "$project". like { "$project": { "roomNumber":1,"roomKeyNumber":1 } } along with facilities and hotel.

    – Jitendra
    Jan 2 at 17:26





















  • i should have all the fields of collection room and collection facilities

    – Saqib Mujtaba
    Jan 2 at 17:17











  • You can directly append field from room collection in your "$project". like { "$project": { "roomNumber":1,"roomKeyNumber":1 } } along with facilities and hotel.

    – Jitendra
    Jan 2 at 17:26



















i should have all the fields of collection room and collection facilities

– Saqib Mujtaba
Jan 2 at 17:17





i should have all the fields of collection room and collection facilities

– Saqib Mujtaba
Jan 2 at 17:17













You can directly append field from room collection in your "$project". like { "$project": { "roomNumber":1,"roomKeyNumber":1 } } along with facilities and hotel.

– Jitendra
Jan 2 at 17:26







You can directly append field from room collection in your "$project". like { "$project": { "roomNumber":1,"roomKeyNumber":1 } } along with facilities and hotel.

– Jitendra
Jan 2 at 17:26






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010110%2f3-table-joins-in-mongodb%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

Npm cannot find a required file even through it is in the searched directory

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith