MongoDB join/lookup not working when using a pipeline - mongodb

using a join in MongoDb I can get the values I want
(await this.ideasCollection).aggregate([
{
$match: { userId, deleted: { $ne: true } },
},
{
$lookup: {
from: 'teams',
localField: '_id',
foreignField: `ideas.daily.ideaId`,
as: 'teams',
},
},
])
but when I try to use a pipeline in order to get fewer fields(using $project) it doesn't work
(await this.ideasCollection).aggregate([
{
$match: { userId, deleted: { $ne: true } },
},
{
$lookup: {
from: 'teams',
let: { ideaId: '$_id' },
pipeline: [
{ $match: { $expr: { $eq: ['$ideas.daily.ideaId', '$$ideaId'] } } },
{ $project: { _id: 1, name: 1 } },
],
as: 'teams',
},
},
])
Is there some kind of trick to make the second option work?
Here's the structure of teams
{
"_id": { "$oid": "58948652ab5f580010faeb44" },
"name": "My team",
"ideas": {
"daily": [
{ "ideaId": { "$oid": "5b6d913e3e552800260904e1" }, "ordinal": 0 },
{ "ideaId": { "$oid": "5bbbc93459914f0013e3c522" }, "ordinal": 1 },
]
}
}
Thank you

Related

Mongo query for lookup array of keys which is itself an item in a nested array

My first collection is as below, I am searching the document with the email and match the particular jobid inside the jobs array. Then insert the document of second collection by matching _id with jobs.Process.profile_id.
{
"_id": {
"$oid": "6229d3cfdbfc81a8777e4821"
},
"jobs": [
{
"job_ID": {
"$oid": "62289ded8079821eb24760e0"
},
"Process": [
{
"profile_id": {
"$oid": "6285e571681188e83d434797"
}
},
{
"profile_id": {
"$oid": "6285e571681188e83d434799"
}
}
],
},
{
"job_ID": {
"$oid": "6228a252fb4554dd5c48202a"
},
"Process": [
{
"profile_id": {
"$oid": "62861067dc9771331e61df5b"
}
}
],
},
{
"job_ID": {
"$oid": "622af1c391b290d34701af9f"
},
"Process": [
""
],
}
],
"email": "********#gmail.com"
}
and my second collection is, I need to insert this document in my first collection by matching with jobs.Process.profile_id.
{
"_id": {
"$oid": "6285e571681188e83d434797"
},
"Name": "Lakshdwanan",
"Location":"California"
}
I have tried with query,
aggregate([
{ $match: { email: email } },
{
$lookup: {
from: 'user__profiles',
localField: 'jobs.Process.profile_id',
foreignField: '_id',
as: 'jobings',
},
},
{
$addFields: {
jobings: {
$map: {
input: {
$filter: {
input: '$jobs',
as: 'm',
cond: {
$eq: ['$$m.job_ID', objInstance],
},
},
},
as: 'm',
in: {
$mergeObjects: [
{
$arrayElemAt: [
{
$filter: {
input: '$jobings',
cond: {
$eq: ['$$this._id', '$$m.Process.profile_id'],
},
},
},
0,
],
},
'$$m',
],
},
},
},
},
},
{
$project: {
jobings: 1,
_id: 0,
},
},
]);
My output should only display second collection document based on the first collection document matching.
EDIT: If you want the data for a specific job only, it is better to $filter the jobs before the $lookup step. After the $lookup, just $unwind and format:
db.firstCol.aggregate([
{
$match: {email: email}
},
{
$project: {
jobs: {
$filter: {
input: "$jobs",
as: "item",
cond: {$eq: ["$$item.job_ID", objInstance]}
}
},
_id: 0
}
},
{
$lookup: {
from: "user__profiles",
localField: "jobs.Process.profile_id",
foreignField: "_id",
as: "jobings"
}
},
{
$project: {res: "$jobings", _id: 0}
},
{
$unwind: "$res"
},
{
$replaceRoot: {newRoot: "$res"}
}
])
Playground
The jobs.Process.profile_id is the user__profiles _id, so no need to merge anything...The results are documents from user__profiles collection "as is" but they can be formatted as wanted..._id key name can be renamed profile_id easily.

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
}
}
])

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

if condtion MongoDB aggregation

