Applying condition on localfield in aggregate in mongodb - mongodb

I have two collections user and transaction, transaction has two fields customer and seller , and user has two fields name and email., user collection contains data of customers and sellers
I want output as if the email passed belongs to customer then seller details should come from lookup and if email passed belongs to seller then customer details should come from lookup
What I have done is, I am passing the value email dynamically , email value can belongs to customer or seller , If email matches seller then I want the localfield in lookup as customer and vice versa. Following is what I have tried.
const email = req.email;
transaction.aggregate([{
$match: {
$or: [{
customer: email,
},
{
seller: email,
},
],
},
},
{
$lookup: {
from: "user",
localField: {
$cond: [{
if: {
$eq: ["$customer", email]
},
then: "seller",
else: "customer",
}],
},
foreignField: "email",
as: "user",
},
},
{
$unwind: "$user"
},
]);
But for the above query error comes as below
$lookup argument 'localField' must be a string
I am using nodejs, express and mongoose.

The localField allows only string, you can use lookup with pipeline,
let to define your conditions logic
pipeline to put your matching condition with lookup collection's field
{
$lookup: {
from: "user",
let: {
localField: {
$cond: [{
if: { $eq: ["$customer", email] },
then: "$seller",
else: "$customer",
}]
}
},
pipeline: [
{ $match: { $expr: { $eq: ["$email", "$$localField"] } } }
],
as: "user"
}
}

Related

Applying $exists to MongoDB aggregation

I have two mongo collections structured like so:
customers
{
_id: ObjectId,
name: String,
companyId: ObjectId
}
companies
{
_id: ObjectId,
name: String,
rights: [
add: boolean,
edit: boolean,
shop: boolean
]
}
So each customer has a companyId that lets us look up the companies.rights available. I need to get a list of which companies have customers but don't have the shop property at all.
So far I have this:
db.getCollection('customers').aggregate([
{
$match: {}
},
{
$lookup: {
from: 'companies',
localField: 'companyId',
foreignField: '_id',
as: 'company'
}
},
{
$project: {
companyId: '$company._id',
companyName: '$company.name',
shopSetting: '$company.rights.shop'
}
}
])
This seems to be working ok to give me all of the companies with their shop value (true or false). But what if I only want to see the companies that don't have the shop field existing at all? How would I modify this query to accomplish that? I've tried reading up on the $exists field in mongo, but this is all pretty new to me so I'm not sure where to apply it here.
Note: I need to query the companies from the customers collection because there are some companies without customers and I need this result to only be companies that are assigned to customers but don't have the rights.shop property existing
db.customers.aggregate([
{ $match: {} },
{
$lookup: {
from: "companies",
localField: "companyId",
foreignField: "_id",
as: "company",
pipeline: [
{
$match: {
$expr: {
$eq: [
{
$filter: {
input: "$rights",
as: "r",
cond: "$$r.shop"
}
},
[]
]
}
}
}
]
}
},
{
$project: {
companyId: "$company._id",
companyName: "$company.name",
shopSetting: "$company.rights.shop"
}
}
])
mongoplayground

get collection data based on id fetched in listing in nested lookups and conditional lookup in mongodb aggregation

