Need field form $group in output after using match, lookup, unwind - mongodb

I have applied following query to get the result but I am not getting total_members in the output:
PodMembers.aggregate([
{
$match: { datetime: { $exists: true } }
},
{
$group: {
_id: "$pod_id",
total_members: { $sum: 1 }
}
},
{ $unwind: "$total_members" },
{
$lookup: {
from: "pods",
localField: "_id",
foreignField: "_id",
as: "podinfo"
}
},
{ $unwind: "$podinfo" },
{ "$replaceRoot": { "newRoot": "$podinfo" } },
{ $addFields: { "Status": false } }
]).exec(function (err, resp) {
if (err) console.log(err)
else console.log(resp)
})
pods documents like:
{
"_id": "5ae46c0aff1bde1634ad8b3f",
"admin": "595bdd5aa6ea2811d48a7802",
"name": "Fashion Trend",
"datetime": 1524919038771,
"approval": false,
"actions": "Comments",
"description": "Fashion is about dressing according to what's fashionable. Style is more about being yourself.",
"profilepic": "https://cdn.com/upload/Community_photos/595bdd5a6ea2811d487802_152919188491.jpg",
"coverpic": "https://cdn.com/upload/Community_photos/595dd5aaea2811d48a7802_152499171374.jpg",
"__v": 0
}
pod_members documents like:
{
"_id": "5b127683187a4f19901e34b7",
"__v": 0,
"pod_id": "5ae470d04e15d607f043a20e",
"instaid": "5ad9a44cabd2180d64073462",
"username": "out_tur",
"user_id": "595bdd5aa6ea2811d48a7802",
"member_name": "Sagar Dalal",
"req_date": 1527936643123,
"datetime": 1527938142745,
}
I have also attached output which I am getting from above code:

Related

Display only select nested fields of object in MongoDB Compass aggregation

I have the following data model:
{
"_id": {
"$oid": "63b6da81661f0ecd23cd9830"
},
"Plan": [
{
"_id": {
"$oid": "63b6311e0871625f7ceb85ad"
},
"Name": "Straight ankle lock",
"Date": {
"$date": {
"$numberLong": "1672725600000"
}
},
"Notes": "Christian taught ankle locks",
"TeamId": {
"$oid": "63a291ebb60592854e23b8fb"
}
}
],
"User": [
{
"_id": {
"$oid": "6240fd2ee1335b45680bee9d"
},
"FirstName": "Test",
"LastName": "User",
"TeamId": {
"$oid": "639fd03bb31c7995a9d4b28c"
}
}
]
}
And I'd like to show a new object via aggregation that looks like:
{
"_id": {
"$oid": "63b6da81661f0ecd23cd9830"
},
"PlanName": "Straight ankle lock",
"UserName": "Test User"
}
I've been trying to figure this out for a few days, but at this point not sure if it is even possible. Any ideas?
Thanks.
Newer model based on Ray's input using project:
{
"_id": {
"$oid": "63b6da81661f0ecd23cd9830"
},
"InsertDate": {
"$date": {
"$numberLong": "1672927873507"
}
},
"Plan": {
"Name": "Straight ankle lock"
},
"User": {
"FirstName": "Adam",
"LastName": "Gusky"
},
"Team": {
"TeamName": "GB2 No Gi"
}
}
The query I'm using to get the above data:
[
{
$lookup: {
from: "Plans",
localField: "PlanId",
foreignField: "_id",
as: "Plan",
},
},
{
$lookup: {
from: "Teams",
localField: "TeamId",
foreignField: "_id",
as: "Team",
},
},
{
$lookup: {
from: "Users",
localField: "UserId",
foreignField: "_id",
as: "User",
},
},
{
$project: {
Plan: {
$first: "$Plan",
},
User: {
$first: "$User",
},
Team: {
$first: "$Team",
},
InsertDate: 1,
},
},
{
$project: {
"Plan.Name": 1,
"User.FirstName": 1,
"User.LastName": 1,
"Team.TeamName": 1,
InsertDate: 1,
},
},
]
You can simply set the value you want in the $project stage.
db.collection.aggregate([
{
$project: {
_id: 1,
PlanName: {
$first: "$Plan.Name"
},
UserName: {
"$concat": [
{
"$first": "$User.FirstName"
},
" ",
{
"$first": "$User.LastName"
}
]
}
}
}
])
Mongo Playground

