Adding parent field value in subdocuments while concating - mongodb

I want to add parent field value inside its subDocs
I want task name inside all of its subdocuments. Example document:
{
_id: 1,
tasks: [
{
_id: 1,
assigned: [
{
_id: 1,
name: "assigned1",
solutions: [
{_id: 1, name: "solution 1"},
{_id: 2, name: "solution 2"}
]
},
{
_id: 2,
name: "assigned2",
solutions: [
{_id: 1, name: "solution 1"},
{_id: 2, name: "solution 2"}
]
}
]
}
]
}
Below is Necessary data related to this question
Mongo PlayGround

If I understand correctly, you want to use $map and $mergeObjects for three hierarchies:
db.collection.aggregate([
{
$set: {
tasks: {
$map: {
input: "$tasks",
as: "task",
in: {
$mergeObjects: [
"$$task",
{
assigned: {
$map: {
input: "$$task.assigned",
as: "assigned",
in: {
$mergeObjects: [
"$$assigned",
{
solutions: {
$map: {
input: "$$assigned.solutions",
as: "solution",
in: {
$mergeObjects: [
"$$solution",
{
taskName: "$$assigned.name"
}
]
}
}
}
}
]
}
}
}
}
]
}
}
}
}
}
])
See how it works on the playground example

Related

Merge nested array data in single array and get them into root of document

I am struggling with retrieving nested document in root.
below i have a schema inside it there is a array of tasks in which objects are present,
in those objects there is again an array of assigned objects and in those objects i have solution array.
now i want to merge all solutions in one array and get that solution array in root od document.
Schema -
{
_id: 1,
tasks: [
{
_id: 1,
assigned: [
{
_id: 1,
solutions: [
{
_id: 1,
name: 'solution 1',
},
{
_id: 2,
name: 'solution 2',
},
],
},
{
_id: 2,
solutions: [
{
_id: 1,
name: 'solution 1',
},
{
_id: 2,
name: 'solution 2',
},
],
},
],
},
],
};
I want to merge all solutions to a single array based on some condition and set that array into new field in root of collection.
const order = this.orderModel
.aggregate([
{ $match: { _id: orderId, student: studentId } },
{
$addFields: {
solutions: {
$map: {
input: '$tasks.assigned.solutions',
as: 's',
in: '$$s',
},
},
},
},
])
.exec();
output i am getting -
"solutions": [
[
[
{
"id": 1,
"name": "solution 1",
}
],
[
{
"_id": 2,
"name": "solution 2"
}
]
]
],
Maybe something like this:
db.collection.aggregate([
{
"$project": {
"solutions": {
"$reduce": {
"input": "$tasks",
"initialValue": [],
"in": {
"$concatArrays": [
"$$value",
{
$reduce: {
input: "$$this.assigned",
initialValue: [],
in: {
$concatArrays: [
"$$value",
"$$this.solutions"
]
}
}
}
]
}
}
}
}
}
])
Explained:
Use $project and two nested $reduce/$concatArrays to join the "solutions" array objects under the new array field "solutions" in the document root.
Playground
if you want to filter based on some condition you can replace "$$this.solutions" with:
{
$filter: {
input: "$$this.solutions",
cond: { $eq: ["$$this._id", 1] }
}
}
will filter only documents with _id:1
see example here

MongoDB Query: How can I aggregate an array of objects as a string

I have an array of objects where I want to make a string concatenating all of the same attributes from the array. Example:
{
_id: 123,
example_document: true,
people: [
{
name: "John",
age: 18
}, {
name: "Clint",
age: 20
}
]
}
And I wanna make a query where my result would be:
{
_id: 123,
example_document: true,
people: [
{
name: "John",
age: 18
}, {
name: "Clint",
age: 20
}
],
concat_names: "John, Clint"
}
I think aggregate is the path I should take, but I'm not being able to find a way of getting a string out of this, only concat array elements into another array. Anyone could help?
You can use $concat combined with $reduce to achieve this, like so:
db.collection.aggregate([
{
$addFields: {
concat_names: {
$reduce: {
input: "$people",
initialValue: "",
in: {
$concat: [
"$$value",
{
$cond: [
{
$eq: [
{
"$strLenCP": "$$value"
},
0
]
},
"",
", "
]
},
"$$this.name"
]
}
}
}
}
}
])
Mongo Playground
You can use aggregation operators $project, and $map:
db.collection.aggregate([
{
$project:
{
concat_names:
{
$map:
{
input: "$people",
as: "i",
in: "$$i.name"
}
}
}
}])

How to update values in string array in all documents? - MongoDB

I have in my collection this structure:
{
_id: ObjectId('...'),
images: [
"images/key1",
"images/key2",
"images/key3",
"images/key4"
],
.... ,
....
}
So, I want to update all documents to:
{
_id: ObjectId('...'),
images: [
"key1",
"key2",
"key3",
"key4"
],
.... ,
....
}
Replacing in all values 'images/' with ''. Thanks 😁
you could done it with update aggregation like this
first match the doc and then in project use map and them split and choose last element
db.collection.update({},
[
{
$addFields: {
images: {
$map: {
input: "$images",
as: "i",
in: {
$last: {
$split: [
"$$i",
"images/"
]
}
}
}
}
}
}
],{multi:true})
https://mongoplayground.net/p/6fDBAlpKDBj
or use this
db.collection.update({},
[
{
$addFields: {
images: {
$map: {
input: "$images",
as: "i",
in: {
$arrayElemAt: [
{
$split: [
"$$i",
"images/"
]
},
1
]
}
}
}
}
}
],{multi:true})
replace $last with $arrayelementAt
https://mongoplayground.net/p/ecHMquZGazy

