I'm new to MongoDB and need to write this query. My JSON Request is like this. I need to build the query in mongodb with aggregation
JSON
[
{
"_id": {
"user_id": "1",
"name": "Madhu",
"LeadName": "Test",
"status": "Lost",
"lead_value": 78000
}
},
{
"_id": {
"user_id": "1",
"name": "Madhu",
"LeadName": "Trail",
"status": "Trial",
"lead_value": 75200
}
},
{
"_id": {
"user_id": "1",
"name": "Madhu",
"LeadName": "Teja",
"status": "Customer",
"lead_value": 45000
}
},
{
"_id": {
"user_id": "1",
"name": "Madhu",
"LeadName": "Kushbu",
"status": "Trial",
"lead_value": 20000
}
},
{
"_id": {
"user_id": "1",
"name": "Madhu",
"LeadName": "Krishna",
"status": "Trial",
"lead_value": 18000
}
},
{
"_id": {
"user_id": "1",
"name": "Madhu",
"LeadName": "Test1",
"status": "Trial",
"lead_value": 12000
}
},
{
"_id": {
"user_id": "89",
"name": "Prashanth Reddy",
"LeadName": "Ganesh",
"status": "Trial",
"lead_value": 12000
}
},
{
"_id": {
"user_id": "89",
"name": "Prashanth Reddy",
"LeadName": "Hima Sree",
"status": "Customer",
"lead_value": 1750
}
}
]
I want the response like sum of the all the "Lead_values" except "Customer" and count the Status also
[
{
"_id": {
"user_id": "1",
"name": "Madhu",
"lead_value": 120354,
"count" : 5
}
},
{
"_id": {
"user_id": "89",
"name": "Prashanth Reddy",
"lead_value": 12000,
"count" : 1
}
}
]
How to write in MongoDB Aggregation?
You can use this query, to get your perfect result.
Here is it.
db.getCollection('YOUR_COLLECTION').aggregate([
{
$match: {
"_id.status": {$ne: "Customer"}
}
},
{
$group: {
_id: "$_id.user_id",
lead_values: {$sum: "$_id.lead_value"},
name: {$first: "$_id.name"},
count: {$sum: 1}
}
}
])
Here you can see, how we can use $sum with $group
You just need a simple $group stage:
db.collection.aggregate([
{
$match: {
"_id.status": {$ne: "Customer"}
}
},
{
$group: {
_id: "$_id.user_id",
lead_values: {$sum: "$_id.lead_value"},
name: {$first: "$_id.name"},
count: {$sum: 1}
}
}
])
You can now reformat the data as you wish.
Related
hope you're fine.
I cannot seem to find a way to aggregate the following document by 'equity id'.
{
"_id": {
"$oid": "6001dc246192c700013e8252"
},
"user": "blablabla",
"_type": "User::Individual",
"created_at": {
"$date": "2021-01-15T18:17:11.130Z"
},
"integrations": [{
"_id": {
"$oid": "6001dc62e7a0970001258da8"
},
"status": "completed",
"authentication_failed_msg": null
}],
"portfolios": [{
"_id": {
"$oid": "6001dc62e7a0970001258da9"
},
"_type": "SimplePortfolio",
"transactions": [{
"_id": {
"$oid": "6001dc62e7a0970001258daa"
},
"settlement_period": 2,
"expenses": 0,
"source": "integration",
"_type": "Transaction::Equity::Buy",
"date": {
"$date": "2020-03-02T00:00:00.000Z"
},
"shares": 100,
"price": 13.04,
"equity_id": "abcd"
}, {
"_id": {
"$oid": "6001dc62e7a0970001258dab"
},
"settlement_period": 2,
"expenses": 0,
"source": "integration",
"_type": "Transaction::Equity::Buy",
"date": {
"$date": "2020-03-02T00:00:00.000Z"
},
"shares": 1000,
"price": 1.03,
"equity_id": "efgh"
I tried something like
db.collection.aggregate([{"$unwind": {'$portfolios.transactions'}},
{"$group" : {"_id": "$equity_id"}}])
Got error InvalidDocument: cannot encode object: {'$portfolios.transactions'}, of type: <class 'set'>
Ideally what I want a list grouped by user and equity_id and a sum of its shares. Does anyone know if the error is caused by my aggregation or the document structure?
You should $unwind twice.
db.collection.aggregate([
{
"$unwind": "$portfolios"
},
{
"$unwind": "$portfolios.transactions"
},
{
"$group": {
"_id": "$portfolios.transactions.equity_id"
}
}
])
mongoplayground
I have a document exchange_history like:
[
{
"_id": {"$oid": "611fd7059ec255088af94e4d"},
"amount": {"$numberDecimal": 1.2},
"exchange_time": {"$date": "2021-08-20T16:02:00.448Z"},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {"$oid": "611fed2a31ef85394c4f9b10"},
"amount": {"$numberDecimal": 1.02},
"exchange_time": {"$date": "2021-08-20T16:02:00.448Z"},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {"$oid": "6120b47d4cf35f7f70312a71"},
"amount": {"$numberDecimal": -1},
"exchange_time": {"$date": "2021-08-21T08:08:16.397Z"},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {"$oid": "6120b4834cf35f7f70312a72"},
"amount": {"$numberDecimal": -2},
"exchange_time": {"$date": "2021-08-21T08:08:16.397Z"},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {"$oid": "6120b4e94cf35f7f70312a73"},
"amount": {"$numberDecimal": 10},
"exchange_time": {"$date": "2021-08-21T08:08:16.397Z"},
"user_id": "611df4d9ece358586d212d91"
}
]
How to calculate the balance (sum amount) and income (sum amount where amount is greater than 0) by user_id at the same time using MongoDB?
My MongoDB version is 4.4. Pls help me!
Try this ...
Document :
[
{
"_id": {
"oid": "611fd7059ec255088af94e4d"
},
"amount": {
"numberDecimal": 1.2
},
"exchange_time": {
"date": "2021-08-20T16:02:00.448Z"
},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {
"oid": "611fed2a31ef85394c4f9b10"
},
"amount": {
"numberDecimal": 1.02
},
"exchange_time": {
"date": "2021-08-20T16:02:00.448Z"
},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {
"oid": "6120b47d4cf35f7f70312a71"
},
"amount": {
"numberDecimal": -1
},
"exchange_time": {
"date": "2021-08-21T08:08:16.397Z"
},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {
"oid": "6120b4834cf35f7f70312a72"
},
"amount": {
"numberDecimal": -2
},
"exchange_time": {
"date": "2021-08-21T08:08:16.397Z"
},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {
"oid": "6120b4e94cf35f7f70312a73"
},
"amount": {
"numberDecimal": 10
},
"exchange_time": {
"date": "2021-08-21T08:08:16.397Z"
},
"user_id": "611df4d9ece358586d212d91"
}
]
Code :
db.collection.aggregate([
{
$group: {
_id: "$user_id",
balance: {
$sum: "$amount.numberDecimal"
},
income: {
$sum: {
$cond: [
{
$gt: [
"$amount.numberDecimal",
0
]
},
"$amount.numberDecimal",
0
]
}
}
}
},
])
And Output:
[
{
"_id": "611df4d9ece358586d212d91",
"balance": 10,
"income": 10
},
{
"_id": "60ef435299b1760752cbafe1",
"balance": -0.78,
"income": 2.2199999999999998
}
]
Try mongoplayground
I have the collection data from a csv file with header. When i run my query
db.ties.aggregate(
[
{
$group:
{
_id: { "SHOP": "$SHOP" },
isLinkedTo: { $push: { "PERSON": "$PERSON", "CITY": "$CITY", "ROOM": "$ROOM", "STYLE": "$STYLE", "hasDonated": {"DATE": "$DATE", "OBJECT": "$OBJECT", "COST": "$COST", "COLOR": "$COLOR", "PAYMENT": "$PAYMENT"}}}
}
},
{ $out: "ties"}
],
{ allowDiskUse: true }
)
I have like result:
{
"_id": {
"Shop": "FirstShopNameCovered"
},
"isLinkedTo": [{
"PERSON": "Carleen",
"CITY": "Rome",
"ROOM": "Kitchen",
"STYLEPREFERED": "Modern",
"hasDonated": {
"DATE": "2019-10-11",
"OBJECT": "Set of dishes",
"COST": 72,
"COLOR": "White",
"PAYMENT": "Credit card"
}
}, {
"PERSON": "Carleen",
"CITY": "Rome",
"ROOM": "Kitcher",
"STYLEPREFERED": "Modern",
"hasDonated": {
"DATE": "2018-10-26",
"OBJECT": "Set of chairs",
"COST": 353,
"COLOR": "Grey",
"PAYMENT": "Coupon"
}
}, {
"PERSON": "Pernick",
"CITY": "Venezia",
"ROOM": "Bathroom",
"STYLE": "Minimalist",
"hasDonated": {
"DATE": "2018-09-18",
"OBJECT": "Mirror",
"COST": 68,
"COLOR": "Brown",
"PAYMENT": "Credit card"
}
}
You can see that there is replicated the Person "PERSON": "Carleen" with all data with 2 different arrays hasDonated.
I wish have something like this result, with person not replicated that contains all hasDonated arrays where he is present:
"_id": {
"Shop": "NameCovered"
},
"isLinkedTo": [{
"PERSON": "Carleen",
"CITY": "Rome",
"ROOM": "Kitchen",
"STYLE": "RetrĂ²",
"hasDonated": {
"DATE": "2019-10-11",
"OBJECT": "Set of dishes",
"COST": 72,
"COLOR": "White",
"PAYMENT": "Credit card"
},
{
"DATE": "2018-10-26",
"OBJECT": "Chair",
"COST": 53,
"COLOR": "Grey",
"PAYMENT": "Coupon"
}
}, {
"PERSON": "Pernick",
"CITY": "Venezia",
"ROOM": "Bathroom",
"STYLE": "Minimalist",
"hasDonated": {
"DATE": "2018-09-18",
"OBJECT": "Mirror",
"COST": 68,
"COLOR": "Brown",
"PAYMENT": "Credit card"
}
How can I do to have the result like this?
First we need to $unwind to flat the array. Then group the hasDonated using $group where unique is found by combination of "_id" and "PERSON" as you mentioned.
[
{
"$unwind": "$isLinkedTo"
},
{
$group: {
_id: {
_id: "$_id",
per: "$isLinkedTo.PERSON"
},
isLinkedTo: {
$first: {
PERSON: "$isLinkedTo.PERSON",
CITY: "$isLinkedTo.CITY",
ROOM: "$isLinkedTo.ROOM",
STYLEPREFERED: "$isLinkedTo.STYLEPREFERED"
}
},
hasDonated: {
$addToSet: "$isLinkedTo.hasDonated"
}
}
},
{
$addFields: {
_id: "$_id._id",
"isLinkedTo.hasDonated": "$hasDonated"
}
},
{
$project: {
hasDonated: 0
}
},
{
$group: {
_id: "$_id",
isLinkedTo: {
$push: "$isLinkedTo"
}
}
}
]
Working Mongo playground
I have Json which have values like state_city details this contains information like which city belongs to which state -
Need to query it for particular state name which will gives me all cities that belongs to that state.
db.collection.find({
"count": 10,
"state.name": "MP"
})
[
{
"collection": "collection1",
"count": 10,
"state": [
{
"name": "MH",
"city": "Mumbai"
},
{
"name": "MH",
"city": "Pune"
},
{
"name": "UP",
"city": "Kanpur"
},
{
"name": "CG",
"city": "Raipur"
}
]
},
{
"collection": "collection2",
"count": 20,
"state": [
{
"name": "MP",
"city": "Indore"
},
{
"name": "MH",
"city": "Bhopal"
},
{
"name": "UP",
"city": "Kanpur"
},
{
"name": "CG",
"city": "Raipur"
}
]
}
]
You have to use aggregate query to get only matching elements in array :
db.collection.aggregate([{
$unwind: "$content.state"
},
{
$match: {
"content.state.name": "MH",
"count": 10
}
},
{
$group: {
_id: "$content.state.city",
}
},
{
$addFields: {
key: 1
}
},
{
$group: {
_id: "$key",
cities: {
$push: "$_id"
}
}
},
{
$project: {
_id: 0,
cities: 1
}
}
])
This query will return :
{
"cities": [
"Pune",
"Mumbai"
]
}
The following query would be the solution.
db.collection.find({ "count": 10, "state":{"name": "MP"}})
For more complex queries, $elemMatch is also available.
I have multiple documents, and I'm trying to aggregate all documents with companyId = xxx and return one array with all the statuses.
So it will look like this:
[
{
"status": "created",
"date": "2019-03-16T10:59:59.200Z"
},
{
"status": "completed",
"date": "2019-03-16T11:00:37.750Z"
},
{
"status": "created",
"date": "2019-03-16T10:59:59.200Z"
},
{
"status": "completed",
"date": "2019-03-16T11:00:37.750Z"
},
{
"status": "created",
"date": "2019-03-16T10:59:59.200Z"
},
{
"status": "completed",
"date": "2019-03-16T11:00:37.750Z"
},
{
"status": "created",
"date": "2019-03-16T10:59:59.200Z"
},
{
"status": "completed",
"date": "2019-03-16T11:00:37.750Z"
}
]
The document look like this:
[
{
"companyId": "xxx",
"position": "",
"section": "",
"comment": "",
"items": [
{
"any": "111",
"name": "some name",
"description": "some description",
"version": "3",
"status": [
{
"status": "created",
"date": "2019-03-16T10:59:59.200Z"
},
{
"status": "completed",
"date": "2019-03-16T11:00:37.750Z"
}
]
},
{
"any": "222",
"name": "some name",
"description": "some description",
"version": "3",
"status": [
{
"status": "created",
"date": "2019-03-16T10:59:59.200Z"
},
{
"status": "completed",
"date": "2019-03-16T11:00:37.750Z"
}
]
}
]
},
{
"companyId": "xxx",
"position": "",
"section": "",
"comment": "",
"items": [
{
"any": "111",
"name": "some name",
"description": "some description",
"version": "3",
"status": [
{
"status": "created",
"date": "2019-03-16T10:59:59.200Z"
},
{
"status": "completed",
"date": "2019-03-16T11:00:37.750Z"
}
]
},
{
"any": "222",
"name": "some name",
"description": "some description",
"version": "3",
"status": [
{
"status": "created",
"date": "2019-03-16T10:59:59.200Z"
},
{
"status": "completed",
"date": "2019-03-16T11:00:37.750Z"
}
]
}
]
}
]
Any suggestion, how to implement this?
Then I want to loop over the array (in code) and count how many items in status created, and completed. maybe it could be done with the query?
Thanks in advance
You can use below aggregation:
db.col.aggregate([
{
$match: { companyId: "xxx" }
},
{
$unwind: "$items"
},
{
$unwind: "$items.status"
},
{
$replaceRoot: {
newRoot: "$items.status"
}
},
{
$group: {
_id: "$status",
count: { $sum: 1 }
}
}
])
Double $unwind will return single status per document and then you can use $replaceRoot to promote each status to root level of your document.
Additionally you can add $group stage to count documents by status.
In addition to the #mickl answer, you can add $project pipeline to get the result as a flat list of status and count.
db.collectionName.aggregate([
{
$match: { companyId: "xxx" }
},
{
$unwind: "$items"
},
{
$unwind: "$items.status"
},
{
$replaceRoot: {
newRoot: "$items.status"
}
},
{
$group: {
_id: "$status",
count: { $sum: 1 }
}
},
{
$project: {
"status":"$_id",
"count":1,
_id:0
}
}
])
If the number of documents on which you are executing the above query is too much then you should avoid using $unwind in the initial stage of aggregation pipeline.
Either you should use $project after $match to reduce the selection of fields or you can use below query:
db.col.aggregate([
{
$match: {
companyId: "xxx"
}
},
{
$project: {
_id: 0,
data: {
$reduce: {
input: "$items.status",
initialValue: [
],
in: {
$concatArrays: [
"$$this",
"$$value"
]
}
}
}
}
},
{
$unwind: "$data"
},
{
$replaceRoot: {
newRoot: "$data"
}
}
])