Aggregate multiple collections based on student Id












1














I am trying to aggregate 2 collections in MongoDB based on a student's ID. One collection consists of student personal information, another one consists of the students logs. The issue is that the data is in array which is why I think my aggregation is not working. Any help will be appreciated.



student collection



{
"_id" : ObjectId("(Object ID here"),
"data" : [
{
"name" : "John",
"id" : 1
},
{
"name" : "Sandy",
"id" : 2
}
]
}


logs collection



{
"_id" : ObjectId("(Object ID here"),
"logs" : [
{
"studentId" : 1,
"activity" : "11112,334,123"
},
{
"studentId" : 2,
"activity" : "11112,334,123"
}
]
}


Here is what I have tried:



dbo.collection("student").aggregate([
{ "$lookup": {
"localField": "data.id",
"from": "logs",
"foreignField": "logs.studentId",
"as": "studentInfo"
}
}]).toArray(function(err, results) {
console.log(results);
});


Expected result:



studentinfo: {
id: 1,
name: "John",
activity" : "11112,334,123"
}









share|improve this question
























  • What version of mongodb you are using? And what output do you want?
    – Anthony Winzlet
    Nov 21 '18 at 16:32












  • Any reason to store data like this? Usually each student would be it's own document
    – Bajal
    Nov 21 '18 at 16:55










  • @AnthonyWinzlet I want to have a joined result with students logs along with personal information
    – HeelMega
    Nov 21 '18 at 17:06










  • @Bajal true but this is part of a bigger application where student does not have that much info currently
    – HeelMega
    Nov 21 '18 at 17:07










  • @HeelMega could you just paste expected output for given input ?
    – mickl
    Nov 21 '18 at 17:08
















1














I am trying to aggregate 2 collections in MongoDB based on a student's ID. One collection consists of student personal information, another one consists of the students logs. The issue is that the data is in array which is why I think my aggregation is not working. Any help will be appreciated.



student collection



{
"_id" : ObjectId("(Object ID here"),
"data" : [
{
"name" : "John",
"id" : 1
},
{
"name" : "Sandy",
"id" : 2
}
]
}


logs collection



{
"_id" : ObjectId("(Object ID here"),
"logs" : [
{
"studentId" : 1,
"activity" : "11112,334,123"
},
{
"studentId" : 2,
"activity" : "11112,334,123"
}
]
}


Here is what I have tried:



dbo.collection("student").aggregate([
{ "$lookup": {
"localField": "data.id",
"from": "logs",
"foreignField": "logs.studentId",
"as": "studentInfo"
}
}]).toArray(function(err, results) {
console.log(results);
});


Expected result:



studentinfo: {
id: 1,
name: "John",
activity" : "11112,334,123"
}









share|improve this question
























  • What version of mongodb you are using? And what output do you want?
    – Anthony Winzlet
    Nov 21 '18 at 16:32












  • Any reason to store data like this? Usually each student would be it's own document
    – Bajal
    Nov 21 '18 at 16:55










  • @AnthonyWinzlet I want to have a joined result with students logs along with personal information
    – HeelMega
    Nov 21 '18 at 17:06










  • @Bajal true but this is part of a bigger application where student does not have that much info currently
    – HeelMega
    Nov 21 '18 at 17:07










  • @HeelMega could you just paste expected output for given input ?
    – mickl
    Nov 21 '18 at 17:08














1












1








1







I am trying to aggregate 2 collections in MongoDB based on a student's ID. One collection consists of student personal information, another one consists of the students logs. The issue is that the data is in array which is why I think my aggregation is not working. Any help will be appreciated.



student collection



{
"_id" : ObjectId("(Object ID here"),
"data" : [
{
"name" : "John",
"id" : 1
},
{
"name" : "Sandy",
"id" : 2
}
]
}


logs collection



{
"_id" : ObjectId("(Object ID here"),
"logs" : [
{
"studentId" : 1,
"activity" : "11112,334,123"
},
{
"studentId" : 2,
"activity" : "11112,334,123"
}
]
}


Here is what I have tried:



dbo.collection("student").aggregate([
{ "$lookup": {
"localField": "data.id",
"from": "logs",
"foreignField": "logs.studentId",
"as": "studentInfo"
}
}]).toArray(function(err, results) {
console.log(results);
});


Expected result:



studentinfo: {
id: 1,
name: "John",
activity" : "11112,334,123"
}









share|improve this question















I am trying to aggregate 2 collections in MongoDB based on a student's ID. One collection consists of student personal information, another one consists of the students logs. The issue is that the data is in array which is why I think my aggregation is not working. Any help will be appreciated.



student collection



{
"_id" : ObjectId("(Object ID here"),
"data" : [
{
"name" : "John",
"id" : 1
},
{
"name" : "Sandy",
"id" : 2
}
]
}