I just need following output
{
message: "Details are:",
status: 1,
data:[
{
leadId: "92106",
projectName: "Sales Rep Mobile App with Shopify Backend"
projectOverview: "<any description>"
notificationsData:[
{
_id:"6076e2593580d805814c338e",
content:"<strong>User Jangid</strong> posted a comment about this message on estimation portal.",
estimationId:"5f75a496c70f05559088d971",
commentId:"6076e2583580d805814c338b",
commentData:{
_id:"6076e2583580d805814c338b",
content:"<p>hello krishna this is and this is second </p>\n"
}
},
{
_id:"6077c7c75c1bfc051f8dff3e",
content:"<strong>User Nunna</strong> posted a comment about this message on estimation portal.",
estimationId:"5f75a496c70f05559088d971",
commentId:"6077c7c35c1bfc051f8dff3b",
commentData:{
_id:"6077c7c35c1bfc051f8dff3b",
content:"<p>hey hiiii</p>\n\n<p> </p>\n"
}
},
],
userName:"user Nunna",
profileImage:"profile url",
isViewed:true,
isSortByStatus:2,
isNotifyEst:1
}
]
}
In the above example i have multiple list of same type of collection
and this list is i aggregate with Estimation Collection
These Above Data if fetched based on Estimation Collection
so based on this Estimation Collection's _id i need to fetch notification and based on commentId that fetched inside notification collection i need to fetch comments collection data.
i already do aggregation with some more collection i only need the notification and comment data inside notificationData array
Yes i Achieve this i use following query for that
let estData = await Estimations.aggregate([
{
$lookup: {
from: "notifications", as: "notificationsData",
let: {
estimation_id: "$_id"
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$estimationId', "$$estimation_id"] },
{ $eq: ['$userId', ObjectId(this.req.currentUser._id)] },
{ $eq: ['$isViewed', false] },
]
}
},
},
{
$lookup:
{
from: "comments",
localField: "commentId",
foreignField: "_id",
as: "commentData"
}
},
{ "$unwind": "$commentData" },
],
}
},
{
$project: {
projectName: 1,
"notificationsData.content": 1,
"notificationsData.estimationId": 1,
"notificationsData._id": 1,
"notificationsData.commentId": 1,
"notificationsData.commentData.content": 1,
"notificationsData.commentData._id": 1,
leadId: 1,
projectOverview: 1
}
},
])
in the above query i get notificationData based on estimationId and commentData data based on commentId that is specified inside notificationData
so here it is solution that i do and works fine.

mongodb lookup with foreign field match

I want to know how can i lookup with match.
I could lookup two collection by this query.
db.category.aggregate([
{
$lookup:
{
from: "faq",
localField: "_id",
foreignField: "category_code",
as: "faq"
}
}
])
Then i get this result.
{
"_id":ObjectId("1234"),
"category_name":"about account",
"faq": [
{
"_id":ObjectId("faq id blah blah"),
"category_code" : ObjectId("1234"),
"faq_title":"When you can't create account",
"del_flg":"N"
},
{
"_id":ObjectId("faq id blah blah2222"),
"category_code" : ObjectId("1234"),
"faq_title":"When you change your account",
"del_flg":"N"
},
{
"_id":ObjectId("faq id blah blah3333"),
"category_code" : ObjectId("1234"),
"faq_title":"When you lost your account",
"del_flg":"Y"
}
[
}
I want just two faq item which "del_flg" is "N"
I am not familiar to use mongodb.
Is it possible if i use pipline? I tried some pipline examples, but i couldn't.
You should try $lookup with pipeline,
let will create a variable to access id inside lookup pipeline using $$
pipeline match using $expr because we are comparing both fields $$id = $category_code
set match condition del_flg: N
db.category.aggregate([
{
$lookup: {
from: "faq",
let: {
id: "$_id"
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$$id",
"$category_code"
]
},
"del_flg": "N"
}
}
],
as: "faq"
}
}
])
Playground: https://mongoplayground.net/p/gUVUMk6KIAV

MongoDB lookup with object relation instead of array

