Je suis nouveau à mongodb et je veux obtenir les documents d'une autre collection basée sur le résultat que je reçois dans la réponse actuelle ci-dessous est le code que j'ai écrit à obtenir les données d'une autre collection, je suis en mesure d'obtenir les données mais je veux des données particulières en fonction de la condition.
Mes collections sont ci-dessous
Users
{
\t "_id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
\t "organization_name" : "Blazeautomation",
\t "emailId" : "[email protected]",
\t "password" : [
\t \t "$2a$10$vwexl4DTVVXFEb8toVZNj.wETfYPqUZum2HOvltEStISkgEc4pAYy"
\t ],
\t "phone_number" : 9177233096,
\t "organization_location" : {
\t \t "latitude" : 32.232323,
\t \t "longitude" : 33.2323
\t },
\t "isOrganization" : true,
\t "isAuthenticated" : true,
\t "OTP" : "194795",
\t "loginNumber" : 1
}
{
\t "_id" : ObjectId("59dcbc71fcce5d16c376a2a7"),
\t "organization_Id" : [
\t \t {
\t \t \t "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
\t \t \t "isActive" : 0
\t \t }
\t ],
\t "installer_first_name" : "raj",
\t "installer_last_name" : "raj",
\t "emailId" : "[email protected]",
\t "password" : [
\t \t "847471",
\t \t "$2a$10$w.YeBAzcw0Lvjttgv62EyefRQ.3aKFlMVRkAHJODSMiOwpaFMATku"
\t ],
\t "phone_number" : "9177233096",
\t "isOrganization" : false,
\t "isAuthenticated" : true,
\t "OTP" : "847471",
\t "loginNumber" : 1,
\t "isActive" : 0
}
{
\t "_id" : ObjectId("59dcbdbbfcce5d16c376a2aa"),
\t "organization_Id" : [
\t \t {
\t \t \t "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
\t \t \t "isActive" : 0
\t \t }
\t ],
\t "installer_first_name" : "raj",
\t "installer_last_name" : "raj",
\t "emailId" : "[email protected]",
\t "password" : [
\t \t "593548"
\t ],
\t "phone_number" : "9177233096",
\t "isOrganization" : false,
\t "isAuthenticated" : false,
\t "OTP" : "593548",
\t "loginNumber" : 0,
\t "isActive" : 0
}
Projects
{
\t "_id" : ObjectId("59dcbc1ffcce5d16c376a2a6"),
\t "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
\t "project_name" : "tessdfgtf12ffffd3g4dd",
\t "project_description" : "Testing API's",
\t "time_stamp" : "2012-11-04T14:51:06.157Z",
\t "images" : {
\t \t "path" : "ssdd/sdfsdf",
\t \t "fileName" : "FirstImageForAPITest.png",
\t \t "ContentType" : "png",
\t \t "image" : "https://vlestreetlightbucket.s3.amazonaws.com/FirstImageForAPITest.png"
\t },
\t "installer_id" : [
\t \t {
\t \t \t "installer_id" : ObjectId("59dcbc71fcce5d16c376a2a7"),
\t \t \t "installerIsActive" : 0
\t \t },
\t \t {
\t \t \t "installer_id" : ObjectId("59dcbdbbfcce5d16c376a2aa"),
\t \t \t "installerIsActive" : 0
\t \t }
\t ],
\t "projectIsActive" : 0
}
{
\t "_id" : ObjectId("59dcbdf2fcce5d16c376a2ac"),
\t "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
\t "project_name" : "test2",
\t "project_description" : "Testing API's",
\t "time_stamp" : "2012-11-04T14:51:06.157Z",
\t "images" : {
\t \t "path" : "ssdd/sdfsdf",
\t \t "fileName" : "FirstImageForAPITest.png",
\t \t "ContentType" : "png",
\t \t "image" : "https://vlestreetlightbucket.s3.amazonaws.com/FirstImageForAPITest.png"
\t },
\t "installer_id" : [
\t \t {
\t \t \t "installer_id" : ObjectId("59dcbc71fcce5d16c376a2a7"),
\t \t \t "installerIsActive" : 0
\t \t },
\t \t {
\t \t \t "installer_id" : ObjectId("59dcbdbbfcce5d16c376a2aa"),
\t \t \t "installerIsActive" : 0
\t \t }
\t ],
\t "projectIsActive" : 0
}
Assignments
{
\t "_id" : ObjectId("59dcbcddfcce5d16c376a2a8"),
\t "projectId" : ObjectId("59dcbc1ffcce5d16c376a2a6"),
\t "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
\t "installer_id" : ObjectId("59dcbc71fcce5d16c376a2a7"),
\t "isActive" : 0
}
{
\t "_id" : ObjectId("59dcbdcbfcce5d16c376a2ab"),
\t "projectId" : ObjectId("59dcbc1ffcce5d16c376a2a6"),
\t "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
\t "installer_id" : ObjectId("59dcbdbbfcce5d16c376a2aa"),
\t "isActive" : 0
}
{
\t "_id" : ObjectId("59dcbe5cfcce5d16c376a2ad"),
\t "projectId" : ObjectId("59dcbdf2fcce5d16c376a2ac"),
\t "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
\t "installer_id" : ObjectId("59dcbdbbfcce5d16c376a2aa"),
\t "isActive" : 0
}
{
\t "_id" : ObjectId("59dcbe5cfcce5d16c376a2ae"),
\t "projectId" : ObjectId("59dcbdf2fcce5d16c376a2ac"),
\t "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
\t "installer_id" : ObjectId("59dcbc71fcce5d16c376a2a7"),
\t "isActive" : 0
}
Mon code pour récupérer les enregistrements
db.Projects.aggregate([ {$match:{"_id":{"$in":arrayOfIds}}},
{$unwind:"$installer_id"},
{"$lookup":{from:'Users',localField:'installer_id.installer_id',foreignField:'_id',as:'installer_id'}},
{$unwind:"$installer_id"},
{$group:{_id:"$_id","organization_Id":{$first:"$organization_Id"},"project_name":{$first:"$project_name"},"project_description":{$first:"project_description"},"time_stamp":{$first:"$time_stamp"},"project_logo":{$first:"$project_logo"},"projectIsActive":{$first:"$projectIsActive"},installer_id:{"$push":"$installer_id"}}},
{$project:{"_id":1,"organization_Id":1,"project_name":1,"project_description":1,"time_stamp":1,"project_logo":1,"projectIsActive":1,"installer_id._id":1,"installer_id.installer_first_name":1,"installer_id.installer_last_name":1,"installer_id.emailId":1}},
{$unwind:"$installer_id"},
{"$lookup":{from:'Assignments',localField:'installer_id._id',foreignField:'installer_id',as:'installer_id.assignments1'}},
{$unwind:"$installer_id.assignments1"},
{$group:{_id:"$_id","organization_Id":{$first:"$organization_Id"},"project_name":{$first:"$project_name"},"project_description":{$first:"project_description"},"time_stamp":{$first:"$time_stamp"},"project_logo":{$first:"$project_logo"},"projectIsActive":{$first:"$projectIsActive"},installer_id:{"$push":"$installer_id"}}}]).pretty()
et je recevais comme résultat
[{
"_id": "59d4b35ac4b6210eaa151b34",
"organization_Id": "59d4acb2d162660d6bf14416",
"project_name": "test123",
"project_description": "project_description",
"time_stamp": "2012-11-04T14:51:06.157Z",
"project_logo": {
"path": "ssdd/sdfsdf",
"filename": "FirstImageForAPITest.png",
"ContentType": "png",
"image": "https://vlestreetlightbucket.s3.amazonaws.com/FirstImageForAPITest.png"
},
"projectIsActive": 0,
"installer_id": [
{
"_id": "59d4accbd162660d6bf14417",
"installer_first_name": "uttam",
"installer_last_name": "kumar",
"emailId": "[email protected]",
"assignments1": [
{
"_id": "59d62664e3387e0b985c260d",
"projectId": "59d4b35ac4b6210eaa151b34",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59d631d1847b140c679ae4dd",
"projectId": "59d4ed2c35132a16073c0375",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59db1541cb828d203a708dcf",
"projectId": "59db14d7cb828d203a708dce",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
}
]
},
{
"_id": "59d4accbd162660d6bf14417",
"installer_first_name": "uttam",
"installer_last_name": "kumar",
"emailId": "[email protected]",
"assignments1": [
{
"_id": "59d62664e3387e0b985c260d",
"projectId": "59d4b35ac4b6210eaa151b34",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59d631d1847b140c679ae4dd",
"projectId": "59d4ed2c35132a16073c0375",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59db1541cb828d203a708dcf",
"projectId": "59db14d7cb828d203a708dce",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
}
]
},
{
"_id": "59d4accbd162660d6bf14417",
"installer_first_name": "uttam",
"installer_last_name": "kumar",
"emailId": "[email protected]",
"assignments1": [
{
"_id": "59d62664e3387e0b985c260d",
"projectId": "59d4b35ac4b6210eaa151b34",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59d631d1847b140c679ae4dd",
"projectId": "59d4ed2c35132a16073c0375",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59db1541cb828d203a708dcf",
"projectId": "59db14d7cb828d203a708dce",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
}
]
},
{
"_id": "59d4accbd162660d6bf14417",
"installer_first_name": "uttam",
"installer_last_name": "kumar",
"emailId": "[email protected]",
"assignments1": [
{
"_id": "59d62664e3387e0b985c260d",
"projectId": "59d4b35ac4b6210eaa151b34",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59d631d1847b140c679ae4dd",
"projectId": "59d4ed2c35132a16073c0375",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59db1541cb828d203a708dcf",
"projectId": "59db14d7cb828d203a708dce",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
}
]
},
{
"_id": "59d4accbd162660d6bf14417",
"installer_first_name": "uttam",
"installer_last_name": "kumar",
"emailId": "[email protected]",
"assignments1": [
{
"_id": "59d62664e3387e0b985c260d",
"projectId": "59d4b35ac4b6210eaa151b34",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59d631d1847b140c679ae4dd",
"projectId": "59d4ed2c35132a16073c0375",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59db1541cb828d203a708dcf",
"projectId": "59db14d7cb828d203a708dce",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
}
]
},
{
"_id": "59d4accbd162660d6bf14417",
"installer_first_name": "uttam",
"installer_last_name": "kumar",
"emailId": "[email protected]",
"assignments1": [
{
"_id": "59d62664e3387e0b985c260d",
"projectId": "59d4b35ac4b6210eaa151b34",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59d631d1847b140c679ae4dd",
"projectId": "59d4ed2c35132a16073c0375",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
},
{
"_id": "59db1541cb828d203a708dcf",
"projectId": "59db14d7cb828d203a708dce",
"organization_Id": "59d4acb2d162660d6bf14416",
"installer_id": "59d4accbd162660d6bf14417",
"isActive": 0
}
]
}
]
}]
Je veux comparer projectId
avec le champ _id
qui est au-dessus du champ organization_Id
et obtenez seulement égalé les résultats que j'ai essayé d'utiliser match de {$match:{"installer_id.assignments1.projectId":"_id"}}
mais je ne reçois pas un tableau vide quelqu'un peut-il s'il vous plaît aider à obtenir le résultat du désir .
Affichez vos exemples de documents pour chaque collection. –
Mis à jour ma question @Clement Amarnath –
Il y a beaucoup de champs distrayants et vous dites * pas * obtenir tableau vide mais je pense que vous voulez dire que vous * obtenez * un tableau vide mais essayez cela pour les débutants - vous avez oublié le $ devant _id: {"installer_id.assignments1.projectId ":" $ _ id "}} –