my problem is how I get a simple object of ownerDetails, correct me if my code is wrong
data I got, from my code
{
"status": true,
"data": [
{
"_id": "61a2a9680b122bc154cbd6af",
"ownerName": "Ajay",
"mobile": 878787878,
"designation": "IT",
"gender": "Male",
"age": 26,
"carDetails": [
{
"carName": "BUY",
"color": "blue",
"sheets": 8,
"avgSpeed": 105,
"price": 850000,
"model": "C110"
},
{
"carName": "GTR",
"color": "blue",
"sheets": 4,
"avgSpeed": 105,
"price": 98000,
"model": "G120"
}
]
}
]
}
i want data like this,
{
"status": true,
"ownerDetails": {
"_id": "61a2a9680b122bc154cbd6af",
"ownerName": "Ajay",
"mobile": 878787878,
"designation": "IT",
"gender": "Male",
"age": 26,
"total_car": 2,
}
}
code for getting data from collections
exports.getOwnerDetails = async (req, res, next) => {
try {
Owner.aggregate([
{
$match: { ownerName: req.body.ownerName }
},
{
$lookup: {
from: "cars",
localField: "ownerName",
foreignField: "ownerName",
as: "carDetails",
},
},
{
$project: {
"carDetails._id": 0,
"carDetails.ownerName": 0,
},
},
]).then((data) => {
res.json({
status: true,
data: data,
});
});
} catch (error) {
res.status(500).json({
status: false,
msg: "Error : " + error,
});
}
};
in may returning data, I got an array of carDetails, but I only need that how many are owned by an Owner, and return a simple object of ownerDetails
$lookup return an array because more than one values can match the join. If you want to get only one value you can get the first element from the array. Then you can add these aggregation stages:
First $addFields to get the first element from the array (index 0).
Then use $project to get the desired output.
{
"$addFields": {
"data": {
"$arrayElemAt": ["$data",0]
}
}
},
{
"$project": {
"status": 1,
"ownerDetails": {
"_id": "$data._id",
"ownerName": "$data.ownerName",
"mobile": "$data.mobile",
"designation": "$data.designation",
"gender": "$data.gender",
"age": "$data.age",
"total_car": {
"$size": "$data.carDetails"
}
}
}
}
Example here
Query
i am not sure if this is what you want
unwind all owners from data
and from each owner, count its cars number
Test code here
aggregate(
[{"$unwind": {"path": "$data"}},
{"$set":
{"data.total_car": {"$size": "$data.carDetails"},
"data.carDetails": "$$REMOVE",
"_id": "$$REMOVE"}},
{"$set": {"ownerDetails": "$data", "data": "$$REMOVE"}}])
Related
I want to pull multiple objects from array.
Here is my sample collection:
Users
{
"_id": "wef324DGSshf",
"userTypes": [
{
"type": "students",
"users": [
{
"name": "John",
"age": 20
},
{
"name": "Mike",
"age": 20
},
{
"name": "Henry",
"age": 30
},
{
"name": "Henry",
"age": 40
}
]
}
]
}
I need to pull those objects where:
type: "students" and ages: [20,40]
So I have these 2 inputs: type & ages
Expected Response:
{
"_id": "wef324DGSshf",
"userTypes": [
{
"type": "students",
"users": [
{
"name": "Henry",
"age": 30
}
]
}
]
}
I have tried this query so far but it is not working:
Users.update({
"userTypes.type": "students",
"userTypes.users.age": {$in: [20, 40]},
},
{
$pull: {
"userTypes": {
"userTypes.users.$.age": {$in: [20, 40]}
}
}
});
Can anyone help me what I am doing wrong here?
Use an arrayFilters to specify the filtering for "type": "students" and normally perform $pull on age
db.collection.update({},
{
"$pull": {
"userTypes.$[ut].users": {
"age": {
$in: [
20,
40
]
}
}
}
},
{
arrayFilters: [
{
"ut.type": "students"
}
],
multi: true
})
Mongo Playground
Explanation: Check out the official doc about arrayFilters. You can think of the entries in arrayFilters as predicates. For a variable ut, it needs to have type: students. Let's go back to the $pull part. The predicate is applied to userTypes. That means for an entry in userTypes, ut, it needs to fit in the predicate of type: students. At the same time, we are $pulling the entries that age is in [20, 40].
I've got a MongoDB / Nodes aggregation that looks a little like this (there are other values in there, but this is the basic idea).
[
{
'$unwind': {
'path': '$Vehicles'
}
},
{
'$match': {
'Vehicles.Manufacturer': 'FORD'
}
},
{
'$facet': {
'makes': [
{
'$group': {
'_id': '$Vehicles.Manufacturer',
'count': {
'$sum': 1
}
}
}
]
}
},
{
'$project': {
'makes': {
'$sortArray': {
'input': '$makes',
'sortBy': 1
}
}
}
}
]
This works fine. But I would also like to pass an unmatched list through. IE an an array of vehicles whose Manufacturer = FORD and an other list of all Manufacturer.
Can't get it to work. Any ideas please?
Thanks in advance.
Edit:-
The current output looks like this:
[{
"makes": [
{
"_id": "FORD",
"count": 285
}
]
}]
and ideally it would look something like this:
[{
"makes": [
{
"_id": "FORD",
"count": 285
}
],
"unfiltered_makes": [
{
"_id": "ABARTH",
"count": 1
},
{
"_id": "AUDI",
"count": 7
},
{
"_id": "BMW",
"count": 2
},
{
"_id": "CITROEN",
"count": 4
},
{
"_id": "DS",
"count": 1
},
{
"_id": "FIAT",
"count": 1
}.... etc
]
}]
The data looks a bit like this:
"Vehicles": [
{
"Id": 1404908,
"Manufacturer": "MG",
"Model": "3",
"Price": 11995 .... etc
},{
"Id": 1404909,
"Manufacturer": "FORD",
"ManufacturerId": 34,
"Model": "Focus",
"Price": 12000 .... etc
} ... etc
]
In this case you can do something like:
db.collection.aggregate([
{$unwind: "$Vehicles"},
{$group: {
_id: "$Vehicles.Manufacturer",
count: {$sum: 1}}
},
{$facet: {
makes: [{$match: {_id: "FORD"}}],
unfiltered_makes: [{$group: {_id: 0, data: {$push: "$$ROOT"}}}]
}
},
{$project: {makes: 1, unfiltered_makes: "$unfiltered_makes.data"}}
])
See how it works on the playground example
Another option is:
db.collection.aggregate([
{$unwind: "$Vehicles"},
{$group: {
_id: "$Vehicles.Manufacturer",
count: {$sum: 1}}
},
{$group: {
_id: 0,
unfiltered_makes: {$push: "$$ROOT"},
makes: {$push: {$cond: [{$eq: ["$_id", "FORD"]}, "$$ROOT", "$$REMOVE"]}}
}
}
])
See how it works on the playground example
Here's another way to do it using "$function" to generate a histogram of "Manufacturer" and format the returned array. The javascript function only traverses the "Vehicles" array once, so this may be fairly efficient, although I did not do algorithm timing comparisons on a large collection.
N.B.: I'm a javascript noob and there may be a better way to do this.
db.collection.aggregate([
{
"$set": {
"unfiltered_makes": {
"$function": {
// generate histogram of manufacturers and format output
"body": "function(makes) {const m = new Object();makes.forEach((elem) => {m[elem.Manufacturer] = m[elem.Manufacturer] + 1 || 1});return Object.entries(m).map(([make, count]) => {return {'_id':make, 'count':count}})}",
"args": ["$Vehicles"],
"lang": "js"
}
}
}
},
{
"$project": {
"_id": 0,
"unfiltered_makes": 1,
"makes": {
"$filter": {
"input": "$unfiltered_makes",
"as": "make",
"cond": {
"$eq": [
"$$make._id",
// your search "Manufacturer" goes here
"FORD"
]
}
}
}
}
}
])
Try it on mongoplayground.net.
I have invoice Model as following
{
...
"itemDetails": [
{
"item": "593a1a01bbb00000043d9a4c",
"purchasingPrice": 100,
"sellingPrice": 150,
"qty": 200,
"_id": "59c39c2a5149560004173a05",
"discount": 0
}
],
"payments": [],
"status": "PENDING",
"created": {
"$date": "2017-09-21T11:02:02.675Z"
},
...
}
Sample Invoice Document is as follows.
{
"_id": {
"$oid": "59c39c2a5149560004173a04"
},
"customer": {
"$oid": "5935013832f9fc0004fa9a16"
},
"order": {
"$oid": "59c1df8393cbba0004a0e956"
},
"employee": {
"$oid": "592d0a6238880f0004637e84"
},
"status": "PENDING",
"deliveryStatus": "PROCESSING",
"created": {
"$date": "2017-09-21T11:02:02.675Z"
},
"discount": 0,
"payments": [],
"itemDetails": [
{
"item": {
"$oid": "593a1a01bbb00000043d9a4c"
},
"purchasingPrice": 100,
"sellingPrice": 150,
"qty": 200,
"_id": {
"$oid": "59c39c2a5149560004173a05"
},
"discount": 0
}
],
"__v": 0
}
Item details Item is an object Id which refers to Item collection.
I'm writing a mongoose Aggregate query to get the sale by Item. for that I need to filter the invoice from a given date range and status does not equal to "CANCELED". for that, I have written following code
module.exports.saleByItem = (req, res) => {
let fromDate;
let toDate;
if ((req.query.fromDate && moment(req.query.fromDate, config.dateFormat, true).isValid()) && (req.query.toDate && moment(req.query.toDate, config.dateFormat, true).isValid())) {
fromDate = moment(req.query.fromDate, config.dateFormat).startOf('day');
toDate = moment(req.query.toDate, config.dateFormat).endOf('day');
}
Invoice.aggregate([
{
"$match": {
"created": {
"$gte": fromDate
? fromDate.toDate()
: undefined,
"$lte": toDate
? toDate.toDate()
: undefined
},
"status": {
"$ne": "CANCELED"
}
}
}, {
"$unwind": "$itemDetails"
}, {
"$group": {
"_id": "$itemDetails.item",
"qty": {
"$sum": "$itemDetails.qty"
},
"value": {
"$sum": {
"$multiply": [
"$itemDetails.qty", {
"$subtract": ["$itemDetails.sellingPrice", "$itemDetails.discount"]
}
]
}
},
"avarageSellingPrice": {
"$avg": {
"$subtract": ["$itemDetails.sellingPrice", "$itemDetails.discount"]
}
}
}
}, {
"$sort": {
"value": -1
}
}, {
"$lookup": {
from: "items",
localField: "_id",
foreignField: "_id",
as: "item"
}
}, {
"$unwind": "$item"
}, {
"$project": {
_id: 1,
itemName: "$item.itemName",
qty: 1,
value: 1,
avarageSellingPrice: 1
}
}
]).then(salesFigures => {
res.status(200).json(salesFigures);
}).catch((err) => {
res.status(422).json(err);
});
};
The issue is when I put today date to both dates it returns sale of today. Gives []
How to handle date ranges in $match with local time-zone?
I'm trying to obtain the objects inside the reviews array by matching the fb_id's that I'm passing. I do not seem to get them to work. This is my actual document in the DB
{
"_id": {
"$oid": "5841d18187dcc74805887a6a"
},
"product_id": "583ae172231f5ec01db5727e",
"category": "Smartphones",
"reviews": [
{
"fb_id": "111",
"name": "name1",
"user_img": "imgurl1",
"title": "asdakj",
"description": "jnkjnkj",
"rating": 5,
"_id": {
"$oid": "5841d18187dcc74805887a6b"
}
},
{
"fb_id": "222",
"name": "name2",
"user_img": "imgurl2",
"title": "dadad",
"description": "asdasdad",
"rating": 3,
"_id": {
"$oid": "5841d1d0cbdf333411530ebd"
}
},
{
"fb_id": "333",
"name": "name3",
"user_img": "img url3",
"title": "asdad",
"description": "asdads",
"rating": 5,
"_id": {
"$oid": "5841d4c2174270f807084721"
}
},
{
"fb_id": "444",
"name": "name4",
"user_img": "imgurl4",
"title": "adasd",
"description": "kjnkj",
"rating": 1,
"_id": {
"$oid": "5841d569eae1b6600ea1ca92"
}
}
],
"__v": 0
}
I will be sending an array of fb_id's to mongoose and want the results only for the objects inside the array that has the fb_id's.
This is my query now:
Review.find({ product_id: '583ae172231f5ec01db5727e' }, { 'reviews.$.fb_id': { $in: <array of fb_id> } }, function(err, results) {
if (err) {
throw err;
};
console.log(results)
});
But it gives me this error :
MongoError: Unsupported projection option: reviews.$.fb_id: { $in: [ 111.0 ] }
EDIT: After a small query, now I am to get some results. But the result has only the first object that match from the fb_id's and not all.
Please tell me how I can achieve this.
You can use this for reference.
your code would be something like this:
Review.find({
product_id: '583ae172231f5ec01db5727e'
},
{
reviews: {
$elemMatch: {
fb_id: {
$in: <array of fb_id>
}
}
}
},
{
_id: 0,
'reviews.$': 1
}
, function(err, results) {
if (err) {
throw err;
};
console.log(results)
});
i need to update one field from a subdocument that appears inside other documents for all documents...
Something like this, i have some documents like this:
{
"_id": { "$oid" : "531cc2410b4ebf000036b2d7" },
"name": "ALMACEN 2",
"items": [
{
"_id": { "$oid" : "531ed4cae604d3d30df8e2ca" },
"brand": "BJFE",
"type": 0,
"color": "GDRNCCD",
"hand": 1,
"price": 500,
"model": 0,
"qty": 24
},
{
"_id": { "$oid" : "531ed4cae604d2330df8e2ca" },
"brand": "BJFE",
"type": 0,
"color": "GDRNCCD",
"hand": 1,
"price": 500,
"model": 0,
"qty": 12443
},
{
"_id": { "$oid" : "531ed22ae604d3d30df8e2ca" },
"brand": "BJFE",
"type": 0,
"color": "GDRNCCD",
"hand": 1,
"model": 0,
"price": 500,
"qty": 5
}
]
}
I want to set the qty to 0 for all documents that contains the item with _id 531ed22ae604d3d30df8e2ca
At moment i have it
warehouses.update({$.items._id: new ObjectID(item.brand._id)}, { $set: { qty: 0}}, {safe: false, multi: true}, function (e) {
if (e) {
error(e);
return;
}
success();
});
But i think the query is not ok..
You'll need to use the "$" positional operator to achieve this:
db.warehouses.update({"items._id": "531ed22ae604d3d30df8e2ca"}, { $set: { "items.$.qty":0} } )
In the mongo console it would look like this:
do {
db.warenhouse.update(
{
"_id" : "531cc2410b4ebf000036b2d7",
items: {
$elemMatch: {
qty: { $ne: 0 }
}
}
},
{
$set: {
"items.$.qty": 0
}
}
)
} while (db.getPrevError().n != 0);
Resource link