How we can use $toUpper with array fields? - mongodb

How we can use toUpper with array field, I have the following query which compare array field 'locations' with an array of camel case items, now my problem is how we can convert locations field values to upper case and then compare with array.
var array = ["KABUL","KAPISA","WARDAK","LOGAR","PARWAN","BAGHLAN","NANGARHAR","LAGHMAN",
"BAMYAN","PANJSHER","KHOST","GHAZNI","KUNARHA","PAKTYA","PAKTIKA","KUNDUZ",
"NOORISTAN","SAMANGAN","TAKHAR","DAYKUNDI","BADAKHSHAN","BALKH","GHOR",
"UROZGAN","FARYAB","ZABUL","SAR-E-PUL","NIMROZ","JAWZJAN","HELMAND","BADGHIS",
"KANDAHAR","FARAH","HERAT"];
db.getCollection('test').aggregate([
{ "$project": {
"locations": {
"$map": {
"input": {
"$setIntersection": ["$locations", array ]
},
"in": { "k": "$$this", "v": 1 }
}
}
}},
{ "$unwind": "$locations" },
{ "$group": {
"_id": "$locations.k",
"v": { "$sum": "$locations.v" }
}},
{ "$sort": { "_id": 1 } },
{ "$group": {
"_id": null,
"obj": { "$push": { "k": "$_id", "v": "$v" } }
}},
{ "$replaceRoot": {
"newRoot": { "$arrayToObject": "$obj" }
}}
])
locations field is like :
"locations" : [
"Afghanistan",
"Kabul",
.....
],

Using $map to transform "each" element of course:
{ "$project": {
"locations": {
"$map": {
"input": {
"$setIntersection": [
{ "$map": { "input": "$locations", "in": { "$toUpper": "$$this" } } },
array
]
},
"in": { "k": "$$this", "v": 1 }
}
}
}},

Related

(MongoDB) Combining Union and Intersection in the same pipeline

I have the following aggregation pipeline running in the latest version of mongoDB and pymongo:
[
{
"$project": {
"union": {
"$setUnion": [
"$query_a",
"$query_b"
]
}
}
},
{
"$unwind": "$union"
},
{
"$group": {
"_id": "$union.ID",
"date_a": {
"$addToSet": "$union.date_a"
},
"date_b": {
"$addToSet": "$union.date_b"
}
}
},
{
"$unwind": "$date_a"
},
{
"$unwind": "$date_b"
},
{
"$project": {
"_id": 1,
"date_a": "$date_a",
"date_b": "date_b",
"diff": {
"$subtract": [
{
"$toInt": "$date_b"
},
{
"$toInt": "$date_a"
}
]
}
}
},
{
"$match": {
"diff": {
"$gt": 0,
"$lte": 20
}
}
},
]
This gives the union of the 2 pipelines query_a and query_b. After this union I want to get an intersection on ID with the pipeline query_c: (query_a UNION query_b) INTERSECTION query_c.
For this playground example the desired output would be:
[
{
"ID": "c80ea2cb-3272-77ae-8f46-d95de600c5bf",
},
{
"ID": "cdbcc129-548a-9d51-895a-1538200664e6",
}
]
You could change and augment your pipeline a little to get your desired output.
db.collection.aggregate([
{
"$project": {
"union": {
// do the intersection here
"$filter": {
"input": {
"$setUnion": [
"$query_a",
"$query_b"
]
},
"as": "elem",
"cond": {
// only take IDs in query_c
"$in": ["$$elem.ID", "$query_c.ID"]
}
}
}
}
},
{
"$unwind": "$union"
},
{
"$group": {
"_id": "$union.ID",
"date_a": {
"$addToSet": "$union.date_a"
},
"date_b": {
"$addToSet": "$union.date_b"
}
}
},
{
"$unwind": "$date_a"
},
{
"$unwind": "$date_b"
},
{
"$project": {
"diff": {
"$subtract": [
{
"$toInt": "$date_b"
},
{
"$toInt": "$date_a"
}
]
}
}
},
{
"$match": {
"diff": {
"$gt": 0,
"$lte": 20
}
}
},
{ // get unique _id's
"$group": {
"_id": "$_id"
}
},
{ // rename _id to ID
"$project": {
"_id": 0,
"ID": "$_id"
}
}
])
Try it on mongoplayground.net.
You can do it with:
Updating first $project stage to also project an array of IDs from query_c.
Using $set as a second stage where you would filter out all items from the union of query_a and query_b, that does not have ID that's in query_c.
You can do it like this:
{
"$project": {
"union": {
"$setUnion": [
"$query_a",
"$query_b"
]
},
"query_c": {
"$map": {
"input": "$query_c",
"in": "$$this.ID"
}
}
}
},
{
"$set": {
"union": {
"$filter": {
"input": "$union",
"cond": {
"$in": [
"$$this.ID",
"$query_c"
]
}
}
}
}
},
The rest of your Aggregation pipeline can remain the same.
Working example

