Merging documents while storing all ids in an array - mongodb

So in mongodb 3.2 (for reasons, we can't upgrade yet) I have a bunch of documents in this structure:
// Document 1
{
"id": "record-1",
"childItems": [
"child-1",
"child-2"
],
"specLookup":[
{
"specId": "spec-1"
},
{
"specId": "spec-2"
}
]
},
// Document 2
{
"id": "record-2",
"childItems": [
"child-3"
],
"specLookup":[
{
"specId": "spec-3"
}
]
}
I need an aggregation query that will merge and manipulate all these records into one document, while maintaining all individual ids in a new array. So based on the two documents above I'd end up with this:
{
"ids": ["record-1", "record-2"],
"childItems": [
"child-1",
"child-2",
"child-3"
],
"specIds":[
"spec-1"
"spec-2",
"spec-3"
]
}
How can I do this? Cheers!

You can do it like this:
$group - to group all the documents add collect all the ids, childItems and specLookup.
$reduce with $concatArrays - to get the data in the format you want.
db.collection.aggregate([
{
"$group": {
"_id": null,
"ids": {
"$addToSet": "$id"
},
"childItems": {
"$addToSet": "$childItems"
},
"specLookup": {
"$addToSet": "$specLookup.specId"
}
}
},
{
"$set": {
"childItems": {
"$reduce": {
"input": "$childItems",
"initialValue": [],
"in": {
"$concatArrays": [
"$$this",
"$$value"
]
}
}
},
"specLookup": {
"$reduce": {
"input": "$specLookup",
"initialValue": [],
"in": {
"$concatArrays": [
"$$this",
"$$value"
]
}
}
}
}
}
])
Working example

Related

How to search an array of objects in mongoDB without using aggregate query?

We have a use case where the data is stored in the below format
[
{
"Name": [
{
"KM": "2"
},
{
"Weld Joint Number": "JN2"
},
{
"Status": "Accepted"
},
{
"Upstream": "PP1"
},
{
"Downstream": "PP2"
}
]
},
{
"Name": [
{
"Pipe No": "PP5731A-08"
},
{
"Km": "1"
},
{
"Section Length (m)": "12.22"
}
]
}
]
We are checking for the possibility where we need to search the records using the find query(without aggregate) which matches the search criteria for the values in that array of Objects.
In the search scenario, the value can match with any value in the array.
Here's one way to find documents with a given single value in an object nested in arrays.
db.collection.find({
"$expr": {
"$in": [
// search value goes here
"Accepted",
{ // this puts all the values into a flat array
"$reduce": {
// instead of "stuff", use the field name of the array
"input": "$stuff.Name",
"initialValue": [],
"in": {
"$concatArrays": [
"$$value",
{
"$map": {
"input": "$$this",
"as": "elem",
"in": {
"$getField": {
"field": "v",
"input": {"$first": {"$objectToArray": "$$elem"}}
}
}
}
}
]
}
}
}
]
}
})
Try it on mongplayground.net.

Regroup all elements of nested array of nested array into a single array

I have this collection :
{
"_id": ObjectId("6045eec8f113547ddd3472d9"),
"execution": {
"_id": ObjectId("6045eec8f113547ddd3472d8"),
"steps": [
{
"_id": ObjectId("6045eec8f113547ddd3472d7"),
"actions": [
{
"_id": ObjectId("6045eec8f113547ddd3472d6"),
"title": "action title"
}
]
}
]
}
}
and i want to achieve this result:
{
"_id": ObjectId("6045eec8f113547ddd3472d9"),
"allActions": [
{
"_id": ObjectId("6045eec8f113547ddd3472d6"),
"title": "action title"
}
]
}
to clarify more, i want to add an array that groups together all "actions" under the "steps" array
db.getCollection("missions").aggregate([
{
$match: {
_id: ObjectId("6045eec8f113547ddd3472d9")
}
},
{
$addFields: {
"allActions": "$execution.steps"
}
}
])
Could someone help me complete the part of the code that will allow me to achieve the desired output?
You may just use $reduce and $concatArrays to group all the entries in the actions array
db.missions.aggregate([
{
$match: {
_id: ObjectId("6045eec8f113547ddd3472d9")
}
},
{
$addFields: {
"allActions": {
"$reduce": {
"input": "$execution.steps.actions",
"initialValue": [],
"in": {
"$concatArrays": [
"$$value",
"$$this"
]
}
}
}
}
}
])
Here is the Mongo playground for your reference.

How to filter an array of objects in mongoose by date field only selecting the most recent date

I'm trying to filter through an array of objects in a user collection on MongoDB. The structure of this particular collection looks like this:
name: "John Doe"
email: "john#doe.com"
progress: [
{
_id : ObjectId("610be25ae20ce4872b814b24")
challenge: ObjectId("60f9629edd16a8943d2cab9b")
date_unlocked: 2021-08-05T12:15:32.129+00:00
completed: true
date_completed: 2021-08-06T12:15:32.129+00:00
}
{
_id : ObjectId("611be24ae32ce4772b814b32")
challenge: ObjectId("60g6723efd44a6941l2cab81")
date_unlocked: 2021-08-06T12:15:32.129+00:00
completed: true
date_completed: 2021-08-07T12:15:32.129+00:00
}
]
date: 2021-08-04T13:06:34.129+00:00
How can I query the database using mongoose to return only the challenge with the most recent 'date_unlocked'?
I have tried: User.findById(req.user.id).select('progress.challenge progress.date_unlocked').sort({'progress.date_unlocked': -1}).limit(1);
but instead of returning a single challenge with the most recent 'date_unlocked', it is returning the whole user progress array.
Any help would be much appreciated, thank you in advance!
You can try this.
db.collection.aggregate([
{
"$unwind": {
"path": "$progress"
}
},
{
"$sort": {
"progress.date_unlocked": -1
}
},
{
"$limit": 1
},
{
"$project": {
"_id": 0,
"latestChallenge": "$progress.challenge"
}
}
])
Test the code here
Alternative solution is to use $reduce in that array.
db.collection.aggregate([
{
"$addFields": {
"latestChallenge": {
"$arrayElemAt": [
{
"$reduce": {
"input": "$progress",
"initialValue": [
"0",
""
],
"in": {
"$let": {
"vars": {
"info": "$$value",
"progress": "$$this"
},
"in": {
"$cond": [
{
"$gt": [
"$$progress.date_unlocked",
{
"$arrayElemAt": [
"$$info",
0
]
}
]
},
[
{
"$arrayElemAt": [
"$$info",
0
]
},
"$$progress.challenge"
],
"$$info"
]
}
}
}
}
},
1
]
}
}
},
{
"$project": {
"_id": 0,
"latestChallenge": 1
}
},
])
Test the code here
Mongoose can use raw MQL so you can use it.

Min nested array in mongo

How can I use the $min function to get the min value within nested arrays (and add it to the document)?
[
{
"_id": "a357e77f-a76a-4bc2-8765-923280663e97",
"customers": [
{
"_id": "97170117-4660-4c6f-b8da-2b34d4d0c9ce",
"orders": [
{
"amount": 0.5
},
{
"amount": 6.400001525878906
}
]
},
{
"_id": "7b9ccf5b-3acb-4ed1-8df4-e3b5afc49cba",
"orders": [
{
"amount": 27.29999542236328
},
{
"amount": 0.29999542236328125
}
]
}
]
},
{
"_id": "58433224-8162-4f0a-8168-bc11b4306b0a",
"customers": [
{
"_id": "8a6055d0-9b94-40be-8f96-8fd9088d24aa",
"orders": [
{
"amount": 19.700000762939453
}
]
},
{
"_id": "a50a57b8-61e7-4727-a15a-4a4137b2f81a",
"orders": [
{
"amount": 43.80000305175781
}
]
}
]
}
]
How can I get the min amount value within the $customers.orders.amount path?
I've tried but it returns 0.
db.collection.aggregate([
{
$addFields: {
"amount": {
$sum: "$customers.orders.amount"
}
}
}
])
You can do as below for each customer
playground
db.collection.aggregate([
{//Destruct
"$unwind": "$customers"
},
{//Destruct
"$unwind": "$customers.orders"
},
{//Group by customer id,
$group: {
"_id": "$customers._id",
min: {
$push: {
"$min": "$customers.orders.amount"
}
}
}
}
])
You can use group by null if you want to find min across all the customers.
Found a solution using reduce to create a flat array of the nested arrays and then use $min on that. MongoPlayground
db.collection.aggregate([
{
"$addFields": {
"min": {
$min: {
"$reduce": {
"input": "$customers",
"initialValue": [],
"in": {
"$concatArrays": [
"$$value",
"$$this.orders.amount"
]
}
}
}
}
}
}
])

How to find match in documents in Mongo and Mongo aggregation?

I have following json structure in mongo collection-
{
"students":[
{
"name":"ABC",
"fee":1233
},
{
"name":"PQR",
"fee":345
}
],
"studentDept":[
{
"name":"ABC",
"dept":"A"
},
{
"name":"XYZ",
"dept":"X"
}
]
},
{
"students":[
{
"name":"XYZ",
"fee":133
},
{
"name":"LMN",
"fee":56
}
],
"studentDept":[
{
"name":"XYZ",
"dept":"X"
},
{
"name":"LMN",
"dept":"Y"
},
{
"name":"ABC",
"dept":"P"
}
]
}
Now I want to calculate following output.
if students.name = studentDept.name
so my result should be as below
{
"name":"ABC",
"fee":1233,
"dept":"A",
},
{
"name":"XYZ",
"fee":133,
"dept":"X"
}
{
"name":"LMN",
"fee":56,
"dept":"Y"
}
Do I need to use mongo aggregation or is it possible to get above given output without using aggregation???
What you are really asking here is how to make MongoDB return something that is actually quite different from the form in which you store it in your collection. The standard query operations do allow a "limitted" form of "projection", but even as the title on the page shared in that link suggests, this is really only about "limiting" the fields to display in results based on what is present in your document already.
So any form of "alteration" requires some form of aggregation, which with both the aggregate and mapReduce operations allow to "re-shape" the document results into a form that is different from the input. Perhaps also the main thing people miss with the aggregation framework in particular, is that it is not just all about "aggregating", and in fact the "re-shaping" concept is core to it's implementation.
So in order to get results how you want, you can take an approach like this, which should be suitable for most cases:
db.collection.aggregate([
{ "$unwind": "$students" },
{ "$unwind": "$studentDept" },
{ "$group": {
"_id": "$students.name",
"tfee": { "$first": "$students.fee" },
"tdept": {
"$min": {
"$cond": [
{ "$eq": [
"$students.name",
"$studentDept.name"
]},
"$studentDept.dept",
false
]
}
}
}},
{ "$match": { "tdept": { "$ne": false } } },
{ "$sort": { "_id": 1 } },
{ "$project": {
"_id": 0,
"name": "$_id",
"fee": "$tfee",
"dept": "$tdept"
}}
])
Or alternately just "filter out" the cases where the two "name" fields do not match and then just project the content with the fields you want, if crossing content between documents is not important to you:
db.collection.aggregate([
{ "$unwind": "$students" },
{ "$unwind": "$studentDept" },
{ "$project": {
"_id": 0,
"name": "$students.name",
"fee": "$students.fee",
"dept": "$studentDept.dept",
"same": { "$eq": [ "$students.name", "$studentDept.name" ] }
}},
{ "$match": { "same": true } },
{ "$project": {
"name": 1,
"fee": 1,
"dept": 1
}}
])
From MongoDB 2.6 and upwards you can even do the same thing "inline" to the document between the two arrays. You still want to reshape that array content in your final output though, but possible done a little faster:
db.collection.aggregate([
// Compares entries in each array within the document
{ "$project": {
"students": {
"$map": {
"input": "$students",
"as": "stu",
"in": {
"$setDifference": [
{ "$map": {
"input": "$studentDept",
"as": "dept",
"in": {
"$cond": [
{ "$eq": [ "$$stu.name", "$$dept.name" ] },
{
"name": "$$stu.name",
"fee": "$$stu.fee",
"dept": "$$dept.dept"
},
false
]
}
}},
[false]
]
}
}
}
}},
// Students is now an array of arrays. So unwind it twice
{ "$unwind": "$students" },
{ "$unwind": "$students" },
// Rename the fields and exclude
{ "$project": {
"_id": 0,
"name": "$students.name",
"fee": "$students.fee",
"dept": "$students.dept"
}},
])
So where you want to essentially "alter" the structure of the output then you need to use one of the aggregation tools to do. And you can, even if you are not really aggregating anything.