$lookup target array always empty [duplicate]












0
















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










share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

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 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





    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






  • 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
















0
















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










share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

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 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





    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






  • 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














0












0








0









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










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

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 mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

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 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





    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






  • 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





    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





    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






  • 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












1 Answer
1






active

oldest

votes


















0














Your query is perfect. So, you need to check the following issue





  1. achievement_id: Should be ObjectId


  2. from: Collection name should be same as collection_name shown in mongodb


  3. Version: $lookup is available on mongo version 3.4+






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Your query is perfect. So, you need to check the following issue





    1. achievement_id: Should be ObjectId


    2. from: Collection name should be same as collection_name shown in mongodb


    3. Version: $lookup is available on mongo version 3.4+






    share|improve this answer




























      0














      Your query is perfect. So, you need to check the following issue





      1. achievement_id: Should be ObjectId


      2. from: Collection name should be same as collection_name shown in mongodb


      3. Version: $lookup is available on mongo version 3.4+






      share|improve this answer


























        0












        0








        0







        Your query is perfect. So, you need to check the following issue





        1. achievement_id: Should be ObjectId


        2. from: Collection name should be same as collection_name shown in mongodb


        3. Version: $lookup is available on mongo version 3.4+






        share|improve this answer













        Your query is perfect. So, you need to check the following issue





        1. achievement_id: Should be ObjectId


        2. from: Collection name should be same as collection_name shown in mongodb


        3. Version: $lookup is available on mongo version 3.4+







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 9:18









        Biplab MalakarBiplab Malakar

        41837




        41837















            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

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