Find subdocument nested inside a document by id in mondogb

I have a mongodb document like this
{
"_id": {
"$oid": "6241dd90891458501c17d627"
},
"A": [
{
"_id": {
"$oid": "6241ddb1891458501c17d63e"
},
"B": [
{
"_id": {
"$oid": "6241ddc4891458501c17d674"
}
},
{
"_id": {
"$oid": "6241ddda891458501c17d675"
}
}
]
},
{
"_id": {
"$oid": "6241ddbe891458501c17d63f"
},
"B": [
{
"_id": {
"$oid": "6241ddda891458501c17d678"
}
},
{
"_id": {
"$oid": "6241ddda891458501c17d679"
}
}
]
}
]
}
This document has 2 nested arrays: an array of "A" elements, inside each element of "A" there's an array of "B" elements. I need to search by an _id of a "B" element, let's say 6241ddda891458501c17d679. I need a way to obtain this structure in mongodb
{
"_id": {
"$oid": "6241dd90891458501c17d627"
},
"A": [
{
"_id": {
"$oid": "6241ddbe891458501c17d63f"
},
"B": [
{
"_id": {
"$oid": "6241ddda891458501c17d679"
}
}
]
}
]
}
How can I achieve this? Thanks very much
Maybe something like this:
Option 1, Find:
db.collection.find({
"A.B._id": {
"$oid": "6241ddda891458501c17d679"
}
},
{
"A": {
"$filter": {
"input": {
"$map": {
"input": "$A",
"as": "a",
"in": {
"_id": "$$a._id",
"B": {
"$filter": {
"input": "$$a.B",
"as": "b",
"cond": {
"$eq": [
{
"$oid": "6241ddda891458501c17d679"
},
"$$b._id"
]
}
}
}
}
}
},
"as": "an",
"cond": {
"$ne": [
"$$an.B",
[]
]
}
}
}
})
Explained:
Use find() with match query on "A.B._id" ( good to have index on this filed for best performance)
In the filter part add $filter/map/filter combination to filter only the matching _id for array B elements and preserve the array A _id , also in the initial filter condition use only non-empty arrays [] to avoid having elements from empty arrays in the final result.
playground1
Option 2 , aggregation:
db.collection.aggregate([
{
$match: {
"A.B._id": {
"$oid": "6241ddda891458501c17d679"
}
}
},
{
"$addFields": {
"A": {
"$filter": {
"input": {
"$map": {
"input": "$A",
"as": "a",
"in": {
"_id": "$$a._id",
"B": {
"$filter": {
"input": "$$a.B",
"as": "b",
"cond": {
"$eq": [
{
"$oid": "6241ddda891458501c17d679"
},
"$$b._id"
]
}
}
}
}
}
},
"as": "an",
"cond": {
"$ne": [
"$$an.B",
[]
]
}
}
}
}
}
])
playground2

MongoDB group by and SUM by array