I have a collection matches like this. I'm using players object {key: ObjectId, key: ObjectID} instead of classic array [ObjectId, ObjectID] for reference players collection
{
"_id": ObjectId("5eb93f8efd259cd7fbf49d55"),
"date": "01/01/2020",
"players": {
"home": ObjectId("5eb93f8efd259cd7fbf49d59"),
"away": ObjectId("5eb93f8efd259cd7fbf49d60")
}
},
{...}
And players collection:
{
"_id": ObjectId("5eb93f8efd259cd7fbf49d59"),
"name": "Roger Federer"
"country": "Suiza"
},
{
"_id": ObjectId("5eb93f8efd259cd7fbf49d60"),
"name": "Rafa Nadal"
"country": "España"
},
{...}
What's the better way to do mongoDB lookup? something like this is correct?
const rows = await db.collection('matches').aggregate([
{
$lookup: {
from: "players",
localField: "players.home",
foreignField: "_id",
as: "players.home"
}
},
{
$lookup: {
from: "players",
localField: "players.away",
foreignField: "_id",
as: "players.away"
},
{ $unwind: "$players.home" },
{ $unwind: "$players.away" },
}]).toArray()
I want output like this:
{
_id: 5eb93f8efd259cd7fbf49d55,
date: "12/05/20",
players: {
home: {
_id: 5eb93f8efd259cd7fbf49d59,
name: "Roger Federer",
country: "Suiza"
},
away: {
_id: 5eb93f8efd259cd7fbf49d60,
name: "Rafa Nadal",
country: "España"
}
}
}
{...}
You can try below aggregation query :
db.matches.aggregate([
{
$lookup: {
from: "players",
localField: "players.home",
foreignField: "_id",
as: "home"
}
},
{
$lookup: {
from: "players",
localField: "players.away",
foreignField: "_id",
as: "away"
}
},
/** Check output of lookup is not empty array `[]` & get first doc & write it to respective field, else write the same value as original */
{
$project: {
date: 1,
"players.home": { $cond: [ { $eq: [ "$home", [] ] }, "$players.home", { $arrayElemAt: [ "$home", 0 ] } ] },
"players.away": { $cond: [ { $eq: [ "$away", [] ] }, "$players.away", { $arrayElemAt: [ "$away", 0 ] } ] }
}
}
])
Test : mongoplayground
Changes or Issues with current Query :
1) As you're using two $unwind stages one after the other, If anyone of the field either home or away doesn't have a matching document in players collection then in the result you don't even get actual match document also, But why ? It's because if you do $unwind on [] (which is returned by lookup stage) then unwind will remove that parent document from result, To overcome this you need to use preservenullandemptyarrays option in unwind stage.
2) Ok, there is another way to do this without actually using $unwind. So do not use as: "players.home" or as: "players.away" cause you're actually writing back to original field, Just in case if you don't find a matching document an empty array [] will be written to actual fields either to "home" or "away" wherever there is not match (In this case you would loose actual ObjectId() value existing in that particular field in matches doc). So write output of lookup to a new field.
Or even more efficient way, instead of two $lookup stages (Cause each lookup has to go through docs of players collection again & again), you can try one lookup with multiple-join-conditions-with-lookup :
db.matches.aggregate([
{
$lookup: {
from: "players",
let: { home: "$players.home", away: "$players.away" },
pipeline: [
{
$match: { $expr: { $or: [ { $eq: [ "$_id", "$$home" ] }, { $eq: [ "$_id", "$$away" ] } ] } }
}
],
as: "data"
}
}
])
Test : mongoplayground
Note : Here all the matching docs from players which match with irrespective of away or home field will be pushed to data array. So to keep DB operation simple you can get that array from DB along with actual matches document & Offload some work to code which is to map respective objects from data array to players.home & players.away fields.

MongoDB $group performance

I have to collections: Account and MyCollection.
MyCollection has a One To Many relationship with account.
This relation is set by storing the id of the linked Account into MyCollection.
I need to filter MyCollection by an attribute of Account, and I need to extract only one MyCollection object per Account.
Here is the request I am making and an overview of my data structure:
// The document structure
Account: {
types: ["type_A"], // An indexed array
}
MyCollection: {
accountId: "accountId", // Id of the linked account, indexed
date: new ISODate(), // an indexed date
// Other data that we need
}
// The actual request
db.MyCollection.aggregate([
{
$match: {someData: "foo"},
},
{
$sort: {date: -1},
},
// Inject foreign account
{
$lookup: {
from: "Account",
localField: "accountId",
foreignField: "_id",
as: "account",
},
},
{
$unwind: "$account",
},
// match account type
{
$match: {
"account.type": {$in: ["type_A", "type_B"]},
},
},
// And extract latest document
{
$group: {
_id: "$accountId",
myCollection: {$first: "$$ROOT"},
},
},
]);
This request gives me the correct results, but takes several minutes to process...
How can I speed up all this ? I have an index on Account.type, but it is not used here.
My tests showed that it is the $group operation that is the most time consuming (the request takes half a second without it).