mongoose $group (aggregation) returns duplicates - node.js

I have a JobApplication collection which contains a jobseeker reference like so:
_id
job_post
jobseeker
60b62fcf36c4bd19fc2a1733
60a59ca4b43549049baef311
60802ba5be283f6ffcf07821
and I want to find the number of jobseekers grouped by theirs countries
I tried to do it like this:
return await JobApplication.aggregate([
{
$lookup: {
from: Jobseeker.collection.collectionName,
localField: 'jobseeker',
foreignField: '_id',
as: 'jobseeker'
}
},
{
$unwind: '$jobseeker'
},
{
$group: {
_id: "$jobseeker.current_country_of_residence",
count: { $sum: 1 }
}
}
])
but I'm getting a duplicated data like this:
"applicants_by_locations": [
{
"_id": "606f4d2bbca7c6f2be0625f9",
"count": 1
},
{
"_id": "606f4d2bbca7c6f2be0625f9",
"count": 2
}]
I'm getting the same country _id twice instead of one _id with count: 3.
I'm not sure what's going on here!!
Updated Code:
await JobApplication.aggregate([
{
$lookup: {
from: Jobseeker.collection.collectionName,
localField: 'jobseeker',
foreignField: '_id',
as: 'jobseeker'
} },
{ $unwind: '$jobseeker' },
{ $group: {
_id: "$jobseeker.current_country_of_residence",
count: { $sum: 1 }
} },
{
$group: {
_id: "$_id",
count: { $sum: "$count" }
} }
])

Related

lookup with add extra field in mongodb

My OBJ
[{
_id:XXXXXXXXXX,
role:admin
},
{
_id:XXXXXXXXXX,
role:superUser
}]
and need results using aggregation how to solve this using aggregation
[{
name:'username'
role:'test'
}
]
I suppose you need the following
let db1 = db.get().collection(`temp1`);
let db2 = db.get().collection(`temp2`);
await db1.aggregate([
{
$lookup: {
from: "temp2",
localField: "_id", // field in the orders collection
foreignField: "_id", // field in the items collection
as: "users"
}
},
{
$replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ["$users", 0] }, "$$ROOT"] } }
},
{ $project: { users: 0 } }
]).toArray()

Convert from localField to let - $lookup

I'm trying to adapt my code so I could after use the pipeline to $project only the fields I need.
This first code is achieving the result expected;
Order.aggregate([
{ $unwind: "$candidate" },
{ $match: { "candidate.groupId": "A" } },
{
$lookup: {
from: "users",
localField: "candidate.autonomousId",
foreignField: "_id",
as: "candidateData",
},
},
]).toArray();
The second one doesn't return the same output. It returns an empty candidateData.
Order.aggregate([
{ $unwind: "$candidate" },
{ $match: { "candidate.groupId": "A" } },
{
$lookup: {
from: "users",
let: { id: "$candidate.autonomousId" },
pipeline: [{ $match: { $expr: { $eq: ["$_id", "$$id"] } } }],
as: "candidateData",
},
},
]).toArray();
What am I doing wrong when I try to write in the second way? I couldn't find my syntax mistake.
Since candidate.autonomousId is an array (contrary to its name) and you are using the $eq operator to compare $$id (array) with _id (ObjectId), the pipeline returns no User documents in candidateData. You should use the $in aggregation operator instead.
db.orders.aggregate([
{
$unwind: "$candidate"
},
{
$match: {
"candidate.groupId": "A"
}
},
{
$lookup: {
from: "users",
let: {
id: "$candidate.autonomousId"
},
pipeline: [
{
$match: {
$expr: {
$in: [
"$_id",
"$$id"
]
}
}
}
],
as: "candidateData",
},
},
])
MongoPlayground
I would recommend renaming the let variable ids for clarity.

How to lookup inside lookup in MongoDB Aggregate?

