$lookup target array always empty [duplicate]
This question already has an answer here:
$lookup returning empty array
1 answer
I have two tables: Achievements
and Achievementcompleteds
Achievements
structure: _id
title
etc
Achievementcompleted
structure: _id
achievement_id
user_id
Now I need to fetch data when user
is completed achievements and display it with not completed achievements. Theres is a list of achievements and need to show what are completed and what are not completed. I'am trying to do it like this:
Achievement.aggregate([
{
$lookup: {
from: "achievementcompleteds", // collection name in db
localField: "_id",
foreignField: "achievement_id",
as: "completed"
}
}
]).then((results) => {});
But the completed is always empty
node.js mongodb
marked as duplicate by Neil Lunn
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 9:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
$lookup returning empty array
1 answer
I have two tables: Achievements
and Achievementcompleteds
Achievements
structure: _id
title
etc
Achievementcompleted
structure: _id
achievement_id
user_id
Now I need to fetch data when user
is completed achievements and display it with not completed achievements. Theres is a list of achievements and need to show what are completed and what are not completed. I'am trying to do it like this:
Achievement.aggregate([
{
$lookup: {
from: "achievementcompleteds", // collection name in db
localField: "_id",
foreignField: "achievement_id",
as: "completed"
}
}
]).then((results) => {});
But the completed is always empty
node.js mongodb
marked as duplicate by Neil Lunn
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 9:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Have you even checked the output of the$lookup
? Are there any entries in thecompleted
array for any of the documents? Are there any documents that are actually supposed to match from the two collections? Can you show sample documents you expect to match? And can you basically decide which question you are asking here, be it the aggregation result or how to write a template for the working aggregation result.
– Neil Lunn
Nov 21 '18 at 9:08
1
Odds are you mean the$lookup
is not working, and it's probably because theachievement_id
is actually a "string" whilst the "related"_id
is of courseObjectId
. In which case, you need to fix your data.
– Neil Lunn
Nov 21 '18 at 9:10
It's probably of the ObejctID I never thought of that
– abraom_185
Nov 21 '18 at 9:15
2
It's almost always the case. The common answer should be much easier to find. Hopefully after a couple of quick edits others find it more easily now as well.
– Neil Lunn
Nov 21 '18 at 9:20
add a comment |
This question already has an answer here:
$lookup returning empty array
1 answer
I have two tables: Achievements
and Achievementcompleteds
Achievements
structure: _id
title
etc
Achievementcompleted
structure: _id
achievement_id
user_id
Now I need to fetch data when user
is completed achievements and display it with not completed achievements. Theres is a list of achievements and need to show what are completed and what are not completed. I'am trying to do it like this:
Achievement.aggregate([
{
$lookup: {
from: "achievementcompleteds", // collection name in db
localField: "_id",
foreignField: "achievement_id",
as: "completed"
}
}
]).then((results) => {});
But the completed is always empty
node.js mongodb
This question already has an answer here:
$lookup returning empty array
1 answer
I have two tables: Achievements
and Achievementcompleteds
Achievements
structure: _id
title
etc
Achievementcompleted
structure: _id
achievement_id
user_id
Now I need to fetch data when user
is completed achievements and display it with not completed achievements. Theres is a list of achievements and need to show what are completed and what are not completed. I'am trying to do it like this:
Achievement.aggregate([
{
$lookup: {
from: "achievementcompleteds", // collection name in db
localField: "_id",
foreignField: "achievement_id",
as: "completed"
}
}
]).then((results) => {});
But the completed is always empty
This question already has an answer here:
$lookup returning empty array
1 answer
node.js mongodb
node.js mongodb
edited Nov 21 '18 at 9:17
Neil Lunn
98.2k23174184
98.2k23174184
asked Nov 21 '18 at 9:02
abraom_185abraom_185
577
577
marked as duplicate by Neil Lunn
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 9:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Neil Lunn
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 9:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Have you even checked the output of the$lookup
? Are there any entries in thecompleted
array for any of the documents? Are there any documents that are actually supposed to match from the two collections? Can you show sample documents you expect to match? And can you basically decide which question you are asking here, be it the aggregation result or how to write a template for the working aggregation result.
– Neil Lunn
Nov 21 '18 at 9:08
1
Odds are you mean the$lookup
is not working, and it's probably because theachievement_id
is actually a "string" whilst the "related"_id
is of courseObjectId
. In which case, you need to fix your data.
– Neil Lunn
Nov 21 '18 at 9:10
It's probably of the ObejctID I never thought of that
– abraom_185
Nov 21 '18 at 9:15
2
It's almost always the case. The common answer should be much easier to find. Hopefully after a couple of quick edits others find it more easily now as well.
– Neil Lunn
Nov 21 '18 at 9:20
add a comment |
1
Have you even checked the output of the$lookup
? Are there any entries in thecompleted
array for any of the documents? Are there any documents that are actually supposed to match from the two collections? Can you show sample documents you expect to match? And can you basically decide which question you are asking here, be it the aggregation result or how to write a template for the working aggregation result.
– Neil Lunn
Nov 21 '18 at 9:08
1
Odds are you mean the$lookup
is not working, and it's probably because theachievement_id
is actually a "string" whilst the "related"_id
is of courseObjectId
. In which case, you need to fix your data.
– Neil Lunn
Nov 21 '18 at 9:10
It's probably of the ObejctID I never thought of that
– abraom_185
Nov 21 '18 at 9:15
2
It's almost always the case. The common answer should be much easier to find. Hopefully after a couple of quick edits others find it more easily now as well.
– Neil Lunn
Nov 21 '18 at 9:20
1
1
Have you even checked the output of the
$lookup
? Are there any entries in the completed
array for any of the documents? Are there any documents that are actually supposed to match from the two collections? Can you show sample documents you expect to match? And can you basically decide which question you are asking here, be it the aggregation result or how to write a template for the working aggregation result.– Neil Lunn
Nov 21 '18 at 9:08
Have you even checked the output of the
$lookup
? Are there any entries in the completed
array for any of the documents? Are there any documents that are actually supposed to match from the two collections? Can you show sample documents you expect to match? And can you basically decide which question you are asking here, be it the aggregation result or how to write a template for the working aggregation result.– Neil Lunn
Nov 21 '18 at 9:08
1
1
Odds are you mean the
$lookup
is not working, and it's probably because the achievement_id
is actually a "string" whilst the "related" _id
is of course ObjectId
. In which case, you need to fix your data.– Neil Lunn
Nov 21 '18 at 9:10
Odds are you mean the
$lookup
is not working, and it's probably because the achievement_id
is actually a "string" whilst the "related" _id
is of course ObjectId
. In which case, you need to fix your data.– Neil Lunn
Nov 21 '18 at 9:10
It's probably of the ObejctID I never thought of that
– abraom_185
Nov 21 '18 at 9:15
It's probably of the ObejctID I never thought of that
– abraom_185
Nov 21 '18 at 9:15
2
2
It's almost always the case. The common answer should be much easier to find. Hopefully after a couple of quick edits others find it more easily now as well.
– Neil Lunn
Nov 21 '18 at 9:20
It's almost always the case. The common answer should be much easier to find. Hopefully after a couple of quick edits others find it more easily now as well.
– Neil Lunn
Nov 21 '18 at 9:20
add a comment |
1 Answer
1
active
oldest
votes
Your query is perfect. So, you need to check the following issue
achievement_id: Should be ObjectId
from: Collection name should be same as collection_name shown in mongodb
Version: $lookup is available on mongo version 3.4+
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your query is perfect. So, you need to check the following issue
achievement_id: Should be ObjectId
from: Collection name should be same as collection_name shown in mongodb
Version: $lookup is available on mongo version 3.4+
add a comment |
Your query is perfect. So, you need to check the following issue
achievement_id: Should be ObjectId
from: Collection name should be same as collection_name shown in mongodb
Version: $lookup is available on mongo version 3.4+
add a comment |
Your query is perfect. So, you need to check the following issue
achievement_id: Should be ObjectId
from: Collection name should be same as collection_name shown in mongodb
Version: $lookup is available on mongo version 3.4+
Your query is perfect. So, you need to check the following issue
achievement_id: Should be ObjectId
from: Collection name should be same as collection_name shown in mongodb
Version: $lookup is available on mongo version 3.4+
answered Nov 21 '18 at 9:18
Biplab MalakarBiplab Malakar
41837
41837
add a comment |
add a comment |
1
Have you even checked the output of the
$lookup
? Are there any entries in thecompleted
array for any of the documents? Are there any documents that are actually supposed to match from the two collections? Can you show sample documents you expect to match? And can you basically decide which question you are asking here, be it the aggregation result or how to write a template for the working aggregation result.– Neil Lunn
Nov 21 '18 at 9:08
1
Odds are you mean the
$lookup
is not working, and it's probably because theachievement_id
is actually a "string" whilst the "related"_id
is of courseObjectId
. In which case, you need to fix your data.– Neil Lunn
Nov 21 '18 at 9:10
It's probably of the ObejctID I never thought of that
– abraom_185
Nov 21 '18 at 9:15
2
It's almost always the case. The common answer should be much easier to find. Hopefully after a couple of quick edits others find it more easily now as well.
– Neil Lunn
Nov 21 '18 at 9:20