Find one user then get their ranking based on their total points using MongoDB

So I got the following data:
Users collection
{
_id: ObjectId("62a2a0422ec90fea68390aaa"),
name: 'Robert Yamashita',
username: 'robyama',
email: 'robert.yamashita#rocketmail.com',
},
{
_id: ObjectId("62a2a0452ec90fea68390aad"),
name: 'Charles X',
username: 'cvx',
email: 'charles.xxx#rocketmail.com',
}
Points collection
{
userId: ObjectId("62a2a0422ec90fea68390aaa"),
action: 'Liked a post',
points: 10,
}
{
userId: ObjectId("62a2a0422ec90fea68390aaa"),
action: 'Liked a post',
points: 10,
}
{
userId: ObjectId("62a2a0452ec90fea68390aad"),
action: 'Liked a comment',
points: 5,
}
I created a pipeline to get the total points of username robyama using the following query:
db.users.aggregate([
{ $match: { username: 'robyama' } },
{
$lookup: {
from: 'points',
localField: '_id',
foreignField: 'user',
as: 'userPoints'
}
},
{
$unwind: '$userPoints'
},
{
$group: {
_id: {
name: '$name',
email: '$email',
username: '$username',
},
count: { $sum: '$userPoints.points' }
}
}
]);
I got the following result:
{
"_id": {
"name": "Robert Yamashita",
"email": "robert.yamashita#rocketmail.com",
"username": "robyama",
},
"count": 20
}
This is exactly what I needed but I wanted to add a ranking field to the returned query since Robert has 20 points and Charles only has 5. So ideally I want the result to be this:
{
"_id": {
"name": "Robert Yamashita",
"email": "robert.yamashita#rocketmail.com",
"username": "robyama",
},
"count": 20
"rank": 1
}
What should I add to my pipeline to get the above output? Any help would be greatly appreciated!
Here's another way to do it. There's only one "$lookup" with one embedded "$group" so it should be fairly efficient. The "$project" seems a bit contrived, but it gives the output in the format you want.
db.users.aggregate([
{
"$match": {
"username": "robyama"
}
},
{
"$lookup": {
"from": "points",
"as": "sortedPoints",
"pipeline": [
{
"$group": {
"_id": "$userId",
"count": {"$sum": "$points"}
}
},
{"$sort": {"count": -1}}
]
}
},
{
"$unwind": {
"path": "$sortedPoints",
"includeArrayIndex": "idx"
}
},
{
"$match": {
"$expr": {
"$eq": ["$_id", "$sortedPoints._id"]
}
}
},
{
"$project": {
"_id": {
"name": "$name",
"username": "$username",
"email": "$email"
},
"count": "$sortedPoints.count",
"rank": {
"$add": ["$idx", 1]
}
}
}
])
Try it on mongoplayground.net.
Well, this is one way of doing it.
Perform join using $lookup and calculate counts for each user.
Sort the elements by counts in desc order.
Group documents by _id as NULL and push them all in an array.
Unwind the array, along with getting row numbers.
Find your required document and calculate the rank using row number.
db.users.aggregate([
{
$lookup: {
from: "points",
localField: "_id",
foreignField: "userId",
as: "userPoints"
}
},
{
$unwind: "$userPoints"
},
{
$group: {
_id: {
name: "$name",
email: "$email",
username: "$username",
},
count: {
$sum: "$userPoints.points"
}
}
},
{
"$sort": {
count: -1
}
},
{
"$group": {
"_id": null,
"docs": {
"$push": "$$ROOT",
}
}
},
{
"$unwind": {
path: "$docs",
includeArrayIndex: "rownum"
}
},
{
"$match": {
"docs._id.username": "robyama"
}
},
{
"$addFields": {
"docs.rank": {
"$add": [
"$rownum",
1
]
}
}
},
{
"$replaceRoot": {
"newRoot": "$docs"
}
}
])
This is the playground link.

How can I get a mongo subset of a collection based on an another collection