I have a simple 3 collections. This bellow is their pseudocode. I want to get all shipments and for each shipment, I want to have all bids for that shipment and in each bid, I need userDetails object.
User: {
name: string,
}
Shipment: {
from: string,
to: string
}
Bid: {
amount: number,
shipmentId: Ref_to_Shipment
userId: Ref_to_User
}
This is what I have tried:
const shipments = await ShipmentModel.aggregate([
{
$lookup: {
from: "bids",
localField: "_id",
foreignField: "shipmentId",
as: "bids"
}
},
{
$lookup: {
from: "users",
localField: "bids.userId",
foreignField: "_id",
as: "bids.user"
}
}
])
And I got the following result:
[
{
"_id": "5fad2fc04458ac156531d1b1",
"from": "Belgrade",
"to": "London",
"__v": 0,
"bids": {
"user": [
{
"_id": "5fad2cdb4d19c80d1b6abcb7",
"name": "Amel",
"email": "Muminovic",
"password": "d2d2d2",
"__v": 0
}
]
}
}
]
I am trying to get all Shipments with their bids and users within bids. Data should look like:
[
{
"_id": "5fad2fc04458ac156531d1b1",
"from": "Belgrade",
"to": "London",
"__v": 0,
"bids": [
{
"_id": "5fad341887c2ae1feff73402",
"amount": 400,
"userId": "5fad2cdb4d19c80d1b6abcb7",
"shipmentId": "5fad2fc04458ac156531d1b1",
"user": {
"name": "Amel",
}
"__v": 0
}
]
}
]
Try with the following code:
const shipments = await ShipmentModel.aggregate([
{
$lookup: {
from: "bids",
localField: "_id",
foreignField: "shipmentId",
as: "bids"
}
},
{
$unwind: {
path: "$bids",
preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: "users",
localField: "bids.userId",
foreignField: "_id",
as: "bids.user"
}
}
])
If you want to prevent null and empty arrays then set
preserveNullAndEmptyArrays: false
Try this query and chek if works and the behaviour is as you expected:
db.Shipment.aggregate([
{
$lookup: {
from: "Bid",
localField: "id",
foreignField: "shipmentId",
as: "bids"
}
},
{
$lookup: {
from: "user",
localField: "id",
foreignField: "id",
as: "newBids"
}
},
{
$project: {
"newBids.id": 0,
"newBids._id": 0,
}
},
{
$match: {
"bids.userId": 1
}
},
{
$addFields: {
"newBids": {
"$arrayElemAt": [
"$newBids",
0
]
}
}
},
{
$set: {
"bids.user": "$newBids"
}
},
{
$project: {
"newBids": 0
}
}
])
This query do your double $lookup and then a $project to delete the fields you don't want, and look for the userId to add the field user. As $lookup generate an array, is necessary use arrayElemAt to get the first position.
Then $set this value generated into the object as bids.user and remove the old value.
Note that I have used a new field id instead of _id to read easier the data.
Try this
I figured out it based on MongoDB $lookup on array of objects with reference objectId and in the answer from J.F. (data organization). Note that he used id instead of _id
The code is
db.Shipment.aggregate([
{
$lookup: {
from: "Bid",
localField: "id",
foreignField: "shipmentId",
as: "bids"
}
},
{
$lookup: {
from: "user",
localField: "bids.userId",
foreignField: "id",
as: "allUsers"
}
},
{
$set: {
"bids": {
$map: {
input: "$bids",
in: {
$mergeObjects: [
"$$this",
{
user: {
$arrayElemAt: [
"$allUsers",
{
$indexOfArray: [
"$allUsers.id",
"$$this.userId"
]
}
]
}
}
]
}
}
}
}
},
{
$unset: [
"allUsers"
]
},
// to get just one
//{
// $match: {
// "id": 1
// }
// },
])

Mongodb lookup aggregation not getting all field values

I have two collections which are of schema like
driver
_id:5f9c1d897ea5e246945cd73a
agent_name:"Ratnabh Kumar Rai"
agent_email:"ra2614#gmail.com"
agent_phone:"70****63331"
and
reviews
_id:5f9d54cb3a3ee10c6829c0a4
order_id:"5f9d40f096e4a506e8684aba"
user_id:"5f9bcb66f7a5bf4be0ad9973"
driver_id:"5f9c1d897ea5e246945cd73a"
rating:3
text:""
so i want to calculate the avg driver rating . I tried to use lookup aggregation so that i can get all details and then later calculate the sum...what i did was
let doc = await db
.collection("drivers")
.aggregate([
{
$project: {
_id: {
$toString: "$_id",
},
},
},
{
$lookup: {
from: "reviews",
localField: "_id",
foreignField: "driver_id",
as: "driver_info",
},
},
{
$project: {
agent_email: 1,
orderReview: "$driver_info",
},
},
])
.toArray();
But i am getting result as
{
_id: '5f9d63eb8737e82fbc193dd9',
orderReview: [ [Object], [Object], [Object], [Object], [Object] ]
}
which is partially correct as i also need to get details from my localfield collection that is drivers field, as of now you can see i am only getting id of driver in my projection i also did "agent_email:1" but not getting email
You're actually only projecting _id in the first pipeline and hence only _id is passed to further pipelines, If you need email in further pipelines you need to project it too
let doc = await db.collection("drivers").aggregate([
{
$project: {
_id: {
$toString: "$_id",
},
agent_email: "$agent_email"
},
},
{
$lookup: {
from: "reviews",
localField: "_id",
foreignField: "driver_id",
as: "driver_info",
},
},
{
$project: {
agent_email: 1,
orderReview: "$driver_info",
},
},
])
MongoDB PlayGround : https://mongoplayground.net/p/h7D-tYJ7sLU
[Update]
I realized that you're doing this for getting average and if you need it to be done in a single aggregate query, here it is how you can do it.
Using unwind operator you can flat the reviews array as objects and then group by _id and use the $avg aggregation operator
db.collection("drivers").aggregate([
{
$project: {
_id: {
$toString: "$_id",
},
agent_email: "$agent_email"
},
},
{
$lookup: {
from: "reviews",
localField: "_id",
foreignField: "driver_id",
as: "driver_info",
},
},
// Makes the driver info flat with other information
{
"$unwind": "$driver_info"
},
{
// Now you can group them
$group: {
_id: "$_id",
// Calculates avg on rating field
avg: {
"$avg": "$driver_info.rating"
},
// To make email field appear in next pipeline.
// You can do it for other fields too like Name, etc
agent_email: {
$first: "$agent_email"
}
}
},
{
$project: {
// select the fields you want to display
agent_email: 1,
avg: 1
},
},
])
MonogoDb playground Link

