Aggregate multiple collections based on student Id
up vote
1
down vote
favorite
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
|
show 1 more comment
up vote
1
down vote
favorite
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
What version of mongodb you are using? And what output do you want?
– Anthony Winzlet
Nov 21 at 16:32
Any reason to store data like this? Usually each student would be it's own document
– Bajal
Nov 21 at 16:55
@AnthonyWinzlet I want to have a joined result with students logs along with personal information
– HeelMega
Nov 21 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 at 17:07
@HeelMega could you just paste expected output for given input ?
– mickl
Nov 21 at 17:08
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
arrays mongodb mongoose mongodb-query aggregation-framework
edited Nov 21 at 17:18
asked Nov 21 at 16:28
HeelMega
706
706
What version of mongodb you are using? And what output do you want?
– Anthony Winzlet
Nov 21 at 16:32
Any reason to store data like this? Usually each student would be it's own document
– Bajal
Nov 21 at 16:55
@AnthonyWinzlet I want to have a joined result with students logs along with personal information
– HeelMega
Nov 21 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 at 17:07
@HeelMega could you just paste expected output for given input ?
– mickl
Nov 21 at 17:08
|
show 1 more comment
What version of mongodb you are using? And what output do you want?
– Anthony Winzlet
Nov 21 at 16:32
Any reason to store data like this? Usually each student would be it's own document
– Bajal
Nov 21 at 16:55
@AnthonyWinzlet I want to have a joined result with students logs along with personal information
– HeelMega
Nov 21 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 at 17:07
@HeelMega could you just paste expected output for given input ?
– mickl
Nov 21 at 17:08
What version of mongodb you are using? And what output do you want?
– Anthony Winzlet
Nov 21 at 16:32
What version of mongodb you are using? And what output do you want?
– Anthony Winzlet
Nov 21 at 16:32
Any reason to store data like this? Usually each student would be it's own document
– Bajal
Nov 21 at 16:55
Any reason to store data like this? Usually each student would be it's own document
– Bajal
Nov 21 at 16:55
@AnthonyWinzlet I want to have a joined result with students logs along with personal information
– HeelMega
Nov 21 at 17:06
@AnthonyWinzlet I want to have a joined result with students logs along with personal information
– HeelMega
Nov 21 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 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 at 17:07
@HeelMega could you just paste expected output for given input ?
– mickl
Nov 21 at 17:08
@HeelMega could you just paste expected output for given input ?
– mickl
Nov 21 at 17:08
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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
}
]
}
]
Where is dataId in the first query coming from?
– HeelMega
Nov 21 at 17:47
dataId
is the variable we have taken inlet
expression. Which is yourlocalField
.
– Anthony Winzlet
Nov 21 at 17:49
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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
}
]
}
]
Where is dataId in the first query coming from?
– HeelMega
Nov 21 at 17:47
dataId
is the variable we have taken inlet
expression. Which is yourlocalField
.
– Anthony Winzlet
Nov 21 at 17:49
add a comment |
up vote
2
down vote
accepted
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
}
]
}
]
Where is dataId in the first query coming from?
– HeelMega
Nov 21 at 17:47
dataId
is the variable we have taken inlet
expression. Which is yourlocalField
.
– Anthony Winzlet
Nov 21 at 17:49
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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
}
]
}
]
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
}
]
}
]
edited Nov 21 at 17:52
answered Nov 21 at 17:27
Anthony Winzlet
11.7k41038
11.7k41038
Where is dataId in the first query coming from?
– HeelMega
Nov 21 at 17:47
dataId
is the variable we have taken inlet
expression. Which is yourlocalField
.
– Anthony Winzlet
Nov 21 at 17:49
add a comment |
Where is dataId in the first query coming from?
– HeelMega
Nov 21 at 17:47
dataId
is the variable we have taken inlet
expression. Which is yourlocalField
.
– Anthony Winzlet
Nov 21 at 17:49
Where is dataId in the first query coming from?
– HeelMega
Nov 21 at 17:47
Where is dataId in the first query coming from?
– HeelMega
Nov 21 at 17:47
dataId
is the variable we have taken in let
expression. Which is your localField
.– Anthony Winzlet
Nov 21 at 17:49
dataId
is the variable we have taken in let
expression. Which is your localField
.– Anthony Winzlet
Nov 21 at 17:49
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What version of mongodb you are using? And what output do you want?
– Anthony Winzlet
Nov 21 at 16:32
Any reason to store data like this? Usually each student would be it's own document
– Bajal
Nov 21 at 16:55
@AnthonyWinzlet I want to have a joined result with students logs along with personal information
– HeelMega
Nov 21 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 at 17:07
@HeelMega could you just paste expected output for given input ?
– mickl
Nov 21 at 17:08