logs collection



{
"_id" : ObjectId("(Object ID here"),
"logs" : [
{
"studentId" : 1,
"activity" : "11112,334,123"
},
{
"studentId" : 2,
"activity" : "11112,334,123"
}
]
}


Here is what I have tried:



dbo.collection("student").aggregate([
{ "$lookup": {
"localField": "data.id",
"from": "logs",
"foreignField": "logs.studentId",
"as": "studentInfo"
}
}]).toArray(function(err, results) {
console.log(results);
});


Expected result:



studentinfo: {
id: 1,
name: "John",
activity" : "11112,334,123"
}






arrays mongodb mongoose mongodb-query aggregation-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 17:18

























asked Nov 21 '18 at 16:28









HeelMega

677




677












  • What version of mongodb you are using? And what output do you want?
    – Anthony Winzlet
    Nov 21 '18 at 16:32












  • Any reason to store data like this? Usually each student would be it's own document
    – Bajal
    Nov 21 '18 at 16:55










  • @AnthonyWinzlet I want to have a joined result with students logs along with personal information
    – HeelMega
    Nov 21 '18 at 17:06










  • @Bajal true but this is part of a bigger application where student does not have that much info currently
    – HeelMega
    Nov 21 '18 at 17:07










  • @HeelMega could you just paste expected output for given input ?
    – mickl
    Nov 21 '18 at 17:08


















  • What version of mongodb you are using? And what output do you want?
    – Anthony Winzlet
    Nov 21 '18 at 16:32












  • Any reason to store data like this? Usually each student would be it's own document
    – Bajal
    Nov 21 '18 at 16:55










  • @AnthonyWinzlet I want to have a joined result with students logs along with personal information
    – HeelMega
    Nov 21 '18 at 17:06










  • @Bajal true but this is part of a bigger application where student does not have that much info currently
    – HeelMega
    Nov 21 '18 at 17:07










  • @HeelMega could you just paste expected output for given input ?
    – mickl
    Nov 21 '18 at 17:08
















What version of mongodb you are using? And what output do you want?
– Anthony Winzlet
Nov 21 '18 at 16:32






What version of mongodb you are using? And what output do you want?
– Anthony Winzlet
Nov 21 '18 at 16:32














Any reason to store data like this? Usually each student would be it's own document
– Bajal
Nov 21 '18 at 16:55




Any reason to store data like this? Usually each student would be it's own document
– Bajal
Nov 21 '18 at 16:55












@AnthonyWinzlet I want to have a joined result with students logs along with personal information
– HeelMega
Nov 21 '18 at 17:06




@AnthonyWinzlet I want to have a joined result with students logs along with personal information
– HeelMega
Nov 21 '18 at 17:06












@Bajal true but this is part of a bigger application where student does not have that much info currently
– HeelMega
Nov 21 '18 at 17:07




@Bajal true but this is part of a bigger application where student does not have that much info currently
– HeelMega
Nov 21 '18 at 17:07












@HeelMega could you just paste expected output for given input ?
– mickl
Nov 21 '18 at 17:08




@HeelMega could you just paste expected output for given input ?
– mickl
Nov 21 '18 at 17:08












1 Answer
1






active

oldest

votes


















2














You can use below aggregation with mongodb 3.6



So basically your foreign field is an array you need to use $lookup with the pipeline to $unwind the foreign array inside the $lookup pipeline and to match the corresponding ids.



db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}}
])


or use this to merge both the arrays



db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}},
{ "$project": {
"students": {
"$map": {
"input": "$students",
"in": {
"studentId": "$$this.studentId",
"activity": "$$this.activity",
"name": { "$arrayElemAt": ["$data.name", { "$indexOfArray": ["$data.id", "$$this.studentId"] }]}
}
}
}
}}
])


Output



[
{
"students": [
{
"activity": "11112,334,123",
"name": "John",
"studentId": 1
},
{
"activity": "11112,334,123",
"name": "Sandy",
"studentId": 2
}
]
}
]





share|improve this answer























  • Where is dataId in the first query coming from?
    – HeelMega
    Nov 21 '18 at 17:47










  • dataId is the variable we have taken in let expression. Which is your localField.
    – Anthony Winzlet
    Nov 21 '18 at 17:49











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%2f53416506%2faggregate-multiple-collections-based-on-student-id%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









2














You can use below aggregation with mongodb 3.6



So basically your foreign field is an array you need to use $lookup with the pipeline to $unwind the foreign array inside the $lookup pipeline and to match the corresponding ids.



db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}}
])


or use this to merge both the arrays