I have two collections.
Collection 1 is like an account.
Collection 2 creates a unique association between a user and an account
I am trying to return the accounts for which the user has no association
Collection1 schema
const Collection1Schema = new Schema({
name: { type: String, required: true },
});
Collection1 data
[
{
"_id": "61cf8452fca008360872c9cd",
"name": "Aff 2"
},
{
"_id": "61cf845ffca008360872c9d0",
"name": "AFF 1"
},
{
"_id": "61cf8468fca008360872c9d3",
"name": "Aff 3"
}
]
Collection2 schema
const Collection2Schema = new Schema({
userID: { type: Schema.Types.ObjectId, required: true },
col_1_ID: { type: Schema.Types.ObjectId, required: true },
});
Collection2 data
[
{
"_id": "61e05bb5fe1d8327d4c73663",
"userID": "61cf82dac828bd519cfd38ca",
"col_1_ID": "61cf845ffca008360872c9d0"
},
{
"_id": "61e05c14fe1d8327d4c7367d",
"userID": "61cf82dac828bd519cfd38ca",
"col_1_ID": "61cf8468fca008360872c9d3"
},
{
"_id": "61e05ca0fe1d8327d4c73695",
"userID": "61e05906246ccc41d4ebd30f",
"col_1_ID": "61cf8452fca008360872c9cd"
}
]
This is what I have so far... but it does not return what the user is NOT apart of
I am using Collection2 as the basis in the pipeline
[
{
'$match': {
'userID': new ObjectId('61cf82dac828bd519cfd38ca')
}
}, {
'$lookup': {
'from': 'Collection1',
'localField': 'col_1_ID',
'foreignField': '_id',
'as': 'aa'
}
}, {
'$unwind': {
'path': '$aa',
'preserveNullAndEmptyArrays': true
}
}
]
What I would like to return is all the collection 1 documents ( where userIdD = '61cf82dac828bd519cfd38ca') is NOT associated in collection 2 ... like this :
[
{
"_id": "61cf8452fca008360872c9cd",
"name": "Aff 2"
}
]
UPDATE 1
Here is a playground where another user has joined another account, so the pipeline does not return "Aff 2" like expected
https://mongoplayground.net/p/W6W88_2MaI3
UPDATE 2
Here is a playground that almost does what I want... it's returning duplication "AFF 2" entries.
https://mongoplayground.net/p/nTI3MKNPEmD
try the inversing lookup
https://mongoplayground.net/p/hXAYyv8X461
db.Collection1.aggregate([
{
"$lookup": {
"from": "Collection2",
"localField": "_id",
"foreignField": "col_1_ID",
"as": "joined_docs"
}
},
{
$unwind: {
"path": "$joined_docs"
}
},
{
$match: {
"joined_docs.userID": {
$ne: "61cf82dac828bd519cfd38ca"
}
}
},
{
$project: {
"joined_docs": 0
}
}
])
ANSWER:
after messing around with several mongo playgrounds and digging into a few different pipeline attributes... here is what works:
https://mongoplayground.net/p/xbZeRfVcrZq
Data:
db={
"Collection1": [
{
"_id": "61cf8452fca008360872c9cd",
"name": "Aff 2"
},
{
"_id": "61cf845ffca008360872c9d0",
"name": "AFF 1"
},
{
"_id": "61cf8468fca008360872c9d3",
"name": "Aff 3"
}
],
"Collection2": [
{
"_id": "61e05bb5fe1d8327d4c73663",
"userID": "61cf82dac828bd519cfd38ca",
"col_1_ID": "61cf845ffca008360872c9d0"
},
{
"_id": "61e05c14fe1d8327d4c7367d",
"userID": "61cf82dac828bd519cfd38ca",
"col_1_ID": "61cf8468fca008360872c9d3"
},
{
"_id": "61e05ca0fe1d8327d4c73695",
"userID": "61e05906246ccc41d4ebd30f",
"col_1_ID": "61cf8452fca008360872c9cd"
},
{
"_id": "61e05c14fe1d8327d4c73600",
"userID": "61cf82dac828bd519cfd3111",
"col_1_ID": "61cf8468fca008360872c9d3"
},
{
"_id": "61e05c14fe1d8327d4c73601",
"userID": "61cf82dac828bd519cfd3112",
"col_1_ID": "61cf8452fca008360872c9cd"
},
]
}
Pipeline:
db.Collection1.aggregate([
{
"$lookup": {
"from": "Collection2",
"localField": "_id",
"foreignField": "col_1_ID",
"as": "joined_docs"
}
},
{
$match: {
"joined_docs.userID": {
$ne: "61cf82dac828bd519cfd38ca"
}
}
},
{
$unwind: {
"path": "$joined_docs",
}
},
{
$group: {
_id: "$_id",
"name": {
"$first": "$name"
},
}
}
])
result:
[
{
"_id": "61cf8452fca008360872c9cd",
"name": "Aff 2"
}
]
try this instead:
https://mongoplayground.net/p/HDm2sbdvH88
db.Collection1.aggregate([
{
"$lookup": {
"from": "Collection2",
"localField": "_id",
"foreignField": "col_1_ID",
"as": "joined_docs"
}
},
{
$unwind: {
"path": "$joined_docs"
}
},
{
$group: {
_id: {
account_id: "$_id",
account_name: "$name",
},
user_ids: {
$push: {
"userID": "$joined_docs.userID"
}
}
}
},
{
$match: {
"user_ids.userID": {
$nin: [
"61cf82dac828bd519cfd38ca"
]
}
}
},
{
$project: {
user_ids: 0
}
}
])

