How to join related tree repository with TypeORM - nestjs

I have this situation:
This is my tree entity where each item can be a parent item or child item of one of the same entity and each of them are related to an artist.
#Entity()
#Tree("materialized-path")
export class ArtistCommissionSetupItem {
#PrimaryGeneratedColumn({type: "bigint"})
id:number
#ManyToOne(()=>Artist, artist => artist.id, {onDelete: 'CASCADE'})
#JoinColumn()
#Index()
artist:Artist
#Column()
#Index()
name:string
#Index()
#Column({nullable: true})
enabled:boolean
#TreeParent()
parent:ArtistCommissionSetupItem
#TreeChildren()
childrens:ArtistCommissionSetupItem[]
}
The thing here is that I want to get the artist with his related commission setup items and I can do that but the result of commission setup items leftJoinAndSelect method doesn't come with the items nested as tree/child tree. I want the data of commission setup items come like findTrees() method returns it.
An example of how the data come:
"commissionSetupItems": [
{
"id": "154",
"name": "Body",
"enabled": null,
"price": null,
"createdAt": "2021-09-23T21:17:05.630Z",
"updatedAt": "2021-09-23T21:17:05.630Z"
},
{
"id": "155",
"name": "icon",
"enabled": true,
"price": "120.00",
"createdAt": "2021-09-23T21:17:05.630Z",
"updatedAt": "2021-09-23T21:17:05.630Z"
},
{
"id": "156",
"name": "Headshot",
"enabled": false,
"price": "120.00",
"createdAt": "2021-09-23T21:17:05.630Z",
"updatedAt": "2021-09-23T21:17:05.630Z"
}
}
an example of how I want the data comes:
{
"commissionSetupItems": [
{
"id": 1,
"name": "Characters",
"key": "characters",
"childrens": [
{
"id": 5,
"name": "Number of characters",
"key": "number_of_characters",
"childrens": []
}
]
},
{
"id": 2,
"name": "Body",
"key": "body",
"childrens": [
{
"id": 6,
"name": "Icon",
"key": "icon",
"childrens": []
},
{
"id": 7,
"name": "Chibi",
"key": "chibi",
"childrens": []
},
{
"id": 8,
"name": "Headshot",
"key": "headshot",
"childrens": []
},
{
"id": 9,
"name": "Torso or Burst",
"key": "torso",
"childrens": []
},
{
"id": 10,
"name": "Half body",
"key": "half_body",
"childrens": []
},
{
"id": 11,
"name": "Full body",
"key": "full_body",
"childrens": []
}
]
}
}
I can't figure out how to do this.

Related

is there any mongo db query to only add new elements the nested elements by id

my data structure is and products elements all tags in empty
I need to add a value to mongo db inner object with array element without looking at other value. Can some one advice to do this.
[
{
"item": "journal",
"id": 11,
"products": [
{
"id": 3,
"name": "p one",
"tags": []
}
]
},
{
"item": "notebook",
"id": 14,
"products": [
{
"id": 4010,
"name": "item-five",
"tags": []
}
]
}
]
and I want to add tag in products elements with match product id '4010', so the expected response should be like
[
{
"item": "journal",
"id": 11,
"products": [
{
"id": 3,
"name": "p one",
"tags": []
}
]
},
{
"item": "notebook",
"id": 14,
"products": [
{
"id": 4010,
"name": "item-five",
"tags": [
{
"id": 21,
"key": "tag-one",
"name": "tag-one",
}
]
}
]
}
]
You should look at arrayFilter option https://www.mongodb.com/docs/v6.0/reference/operator/update/positional-filtered/#update-all-documents-that-match-arrayfilters-in-an-array
Something like this might do the trick.
db.collection.updateMany(
{ },
{ $push: { "products.$[x].tags": {
"id": 21,
"key": "tag-one",
"name": "tag-one",
} } },
{ arrayFilters: [{ "x.id": 4010 }] }
);

Remove nested categories in mongo

I want when a category is deleted. All its child categories and its own categories will be deleted.
For example, I want if the web development category is deleted. All its child categories (delete Django and Django Channel)
{
"statusCode": 200,
"data": {
"categories": [
{
"_id": "62fdfb567bb46dcf93b3717f",
"title": "Web development",
"parent": null,
"position": "important",
"slug": "-eb-development",
"createdAt": "2022-08-18T08:41:58.826Z",
"updatedAt": "2022-08-18T08:41:58.826Z",
"children": [
{
"_id": "62fdfb677bb46dcf93b3718d",
"title": "Django",
"parent": "62fdfb567bb46dcf93b3717f",
"position": "unimportant",
"slug": "-jango",
"createdAt": "2022-08-18T08:42:15.557Z",
"updatedAt": "2022-08-18T08:42:15.557Z",
"children": [
{
"_id": "62fdfb857bb46dcf93b371a1",
"title": "Django chennels",
"parent": "62fdfb677bb46dcf93b3718d",
"position": "unimportant",
"slug": "-jango-chennels",
"createdAt": "2022-08-18T08:42:45.665Z",
"updatedAt": "2022-08-18T08:42:45.665Z",
"children": [],
"id": "62fdfb857bb46dcf93b371a1"
}
],
"id": "62fdfb677bb46dcf93b3718d"
}
],
"id": "62fdfb567bb46dcf93b3717f"
}
]
}
}
I wrote a code for this which does not work properly and removes only one child from the desired category.
const category = await this.checkExistCategory(id);
const deleteResult = await Category.deleteMany({
$or: [{ _id: category._id }, { parent: category._id }],
});
Thank you very much for helping me!!

Joining only matching elements

I want to join more than two collections in MongoDB . Is it possible to join?
I have 2 collections and I want to join them based by their ids
news collection
{
"lang": "ru",
"news": [{
"title": "title",
"content": "desc",
"image": "path",
"categoryId": {
"$oid": "6171499e0ba3d75082823c9a"
},
"createdAt": {
"$date": "2021-10-21T12:30:33.215Z"
},
"_id": {
"$oid": "61715d7990905adefcf314d1"
}
}]
}
and I have got newscategory collection like that
[
{
"lang": "ru",
"categories": [{
"name": "sdfsdsdcsdf",
"_id": {
"$oid": "6171499e0ba3d75082823c9a"
}
}]
},
{
"lang": "en",
"categories": [{
"name": "ooo",
"_id": {
"$oid": "61712980f8ee795c6a569dcb"
}
}, {
"name": "yuy",
"_id": {
"$oid": "61712980f8ee795c6a569dcc"
}
}, {
"name": "rtyrty",
"_id": {
"$oid": "61712980f8ee795c6a569dcd"
}
}]
}
]
Expected Output:
[
{
"_id": "61715d7990905adefcf314d0",
"lang": "ru",
"news": [
{
"title": "sdf",
"content": "sdf",
"image": "asdas",
"categoryId": "6171499e0ba3d75082823c9a",
"caregory": [
{
"name": "sdfsdsdcsdf",
"_id": "6171499e0ba3d75082823c9a"
},
]
"createdAt": "2021-10-21T12:30:33.215Z",
"_id": "61715d7990905adefcf314d1"
}
],
Match is based on both "category_id" and "categories._id".
How can I achieve result like that ?

Query to retrieve every subdocument alone without passing parent id using Mongoose

Here i like to explain my problem.
How can i write a mongoose query to retrieve every subdocument from JSON without passing parent_id.
[
{
"_id": "56a320003fe17cc7363dd0d7",
"name": "Leanna Jacobson",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Riley Case"
},
{
"id": 1,
"name": "Herman Carter"
},
{
"id": 2,
"name": "Pacheco Woodard"
}
]
},
{
"_id": "56a3200001501cfa1ea2641d",
"name": "Juliana Bonner",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Keller Woodward"
},
{
"id": 1,
"name": "Fern Knight"
},
{
"id": 2,
"name": "Cain Richards"
}
]
},
{
"_id": "56a3200006864c78ecb1aeed",
"name": "Gena Stark",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Kate Franco"
},
{
"id": 1,
"name": "Araceli Mcclure"
},
{
"id": 2,
"name": "Molly Nelson"
}
]
},
{
"_id": "56a320006d868155161038b6",
"name": "Eve Gonzalez",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Pam Lang"
},
{
"id": 1,
"name": "Christy Marks"
},
{
"id": 2,
"name": "Donovan Warren"
}
]
},
{
"_id": "56a3200066b94852f5680568",
"name": "Coleman Wooten",
"gender": "male",
"friends": [
{
"id": 0,
"name": "Roberta Olson"
},
{
"id": 1,
"name": "Roseann Reid"
},
{
"id": 2,
"name": "Kerri Russell"
}
]
}
]
Here i need to retrieve every friends details from the subdocument array friends for every parent.
so how can i write query for this?????
Suppose the name of your schema is Person, try this one.
//find all document, only select `friends` field from every document
Person.find({}, 'friends', function (err, friends) {
// the return friends is the [[friends], [friends], ...]
});