db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}},
{ "$project": {
"students": {
"$map": {
"input": "$students",
"in": {
"studentId": "$$this.studentId",
"activity": "$$this.activity",
"name": { "$arrayElemAt": ["$data.name", { "$indexOfArray": ["$data.id", "$$this.studentId"] }]}
}
}
}
}}
])


Output



[
{
"students": [
{
"activity": "11112,334,123",
"name": "John",
"studentId": 1
},
{
"activity": "11112,334,123",
"name": "Sandy",
"studentId": 2
}
]
}
]





share|improve this answer























  • Where is dataId in the first query coming from?
    – HeelMega
    Nov 21 '18 at 17:47










  • dataId is the variable we have taken in let expression. Which is your localField.
    – Anthony Winzlet
    Nov 21 '18 at 17:49
















2














You can use below aggregation with mongodb 3.6



So basically your foreign field is an array you need to use $lookup with the pipeline to $unwind the foreign array inside the $lookup pipeline and to match the corresponding ids.



db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}}
])


or use this to merge both the arrays



db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}},
{ "$project": {
"students": {
"$map": {
"input": "$students",
"in": {
"studentId": "$$this.studentId",
"activity": "$$this.activity",
"name": { "$arrayElemAt": ["$data.name", { "$indexOfArray": ["$data.id", "$$this.studentId"] }]}
}
}
}
}}
])


Output



[
{
"students": [
{
"activity": "11112,334,123",
"name": "John",
"studentId": 1
},
{
"activity": "11112,334,123",
"name": "Sandy",
"studentId": 2
}
]
}
]





share|improve this answer























  • Where is dataId in the first query coming from?
    – HeelMega
    Nov 21 '18 at 17:47










  • dataId is the variable we have taken in let expression. Which is your localField.
    – Anthony Winzlet
    Nov 21 '18 at 17:49














2












2








2






You can use below aggregation with mongodb 3.6



So basically your foreign field is an array you need to use $lookup with the pipeline to $unwind the foreign array inside the $lookup pipeline and to match the corresponding ids.



db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}}
])


or use this to merge both the arrays



db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}},
{ "$project": {
"students": {
"$map": {
"input": "$students",
"in": {
"studentId": "$$this.studentId",
"activity": "$$this.activity",
"name": { "$arrayElemAt": ["$data.name", { "$indexOfArray": ["$data.id", "$$this.studentId"] }]}
}
}
}
}}
])


Output



[
{
"students": [
{
"activity": "11112,334,123",
"name": "John",
"studentId": 1
},
{
"activity": "11112,334,123",
"name": "Sandy",
"studentId": 2
}
]
}
]





share|improve this answer














You can use below aggregation with mongodb 3.6



So basically your foreign field is an array you need to use $lookup with the pipeline to $unwind the foreign array inside the $lookup pipeline and to match the corresponding ids.



db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}}
])


or use this to merge both the arrays



db.students.aggregate([
{ "$lookup": {
"from": "logs",
"let": { "dataId": "$data.id" },
"pipeline": [
{ "$unwind": "$logs" },
{ "$match": { "$expr": { "$in": ["$logs.studentId", "$$dataId"] }}},
{ "$replaceRoot": { "newRoot": "$logs" }}
],
"as": "students"
}},
{ "$project": {
"students": {
"$map": {
"input": "$students",
"in": {
"studentId": "$$this.studentId",
"activity": "$$this.activity",
"name": { "$arrayElemAt": ["$data.name", { "$indexOfArray": ["$data.id", "$$this.studentId"] }]}
}
}
}
}}
])


Output



[
{
"students": [
{
"activity": "11112,334,123",
"name": "John",
"studentId": 1
},
{
"activity": "11112,334,123",
"name": "Sandy",
"studentId": 2
}
]
}
]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 17:52

























answered Nov 21 '18 at 17:27









Anthony Winzlet

14.1k41239




14.1k41239












  • Where is dataId in the first query coming from?
    – HeelMega
    Nov 21 '18 at 17:47










  • dataId is the variable we have taken in let expression. Which is your localField.
    – Anthony Winzlet
    Nov 21 '18 at 17:49


















  • Where is dataId in the first query coming from?
    – HeelMega
    Nov 21 '18 at 17:47










  • dataId is the variable we have taken in let expression. Which is your localField.
    – Anthony Winzlet
    Nov 21 '18 at 17:49
















Where is dataId in the first query coming from?
– HeelMega
Nov 21 '18 at 17:47




Where is dataId in the first query coming from?
– HeelMega
Nov 21 '18 at 17:47












dataId is the variable we have taken in let expression. Which is your localField.
– Anthony Winzlet
Nov 21 '18 at 17:49




dataId is the variable we have taken in let expression. Which is your localField.
– Anthony Winzlet
Nov 21 '18 at 17:49


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53416506%2faggregate-multiple-collections-based-on-student-id%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'