Sending in a MongoDB Array to get results from a different collection in MongoDB

I have a MongoDB Collection that contains timecards. I have a 2nd collection that contains locations. Normally I would do this if I was looking for anyone that was oncall at the time (you have to have a signout for every signin):
TimeCard.aggregate([
{ $match: {agency_id: req.query.agency_id}},
{ $sort: { user_id: 1, received_time: -1 }},
{ $group: { _id: "$user_id", count_types: { $sum: 1 }, lastTime: { $last: "$received_time" }}},
{ $match: { count_types: { $mod: [2, 1] }}},
{ $lookup: { from: "locations", localField: "_id", foreignField: "user_id", as: "Test" }}
], function(err, docs){
if(err){console.log(err);}
else {res.json(docs);}
});
And this would give me this result:
[
{
"_id": "123-88",
"count_types": 5,
"lastTime": "2017-04-20T01:30:18.713Z",
"Test": [
{
"_id": "58fa4021ffa99100116585e0",
"user_id": "123-88",
"agency_id": "44",
"contract_region_id": "contract-region-id-007",
"department_id": "department-id-42",
"shift_id": "shift-id-45",
"unit_id": "unit-id-88",
"received_time": "2017-04-21T17:23:45.672Z",
"location": "Science Lab",
"__v": 0
},
{
"_id": "58fed3efdac1bd00112a914b",
"user_id": "123-88",
"agency_id": "44",
"contract_region_id": "contract-region-id-007",
"department_id": "department-id-42",
"shift_id": "shift-id-45",
"unit_id": "unit-id-88",
"received_time": "2017-04-25T04:43:27.501Z",
"location": "Gym",
"__v": 0
}
]
}
]
Now I could have multiple users in the array all with their own location data. What I want is only the Last location they were at (based on received_time inside Test array). So my best guess is that I would need to first just get a list of user_ids and then call the second collection and pass in the array to get the results but I am not sure how to do this correctly or if that is even the best way to do this. Thanks again for your help.
Use the $filter operator to get the only element in the Test array with the received_time key matching the aggregated lastTime field. You can apply this within an $addFields pipeline step if your MongoDB Server version is 3.4 or greater:
TimeCard.aggregate([
{ $match: {agency_id: req.query.agency_id}},
{ $sort: { user_id: 1, received_time: -1 }},
{ $group: { _id: "$user_id", count_types: { $sum: 1 }, lastTime: { $last: "$received_time" }}},
{ $match: { count_types: { $mod: [2, 1] }}},
{ $lookup: { from: "locations", localField: "_id", foreignField: "user_id", as: "Test" }},
{
$addFields: {
Test: {
$filter: {
input: "$Test",
as: "user",
cond: { $eq: ["$lastTime", "$$user.received_time"] }
}
}
}
}
], function(err, docs){
if(err){console.log(err);}
else {res.json(docs);}
});
If your MongoDB server does not have support for $addFields then use the $project pipeline instead:
TimeCard.aggregate([
{ $match: {agency_id: req.query.agency_id}},
{ $sort: { user_id: 1, received_time: -1 }},
{ $group: { _id: "$user_id", count_types: { $sum: 1 }, lastTime: { $last: "$received_time" }}},
{ $match: { count_types: { $mod: [2, 1] }}},
{ $lookup: { from: "locations", localField: "_id", foreignField: "user_id", as: "Test" }},
{
$project: {
count_types: 1,
lastTime: 1,
Test: {
$filter: {
input: "$Test",
as: "user",
cond: { $eq: ["$lastTime", "$$user.received_time"] }
}
}
}
}
], function(err, docs){
if(err){console.log(err);}
else {res.json(docs);}
});

Resources