Get Price range based on products Using MongoDb..?

I have my products being maintained in Mongo collection.
Please suggest a mongo query which will fetch price range based on the number of products available and its corresponding price. I have the following product collection..
[
{
"_id": "563b2574db70394054f35dc5",
"category": {
"id": "101010"
},
"brand": {
"name": "Global Desi"
},
"is_active": true,
"parent_sku": "88903689927175",
"price": 1234,
"sku": "1321asas",
"name": "Buddha print kurta",
"quantity": 1
},
{
"_id": "563b2574db70394054f35dc6",
"category": {
"id": "101010"
},
"brand": {
"name": "Global Desi"
},
"is_active": true,
"parent_sku": "88903689927175",
"price": 5678,
"sku": "1322asas",
"name": "Buddha print kurta",
"quantity": 1
},
{
"_id": "563b2574db70394054f35dc7",
"category": {
"id": "101010"
},
"brand": {
"name": "Global Desi"
},
"is_active": true,
"parent_sku": "88903689927175",
"price": 2345,
"sku": "1323asas",
"name": "Buddha print kurta",
"quantity": 1
},
{
"_id": "563b2574db70394054f35dc8",
"category": {
"id": "101010"
},
"brand": {
"name": "Global Desi"
},
"is_active": true,
"parent_sku": "88903689927175",
"price": 7890,
"sku": "1324asas",
"name": "Buddha print kurta",
"quantity": 1
}
]
range 501 - 1000, for example:
db.collection.find({price: {$gte: 501, $lte: 1000}}).sort({price:1})

Resources