I need to display the currentLocation of which the process is completed,which are presented in four different collections. It's a chain process, in the order of labtest, inwards, goods, production. Each of them is different collections. I want the result in which, if labtest status is completed it goes to inwards, if inwards status also completed, then it goes and checks on goods. If goods status is In-Progress, it should return Inwards as currentLocation.
But, I am getting a response as Production....it seems like me if the condition is not working. Please help me to solve this issue
async function getGrnDetails(userParam) {
var user = User.findOne({ grnNo: userParam.grnNo })
var userss = User.aggregate([
{
$match:
{
grnNo: userParam.grnNo
}
}, {
$lookup: {
from: "goods",
let: { grnNo: "$grnNo" },
pipeline: [
{
$match: {
$expr:
{
$eq: ["$grnNo", '$$grnNo']
}
}
},
],
as: "Goods",
},
},
{
$lookup: {
from: "inwards",
let: { grnNo: "$grnNo" },
pipeline: [
{
$match: {
$expr:
{
$eq: ["$grnNo", '$$grnNo']
}
}
}
],
as: "Inwards",
}
},
{
$lookup: {
from: "productions",
let: { grnNo: "$grnNo" },
pipeline: [
{
$match: {
$expr:
{
$eq: ["$grnNo", '$$grnNo']
}
}
},
],
as: "Productions",
}
},
{
$unwind: {
"path": "$Productions"
}
},{
$unwind: {
"path": "$status"
}
},{
$unwind: {
"path": "$Goods"
}
},
{
$unwind: {
"path": "$Inwards"
}
},
{
$group: {
"_id": {
beforeHeadSettingArray: '$beforeHeadSettingArray',
beforeWashingArray: '$beforeWashingArray', id: '$id',
status: '$status', defaultAccountId: '$defaultAccountId',
beforeStenterArray: '$beforeStenterArray',
beforeCompactingArray: '$beforeCompactingArray',
afterCompactingArray: '$afterCompactingArray',
status: "$status",
Goods: '$Goods.processArray', Inwards: '$Inwards.fabricArray', Production: '$Productions.operationDisplay', currentLocation: {
$cond: {
if: {
$strcasecmp: ["$status", "Completed"]
}, then: {
$cond: {
if: {
$strcasecmp: ["$Inwards.status", "Completed"]
},
then: {
$cond: {
if: {
$strcasecmp: ["$Goods.status", "Completed"]
},
then:
{
$cond: {
if: {
$strcasecmp: ["$Productions.status", "Completed"]
},
then: "Productions",
else: "Goods"
}
},
else: "Inwards"
}
},
else: "LabTest"
}
},
else: "Yet To be Started"
}
}
},
"ProgressArray": {
$addToSet: {
"Inwards": "$Inwards.status",
"Goods": "$Goods.status",
"Productions": "$Productions.status",
"LabTest": "$status"
},
}
}
} ,{ $sort: { _id: 1 } },
{ $limit: 1 }
]);
return await userss
}
The Problem is I used strcasecmp, when I changed to eq it worked perfectly
currentLocation: {
$cond: {
if: {
$eq: ["$status", "Completed"]
}, then: {
$cond: {
if: {
$eq: ["$Inwards.status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$Goods.status", "Completed"]
},
then:
{
$cond: {
if: {
$eq: ["$Productions.status", "Completed"]
},
then: "Finished",
else: "Productions"
}
},
else: "Goods"
}
},
else: "Inwards"
}
},
else: "LabTest"
}
}
,

How to do lookup on an aggregated collection in mongodb that is being grouped?

For some reason, I can't retrieve the author name from another collection on my aggregate query.
db.getCollection('books').aggregate([
{
$match: {
authorId: { $nin: [ObjectId('5b9a008575c50f1e6b02b27b'), ObjectId('5ba0fb3275c50f1e6b02b2f5'), ObjectId('5bc058b6ae9a2a4d6df330b1')]},
isBorrowed: { $in: [null, false] },
status: 'ACTIVE',
},
},
{
$lookup: {
from: "authors",
localField: "authorId", // key of author id in "books" collection
foreignField: "_id", // key of author id in "authors" collection
as: "bookAuthor",
}
},
{
$group: {
_id: {
author: '$authorId',
},
totalSalePrice: {
$sum: '$sale.amount',
},
},
},
{
$project: {
author: '$_id.author',
totalSalePrice: '$totalSalePrice',
authorName: '$bookAuthor.name', // I can't make this appear
_id: 0,
},
},
{ $sort: { totalSalePrice: -1 } },
])
Any advice on where I had it wrong? Thanks for the help.
Two things that are missing here: you need $unwind to convert bookAuthor from an array into single object and then you need to add that object to your $group stage (so that it will be available in next stages), try:
db.getCollection('books').aggregate([
{
$match: {
authorId: { $nin: [ObjectId('5b9a008575c50f1e6b02b27b'), ObjectId('5ba0fb3275c50f1e6b02b2f5'), ObjectId('5bc058b6ae9a2a4d6df330b1')]},
isBorrowed: { $in: [null, false] },
status: 'ACTIVE',
},
},
{
$lookup: {
from: "authors",
localField: "authorId",
foreignField: "_id",
as: "bookAuthor", // this will be an array
}
},
{
$unwind: "$bookAuthor"
},
{
$group: {
_id: {
author: '$authorId',
},
bookAuthor: { $first: "$bookAuthor" },
totalSalePrice: {
$sum: '$sale.amount',
},
},
},
{
$project: {
author: '$_id.author',
totalSalePrice: '$totalSalePrice',
authorName: '$bookAuthor.name',
_id: 0,
},
},
{ $sort: { totalSalePrice: -1 } },
])
Actually you have lost the bookAuthor field in the $group stage. You have to use $first accumulator to get it in the next $project stage.
{ "$group": {
"_id": { "author": "$authorId" },
"totalSalePrice": { "$sum": "$sale.amount" },
"authorName": { "$first": "$bookAuthor" }
}},
{ "$project": {
"author": "$_id.author",
"totalSalePrice": "$totalSalePrice",
"authorName": { "$arrayElemAt": ["$bookAuthor.name", 0] }
"_id": 0,
}}