Referring to a different collection from an existing one and counting from the same collection

I have to collections A and B in which the documents of A contains the object ids which are present in the B in the fields centre and gcentre. What I'm currently is outputting the result which contains the name of the parent centre by referring from collection A's object id to B and then referring the gcentre's id to find the child and centre and count the documents assigned via javascript post-processing. Been new to the aggregation pipeline, I don't know how to refer via object id and that sort of counting of records. Is it possible with the aggregation pipeline? I have tried with $lookup but it doesn't seem to give the output as expected.
Documents in collection A:
{
"_id": {
"$oid": "5bbafa98d8c77251bea30f8c"
},
"parentCentre": {
"$oid": "1cbafa99d8c77251bea30f11"
},
"childCentre": {
"$oid": "5cbafa99d8c77251bea30f8d"
},
},
{
"_id": {
"$oid": "5bbafa98d8c77251bea30f8c"
},
"parentCentre": {
"$oid": "1cbafa99d8c77251bea30f11"
},
"childCentre": {
"$oid": "5cbafa99d8c77251bea30f8d"
},
},
{
"_id": {
"$oid": "5bbafa98d8c77251bea30f8c"
},
"parentCentre": {
"$oid": "1cbafa99d8c77251bea30f11"
},
"childCentre": {
"$oid": "5cbafa99d8c77251bea30f8d"
},
},
{
"_id": {
"$oid": "5bbafa98d8c77251bea30f8c"
},
"parentCentre": {
"$oid": "1cbafa99d8c77251bea30f21"
},
"childCentre": {
"$oid": "5cbafa99d8c77251bea30f6d"
},
}
Documents in collection B:
{
"_id": {
"$oid": "1cbafa99d8c77251bea30f11"
},
"Type": "Parent",
"Name": "Kris Labs"
},
{
"_id": {
"$oid": "1cbafa99d8c77251bea30f21"
},
"Type": "Parent",
"Name": "DEX Labs"
},
{
"_id": {
"$oid": "5cbafa99d8c77251bea30f8d"
},
"Type": "Child",
"Name": "Mili Labs"
},
{
"_id": {
"$oid": "5cbafa99d8c77251bea30f6d"
},
"Type": "Child",
"Name": "Max Labs"
}
Result:
{
"parentCentreName":"Kris Labs",
"Records":{
{
childCentreName: "Max Labs",
recordCount: 3
}
}
},
{
"parentCentreName":"DEX Labs",
"Records":{
{
childCentreName: "Mili Labs",
recordCount: 1
}
}
}
You can use the following aggregation query:
db.A.aggregate([
{
$group: {
_id: {
p: "$parentCentre",
c: "$childCentre"
},
count: {
$sum: 1
}
}
},
{
$group: {
_id: "$_id.p",
Records: {
$push: {
childCentreName: "$_id.c",
recordCount: "$count"
}
}
}
},
{
$unwind: "$Records"
},
{
"$lookup": {
"from": "B",
"localField": "_id",
"foreignField": "_id",
"as": "p"
}
},
{
"$lookup": {
"from": "B",
"localField": "Records.childCentreName",
"foreignField": "_id",
"as": "c"
}
},
{
$unwind: "$c"
},
{
$unwind: "$p"
},
{
$project: {
"parentCentreName": "$p.Name",
"Records.childCentreName": "$c.Name",
"Records.recordCount": 1,
_id: 0
}
},
{
$group: {
_id: "$parentCentreName",
"Records": {
$push: "$Records"
}
}
},
{
$project: {
"parentCentreName": "$_id",
"Records": 1,
_id: 0
}
}
])
MongoDB Playground

