I have a query using $lookup that "join" two models and $project to select all fields that i need only, and in that $project I need to $sum a value called totalValue but only return zero:
My query
User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday",
lastName: "$lastname"
},
totalServices: {
$sum: "$services"
},
}
},
{
$lookup: {
from: "schedules",
"let": { "id": "$_id.phone" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$customer.phone", "$$id"] }}},
{ "$project": { "scheduleStart": 1, "scheduleEnd": 1 }}
],
"as": "user_detail"
}
},
{
$project: {
_id: 1,
name: 1,
name: 1,
cpf: 1,
phone: 1,
email: 1,
birthday: 1,
totalServices: 1,
totalValue: { $sum : "$user_detail.value" }, // here only return zero
count: {
$sum: 1
},
user_detail: 1
}
},
You need to $project your value field in the user_details projection to get it in the next aggregation stage
{ "$project": { "scheduleStart": 1, "scheduleEnd": 1, "value": 1 }}
Related
I have two collections users and profiles. I am implementing a search with the following query:
User.aggregate(
[
{
$match: {
_id: { $ne: req.user.id },
isDogSitter: { $eq: true },
profileId: { $exists: true }
}},
{
$project: {
firstName: 1,
lastName: 1,
email: 1,
isDogSitter: 1,
profileId: 1,
}},
{
$lookup: {
from: "profiles",
pipeline: [
{
$project: {
__v: 0,
availableDays: 0,
}},
{
$match: {
city: search
}}
],
as: "profileId",
}}
],
(error, result) => {
console.log("RESULT ", result);
}
);
What this does is that its searches for the city in the profiles collection and when there is not search match then profileId becomes an empty array. What I really want is that if the profileId is an empty array then I don't want to return the other fields in the documents too. It should empty the array. Below is my current returned result.
RESULT [
{
_id: 60cabe38e26d8b3e50a9db21,
isDogSitter: true,
firstName: 'Test',
lastName: 'Sitter',
email: 'test#user.com',
profileId: []
}
]
Add $match pipeline stage after the $lookup pipeline stage and
add the empty array condition check over there.
User.aggregate(
[
{
$match: {
_id: { $ne: req.user.id },
isDogSitter: { $eq: true },
profileId: { $exists: true }
}},
{
$project: {
firstName: 1,
lastName: 1,
email: 1,
isDogSitter: 1,
profileId: 1,
}},
{
$lookup: {
from: "profiles",
pipeline: [
{
$project: {
__v: 0,
availableDays: 0,
}},
{
$match: {
city: search
}}
],
as: "profileId",
}}
{
$match: { // <-- Newly added $match condition
"profileId": {"$ne": []}
},
},
],
(error, result) => {
console.log("RESULT ", result);
}
);
db.setting.aggregate([
{
$match: {
status: true,
deleted_at: 0,
_id: {
$in: [
ObjectId("5c4ee7eea4affa32face874b"),
ObjectId("5ebf891245aa27c290672325")
]
}
}
},
{
$lookup: {
from: "site",
localField: "_id",
foreignField: "admin_id",
as: "data"
}
},
{
$project: {
name: 1,
status: 1,
price: 1,
currency: 1,
numberOfRecord: {
$size: "$data"
}
}
},
{
$sort: {
numberOfRecord: 1
}
}
])
how to push the currency into price object using project please guide thanks a lot, also eager to know what is difference between $addtoSet and $push, what is good option to opt it from project or fix it from $addField
https://mongoplayground.net/p/RiWnnRtksb4
Output should be like this:
[
{
"_id": ObjectId("5ebf891245aa27c290672325"),
"currency": "USD",
"name": "Menz",
"numberOfRecord": 0,
"price": {
"numberDecimal": "20",
"currency": "USD",
},
"status": true
},
{
"_id": ObjectId("5c4ee7eea4affa32face874b"),
"currency": "USD",
"name": "Dave",
"numberOfRecord": 2,
"price": {
"numberDecimal": "10",
"currency": "USD"
},
"status": true
}
]
You can insert a field into an object with project directly, like this (field price):
$project: {
name: 1,
status: 1,
price: {
numberDecimal: "$price.numberDecimal",
currency: "$currency"
},
numberOfRecord: {
$size: "$data"
}
}
By doing it with project, there is no need to use $addField.
For the difference between $addToSet and $push, read this great answer.
You can just set the object structure while projecting, so in this case there's no need for either $push or $addToSet.
{
$project: {
name: "1",
status: 1,
price: {
currency: "$currency",
numberDecimal: "$price.numberDecimal"
},
currency: 1,
numberOfRecord: {
$size: "$data",
}
}
}
Now the difference between $push and $addToSet is pretty trivial and derived from the name, $push saves all items while $addToSet will just create a set of them, for example:
input:
[
//doc1
{
item: 1
},
//doc2
{
item: 2
},
//doc3
{
item: 1
}
]
Now this:
{
$group: {
_id: null,
items: {$push: "$item"}
}
}
Will result in:
{_id: null, items: [1, 2, 1]}
While:
{
$group: {
_id: null,
items: {$addToSet: "$item"}
}
}
Will result in:
{_id: null, items: [1, 2]}
I have the following stage in my MongoDB aggregation pipeline that returns the qty and sum of sales, which works fine:
{
$lookup: {
from: 'sales',
let: { part: '$_id' },
pipeline: [
{ $match: { $and: [{ $expr: { $eq: ['$partner', '$$part'] } }] } },
{ $group: { _id: null, qty: { $sum: 1 }, soldFor: { $sum: '$soldFor' } } },
{ $project: { _id: 0, qty: 1, soldFor: 1 } }],
as: 'sales'}},
{ $unwind: { path: '$sales', preserveNullAndEmptyArrays: true } },
{ $project: { _id: 1, sales: 1 }
}
However, if there are no sales, then the $project projection returns an empty sales object, but what I'd really like is it to return a completed object, but with 0 - like this:
{
sales: {
qty: 0,
soldFor: 0
}
}
You can use $cond operator here
{
"$project": {
"_id": 1,
"sales": {
"$cond": [
{ "$eq": [{ "$size": "$sales" }, 0] },
{
"sales": {
"qty": 0,
"soldFor": 0
}
},
"$sales"
]
}
}
}
I have a query, that use $lookup to "join" two models, after this i use $project to select olny the fields that i need, but my $project brings an arrray of objects (user_detail) that contains more data that i need. I want only two fields (scheduleStart and scheduleEnd) of my result.
My query:
User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday",
lastName: "$lastname"
},
totalServices: {
$sum: "$services"
},
}
},
{
$lookup: {
from: "schedules",
localField: "_id.phone",
foreignField: "customer.phone",
as: "user_detail"
}
},
{
$project: {
_id: 1,
name: 1,
name: 1,
cpf: 1,
phone: 1,
email: 1,
birthday: 1,
totalServices: 1,
totalValue: { $sum : "$user_detail.value" },
count: {
$sum: 1
},
user_detail: 1
}
},
Result of query:
count: 1
totalServices: 0
totalValue: 73
user_detail: Array(2)
0:
...
paymentMethod: 0
paymentValue: "0"
scheduleDate: "2018-10-02"
scheduleEnd: "2018-10-02 08:40"
scheduleStart: "2018-10-02 08:20"
status: 3
store: "5b16cceb56a44e2f6cd0324b"
updated: "2018-11-27T13:30:21.116Z"
1:
...
paymentMethod: 0
paymentValue: "0"
scheduleDate: "2018-11-27"
scheduleEnd: "2018-11-27 00:13"
scheduleStart: "2018-11-27 00:03"
status: 2
store: "5b16cceb56a44e2f6cd0324b"
updated: "2018-11-27T19:33:39.498Z"
_id:
birthday: "1992-03-06"
email: "csantosgrossi#gmail.com"
id: "5bfed8bd70de7a383855f09e"
name: "Chris Santos G"
phone: "11969109995"
...
Result that i need:
count: 1
totalServices: 0
totalValue: 73
user_detail: Array(2)
0:
scheduleEnd: "2018-10-02 08:40"
scheduleStart: "2018-10-02 08:20"
1:
scheduleEnd: "2018-11-27 00:13"
scheduleStart: "2018-11-27 00:03"
_id:
birthday: "1992-03-06"
email: "csantosgrossi#gmail.com"
id: "5bfed8bd70de7a383855f09e"
name: "Chris Santos G"
phone: "11969109995"
...
How can i do this with my query?
You can use $lookup 3.6 syntax to $project the fields inside the $lookup pipeline
User.aggregate([
{ "$lookup": {
"from": "schedules",
"let": { "id": "$_id.phone" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$customer.phone", "$$id"] }}},
{ "$project": { "scheduleStart": 1, "scheduleEnd": 1 }}
],
"as": "user_detail"
}}
])
For version of mongo version > 3.6 this query should work for you:
User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday",
lastName: "$lastname"
},
totalServices: {
$sum: "$services"
},
}
},
{
$lookup: {
from: "schedules",
localField: "_id.phone",
foreignField: "customer.phone",
as: "user_detail"
}
},
{
$project: {
_id: 1,
name: 1,
name: 1,
cpf: 1,
phone: 1,
email: 1,
birthday: 1,
totalServices: 1,
totalValue: { $sum : "$user_detail.value" },
count: {
$sum: 1
},
user_detail: {
scheduleEnd: 1,
scheduleStart: 1,
}
}
},
Here's the structure part of my collection:
_id: ObjectId("W"),
names: [
{
number: 1,
list: ["A","B","C"]
},
{
number: 2,
list: ["B"]
},
{
number: 3,
list: ["A","C"]
}
...
],
...
I use this request:
db.publication.aggregate( [ { $match: { _id: ObjectId("54a1de90453d224e80f5fc60") } }, { $group: { _id: "$_id", SizeName: { $first: { $size: { $ifNull: [ "$names", [] ] } } }, names: { $first: "$names" } } } ] );
but I would now use $size in every documents of my names array.
Is it possible to get this result (where "sizeList" is the result of $size) :
_id: ObjectId("W"),
SizeName: 3,
names: [
{
SizeList: 3,
number: 1,
list: ["A","B","C"]
},
{
SizeList: 1,
number: 2,
list: ["B"]
},
{
SizeList: 2,
number: 3,
list: ["A","C"]
}
...
],
...
All you really want here is a $project stage and use $map to alter the contents of the array members:
db.names.aggregate([
{ "$project": {
"SizeName": { "$size": "$names" },
"names": { "$map": {
"input": "$names",
"as": "el",
"in": {
"SizeList": { "$size": "$$el.list" },
"number": "$$el.number",
"list": "$$el.list"
}
}}
}}
])
You can alternately process with $unwind and group it all back again, but that's kind of long winded when you have MongoDB 2.6 anyway.
This isn't necessarily the most efficient way, but I think it does what you're aiming to do:
db.publication.aggregate( [
{ $unwind: '$names' },
{ $unwind: '$names.list' },
{ $group: {
_id: { _id: "$_id", number: "$names.number" },
SizeList: { $sum: 1 },
list: { $push: "$names.list" }
}
},
{ $group: {
_id: "$_id._id",
names: {
$push: {
number: "$_id.number",
list: "$list",
SizeList: "$SizeList"
}
},
SizeName: {$sum: 1}
}
}
]);