I'm new in mongoDB.
This is one example of record from collection:
{
supplier: 1,
type: "sale",
items: [
{
"_id": ObjectId("60ee82dd2131c5032342070f"),
"itemBuySum": 10
},
{
"_id": ObjectId("60ee82dd2131c50323420710"),
"itemBuySum": 10,
},
{
"_id": ObjectId("60ee82dd2131c50323420713"),
"itemBuySum": 10
},
{
"_id": ObjectId("60ee82dd2131c50323420714"),
"itemBuySum": 20
}
]
}
I need to group by TYPE field and get the SUM. This is output I need:
{
supplier: 1,
sales: 90,
returns: 170
}
please check Mongo playground for better understand. Thank you!
$match - Filter documents.
$group - Group by type and add item into data array which leads to the result like:
[
[/* data 1 */],
[/* data 2 */]
]
$project - Decorate output document.
3.1. First $reduce is used to flatten the nested array to a single array (from Result (2)) via $concatArrays.
3.2. Second $reduce is used to aggregate $sum the itemBuySum.
db.collection.aggregate({
$match: {
supplier: 1
},
},
{
"$group": {
"_id": "$type",
"supplier": {
$first: "$supplier"
},
"data": {
"$push": "$items"
}
}
},
{
"$project": {
_id: 0,
"supplier": "$supplier",
"type": "$_id",
"returns": {
"$reduce": {
"input": {
"$reduce": {
input: "$data",
initialValue: [],
in: {
"$concatArrays": [
"$$value",
"$$this"
]
}
}
},
"initialValue": 0,
"in": {
$sum: [
"$$value",
"$$this.itemBuySum"
]
}
}
}
}
})
Sample Mongo Playground
db.collection.aggregate([
{
$match: {
supplier: 1
},
},
{
"$group": {
"_id": "$ID",
"supplier": {
"$first": "$supplier"
},
"sale": {
"$sum": {
"$cond": {
"if": {
"$eq": [
"$type",
"sale"
]
},
"then": {
"$sum": "$items.itemBuySum"
},
"else": {
"$sum": 0
}
}
}
},
"returns": {
"$sum": {
"$sum": {
"$cond": {
"if": {
"$eq": [
"$type",
"return"
]
},
"then": {
"$sum": "$items.itemBuySum"
},
"else": {
"$sum": 0
}
}
}
}
}
}
},
{
"$project": {
_id: 0,
supplier: 1,
sale: 1,
returns: 1
}
}
])

how to project by a value as a property in mongodb?

I have this Object:
{
"_id":"1",
"a":"b",
"b":"wanted value"
},
{
"_id":"2",
"a":"c",
"c":"wanted value 2"
}
how can i get the value of a and make it the property with which i project to get "wanted value"/"wanted value 2" ?
wanted output:
{
"_id":"1",
"b":"wanted value"
},
{
"_id":"2",
"c":"wanted value 2"
}
Use $objectToArray to convert the root document into an array of k-v tuples then use $reduce to process the array.
db.collection.aggregate([
{
"$addFields": {
"arr": {
"$objectToArray": "$$ROOT"
}
}
},
{
"$addFields": {
"v": {
"$reduce": {
"input": "$arr",
"initialValue": null,
"in": {
"$cond": {
"if": {
$eq: [
"$$this.k",
"a"
]
},
"then": "$$this.v",
"else": "$$value"
}
}
}
}
}
},
{
"$set": {
"arr": {
"$filter": {
"input": "$arr",
"as": "tuple",
"cond": {
$eq: [
"$$tuple.k",
"$v"
]
}
}
}
}
},
{
"$set": {
"result": {
"$arrayToObject": "$arr"
}
}
},
{
"$replaceRoot": {
"newRoot": "$result"
}
}
])
Here is the Mongo playground for your reference.

Query Mongodb using condition from previous field defined via $first

Trying to total a documents by a condition where timestamp equals the first timestamp found
Need to sum the number of documents that meet the condition that the document matched the first timestamp value found. Below is what I've tried in order to reference the timestamp equal to { "$first": "$timestamp" }
IP.aggregate([
{
"$sort":{'timestamp':-1}
},
{
"$group": {
"_id": "$application",
"url": { "$first": "$app_url_name" },
"timestamp": { "$first": "$timestamp" },
"total": {
$sum: {
$cond: {
if: {
$and:[
{$eq: ['$environment_category', 'PROD']},
{$eq: ['$timestamp', '$_id.timestamp']}
]
},
then: 1,
else: 0
}
}
},
"enabled": { $sum: {$cond: {if: {
$and: [
{$eq: ['$availability', 'available']},
{$eq: ['$state', 'enabled']},
{$eq: ['$environment_category', 'PROD']}
]
} ,
then: 1,
else: 0} }}
}
},
Here's the solution:
db.ips.aggregate([
{
"$sort": { "timestamp": -1 }
},
{
"$group": {
"_id": "$application",
"doc": { "$first": "$$ROOT" },
"items": {"$push": "$$ROOT"}
}
},
{
"$project": {
"_id": 0,
"application": "$_id",
"timestamp": "$doc.timestamp",
"items": {
"$filter": {
"input": "$items",
"as": "item",
"cond": { "$eq": [ "$$item.timestamp", "$doc.timestamp" ] }
}
}
}
},
]);