MongoDB unknown top level operator

Well I'm having problems with aggragate of the mongo, I need to validate if in all the answers it doesn't have a field marked as deletedAt. My aggragate looks like this:
[
{ '$match': { _id: "5f0cc0e676de351ce21a752b" } },
{
'$lookup': {
from: 'Exams',
localField: 'exams.idExams',
foreignField: '_id',
as: 'exams'
}
},
{
'$lookup': {
from: 'Sports',
localField: 'idSports',
foreignField: '_id',
as: 'sportPracticed'
}
},
{
'$unwind': { path: '$sportPracticed', preserveNullAndEmptyArrays: true }
},
{
'$lookup': {
from: 'Galery',
localField: '_id',
foreignField: 'idPlayer',
as: 'galery'
}
},
{
'$lookup': {
from: 'EvaluationPlayer',
localField: '_id',
foreignField: 'idPlayer',
as: 'evaluationPlayer'
}
},
{
'$lookup': {
from: 'Evaluation',
localField: 'evaluationPlayer.idEvaluation',
foreignField: '_id',
as: 'evaluations'
}
},
{
'$lookup': {
from: 'Category',
localField: 'evaluations.idCategory',
foreignField: '_id',
as: 'category'
}
},
{
'$lookup': {
from: 'Club',
localField: 'idClub',
foreignField: '_id',
as: 'club'
}
},
{ '$unwind': { path: '$club', preserveNullAndEmptyArrays: true } },
{
'$lookup': {
from: 'Agent',
localField: 'idAgent',
foreignField: '_id',
as: 'agent'
}
},
{ '$unwind': { path: '$agent', preserveNullAndEmptyArrays: true } },
{
$match: {
$and: [
{ $exams: { $elemMatch: { deletedAt: { $exists: false } } } },
{ $sportPracticed: { deletedAt: { $exists: false } } },
{ $galery: { $elemMatch: { deletedAt: { $exists: false } } } },
{ $evaluationPlayer: { $elemMatch: { deletedAt: { $exists: false } } } },
{ $evaluations: { $elemMatch: { deletedAt: { $exists: false } } } },
{ $category: { $elemMatch: { deletedAt: { $exists: false } } } },
{ $club: { deletedAt: { $exists: false } } },
{ $agent: { deletedAt: { $exists: false } } },
]
}
}
]
However, when this code is executed, it returns the error: unknown top level operator: $ exams.
How can I solve this problem ?
My plans is not to bring data from relationships where it has already been deleted and what says that it was deleted is the existence of the deleteadAt;
Collection Player:
[{
"_id": "5f0cc0e676de351ce21a752b",
"language": "pt-br",
"country": "BR",
"status": true,
"name": "Laura Silva",
"nickname": "laurasilva",
"dateOfBirth": "1995-05-01T00:00:00.000Z",
"email": "laurasilva#gmail.com",
"phones": [],
"cpf": "54721452365",
"gender": "F",
"father": "Luiz Silva",
"mother": "Larissa Silva",
"weight": "67",
"height": "1.67",
"currentTeam": {
"name": "América",
"initialDate": "2020-01-01"
},
"professionalPlayer": true,
"bird": "ARW1",
"idSports": "5f0cbe10e6c0930b8dcc5181",
"competitions": [],
"exams": [],
"preference": [],
"coachReferrals": [],
"createdAt": "2020-07-13T20:15:34.348Z",
"updatedAt": "2020-07-13T20:15:34.348Z",
"__v": 0,
"idAgent": null,
"idClub": null
},
{
"_id": "5ee644c0280583764bfe7d97",
"currentTeam": {
"name": "Cruzeiro",
"initialDate": "2019-02-25"
},
"network": {
"instagram": {
"url": "#renanmoaesoficial"
}
},
"language": "pt-br",
"country": "BR",
"status": true,
"name": "Renan Moraes",
"nickname": "renanmoraes",
"dateOfBirth": "1993-01-21T00:00:00.000Z",
"email": "renan.desenvolviemnto#gmail.com",
"phones": [
{
"_id": "5ee644c0280583764bfe7d98",
"phone": "(31) 98796-1357"
}
],
"cpf": "123.367.952-85",
"gender": "M",
"father": "Antonio",
"mother": "Sandra",
"weight": "1,80",
"height": "45",
"professionalPlayer": false,
"bird": "2551566655",
"idSports": "5ee640d040b0a0649799c531",
"exams": [
{
"results": [],
"attestation": [],
"_id": "5ee644c0280583764bfe7d9a",
"idExams": "5ee6405f061a1362c3778435"
},
{
"results": [],
"attestation": [],
"_id": "5ee644c0280583764bfe7d99",
"idExams": "5ee6406e061a1362c3778436"
}
],
"standardPhoto": "https://exame.com/wp-content/uploads/2018/10/capaprofile.jpg?quality=70&strip=info",
"competitions": [],
"preference": [],
"coachReferrals": [],
"createdAt": "2020-06-14T15:39:44.198Z",
"updatedAt": "2020-06-14T15:39:44.198Z",
"__v": 0,
"idAgent": null,
"idClub": "5f176c8d58beb94efe56c59b"
}]
Collection medicalExams:
{
"_id": "5ee6406e061a1362c3778436",
"status": true,
"name": "Exames das prostatas",
"value": "1",
"type": "A",
"createdAt": "2020-06-14T15:21:18.114Z",
"updatedAt": "2020-06-14T15:21:18.114Z",
"__v": 0
},
{
"_id": "5ee6405f061a1362c3778435",
"status": true,
"name": "Exames dos rins",
"value": "3",
"type": "M",
"createdAt": "2020-06-14T15:21:03.405Z",
"updatedAt": "2020-06-14T15:21:03.405Z",
"__v": 0
}
Here on this site there is an example of the error, in which case the two items would need to come ... https://mongoplayground.net/p/IgnIt8wGvKq
Key references in $match stage shouldn't begin with $ sign.
Remove $ sign from the $match stage.
Also, the deletedAtkey for last two conditions should not be in separate parenthesis.
{
$match: {
$and: [
{ exams: { $elemMatch: { deletedAt: { $exists: false } } } },
{ galery: { $elemMatch: { deletedAt: { $exists: false } } } },
{ evaluationPlayer: { $elemMatch: { deletedAt: { $exists: false } } } },
{ evaluations: { $elemMatch: { deletedAt: { $exists: false } } } },
{ category: { $elemMatch: { deletedAt: { $exists: false } } } },
{ "sportPracticed.deletedAt": { $exists: false } },
{ "club.deletedAt": { $exists: false } },
{ "agent.deletedAt": { $exists: false } },
]
}
}
Well, I managed to solve my problem .. It turns out that I needed to make a pipeline for each existing lookup relationship, careful that I was looking for the data in the correct way.
I had relationships that were made in an array, and I needed to match each item in my array with an id from another collection. And to achieve this I added let to declare variables to be used within my lookup and then use $ in to do specific searches for those ids ..
Anyway the final object was like this, I took some items that were also not important to make it simpler.
{
"$lookup": {
"from": "Exams",
"let": {
"exams_items": "$exams.idExams"
},
"pipeline": [
{
"$match": {
"$expr": {
"$in": [
"$_id",
"$$exams_items"
]
},
"deletedAt": {
$exists: false
}
}
}
],
"as": "allExams"
}
},
{
"$lookup": {
"from": "Sports",
"let": {
"id_sport": "$idSports"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$_id",
"$$id_sport"
]
},
"deletedAt": {
$exists: false
}
}
}
],
"as": "sportPracticed"
}
},
{
$unwind: {
"path": "$sportPracticed",
"preserveNullAndEmptyArrays": true
}
},
{
"$lookup": {
"from": "Galery",
"let": {
"id_player": "$_id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$idPlayer",
"$$id_player"
]
},
"deletedAt": {
$exists: false
}
}
}
],
"as": "galery"
}
},
{
"$lookup": {
"from": "EvaluationPlayer",
"let": {
"id_player": "$_id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$idPlayer",
"$$id_player"
]
},
"deletedAt": {
$exists: false
}
}
}
],
"as": "evaluationPlayer"
}
},
{
$lookup: {
from: "Club",
localField: "idClub",
foreignField: "_id",
as: "club"
}
},
{
$unwind: {
"path": "$club",
"preserveNullAndEmptyArrays": true
}
},
{
$lookup: {
from: "Agent",
localField: "idAgent",
foreignField: "_id",
as: "agent"
}
},
{
$unwind: {
"path": "$agent",
"preserveNullAndEmptyArrays": true
}
}