Mongodb - Merge nested subdocuments array based on specific properties

I've built an aggregation that is returning this data:
[
{
"users": [
{
_id: 1,
name: "John",
age: 31
},
{
_id: 2,
name: "Jane",
age: 26
}
],
"teams": [
{
id: 1,
name: "Team 1",
color: "yellow"
},
{
id: 2,
name: "Team 2",
color: "red"
}
],
"moreTeams": [
{
id: 1,
name: "Team 1 - More",
},
{
id: 2,
name: "Team 2 - More",
},
{
id: 3,
name: "Team 3 - More",
extra: "extra"
}
]
}
]
How can I group "teams" and moreTeams into the same array (based on id), keeping all properties and eventually overriding where they have the same names?
Here's my desired result:
[
{
"users": [
{
_id: 1,
name: "John",
age: 31
},
{
_id: 2,
name: "Jane",
age: 26
}
],
"groupedTeams": [
{
id: 1,
name: "Team 1 - More",
color: "yellow"
},
{
id: 2,
name: "Team 2 - More",
color: "red"
},
{
id: 3,
name: "Team 3 - More",
extra: "extra"
}
]
}
]
I've tried using $unwind and $group with no success :(
Palyground example: https://mongoplayground.net/p/yZY8oQ9r-N1
$map to iterate loop of moreTeams array
$filter to iterate loop of teams array and filter matching object by id
$arrayElemAt to get first element object from above filter
$mergeObjects to merge above object from arrayElemAt and current object
db.collection.aggregate([
{
$addFields: {
moreTeams: {
$map: {
input: "$moreTeams",
as: "m",
in: {
$mergeObjects: [
{
$arrayElemAt: [
{
$filter: {
input: "$teams",
cond: { $eq: ["$$m.id", "$$this.id"] }
}
},
0
]
},
"$$m"
]
}
}
}
}
},
Now we have merged moreTeams in teams but we have to merge teams in moreTeams as well
$project, $filter to iterate loop of teams array and match condition if id is not in moreTeams
$concatArrays to concat moreTeams and above filtered array that is not in moreTeams
{
$project: {
users: 1,
moreTeams: {
$concatArrays: [
"$moreTeams",
{
$filter: {
input: "$teams",
cond: { $not: { $in: ["$$this.id", "$moreTeams.id"] } }
}
}
]
}
}
}
])
Playground

Get list of child item ids that misses tag of parent in MongoDB

I want to get all items (or better ids of items) that have missing tags.
If a child item misses the tag of its parent it should be outputted.
The documents looks like this:
[
{
id: 1,
tags: [
"a",
"b"
],
childs: [
2,
3
]
},
{
id: 2,
tags: [
"a"
],
childs: []
},
{
id: 3,
tags: [],
childs: []
},
{
id: 4,
tags: [
"c"
],
childs: [
5
]
},
{
id: 5,
tags: [
"c"
],
childs: []
},
{
id: 6,
tags: [
"b"
],
childs: [
5
]
},
{
id: 7,
tags: [],
childs: []
},
]
Now I want to search by a tag name to get items with missing tag.
The desired result should look like this:
Check for tag "a":
{
id: 3,
...
}
or
{
ids: [3]
}
Check for tag "b":
{
id: 2,
...
},
{
id: 3,
...
}
or
{
ids: [2, 3]
}
I tried aggregation with the lookup and pipeline function, but did not got it working.
[{
$match: {
tags: "a",
childs: {
$ne: []
}
}
}, {
$lookup: {
from: 'collection1',
localField: 'childs',
foreignField: 'id',
as: 'childs_items',
pipeline: [{
$matching : {
"tags": {
$nin: "a"
}
}
}]
}
}]
What would be the best approach?
EDIT: changed document example tags of last two docs to "c"
EDIT2: added example data
Might be there will be other easy ways but, this is your corrected query,
$match conditions as usual,
$lookup with pipeline, define variable for childs to access inside lookup
$match conditions, childs match or not, tags not equal to specified character
$project to create ids array, using $reduce
db.collection.aggregate([
{
$match: {
tags: "b", // add your search
childs: { $ne: [] }
}
},
{
$lookup: {
from: "collection",
let: { childs: "$childs" },
as: "ids",
pipeline: [
{
$match: {
$expr: { $in: ["$id", "$$childs"] },
tags: { $ne: "b" } // add your search
}
}
]
}
},
{
$project: {
id: 1,
ids: {
$reduce: {
input: "$ids",
initialValue: [],
in: { $concatArrays: ["$$value", ["$$this.id"]] }
}
}
}
}
])
Playground
Your last edit, you can use $unwind and then $group after $lookup in above example, remove $project stage,
{ $unwind: "$ids" },
{
$group: {
_id: null,
ids: { $push: "$ids.id" }
}
}
Playground