Query to retrieve every subdocument alone without passing parent id using Mongoose - node.js

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], ...]
});

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 }] }
);

How to filter the response from mongoDB, so nested arrays will include only items that meet a condition?

My documents look like this
{
"_id": {
"$oid": "62825f71005ce00c5f0235c1"
},
"user": "jon",
"roles": {
"User": 2001,
},
"STOCK ": [
{
"sku": "BLACK-M",
"productname": "BLACK",
"sendout": 0,
"recived": 1,
"totalinstock": 40,
"location": "B32",
"_id": {
"$oid": "62826016005ce00c5f0235c8"
}
},
{
"sku": "CJNS",
"productname": "89796363",
"sendout": 0,
"recived": 45,
"totalinstock": 0,
"location": "B232",
"_id": {
"$oid": "62836f2d56b4f1ac79c99b8d"
}
}
],
"ORDERS": [
{
"date": {
"$date": "2022-06-02T15:23:58Z"
},
"OrderNumber": "745607",
"City": "xxxxx",
"Address": "yyyyyy",
"Phone": "8678678678",
"Name": "Holly ",
"Trackingnumber": 40,
"ZipCode": 00000,
"Province": "New ",
"Quantity": [
1
],
"Product_Name": [
" pants pants"
],
"SKU": [
"CJNS"
],
"_id": {
"$oid": "6298d61ba6eeec72b78332a7"
}
},
{
"date": {
"$date": "2022-06-02T15:23:58Z"
},
"OrderNumber": "748840",
"City": "xxxxx",
"Address": "yyyyyy",
"Phone": "354353454",
"Name": "Michael",
"Trackingnumber": 0,
"ZipCode": 00000,
"Province": "North",
"Quantity": [
1
],
"Product_Name": [
" pants pants"
],
"SKU": [
"CJNS"
],
"_id": {
"$oid": "6298d61ba6eeec72b78332a9"
}
}
]
}
I successful to return all the objects in STOCK or all the objects in ORDERS
Through this query
const foundUser= await User.find({"user":req.body.user},("Orders") ).exec()
Now I want to filter the response to include only items where "Trackingnumber" is different from 0
For the sample data I expect to receive only
{
"date": {
"$date": "2022-06-02T15:23:58Z"
},
"OrderNumber": "748840",
"City": "xxxxx",
"Address": "yyyyyy",
"Phone": "354353454",
"Name": "Michael",
"Trackingnumber": 0,
"ZipCode": 00000,
"Province": "North",
"Quantity": [
1
],
"Product_Name": [
" pants pants"
],
"SKU": [
"CJNS"
],
"_id": {
"$oid": "6298d61ba6eeec72b78332a9"
}
}
You can use an aggregation pipeline with a $filter for this:
db.collection.aggregate([
{
$match: {
"user": "jon"
}
},
{
$project: {
ORDERS: {
$filter: {
input: "$ORDERS",
as: "item",
cond: {$ne: ["$$item.Trackingnumber", 0]}
}
}
}
}
])
Playground example
User.find({"Orders" : {"Trackingnumber": 0} })

How to join related tree repository with TypeORM

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.

How to query nested object in mongodb

I'm new to mongodb and I've been trying to query this doc for awhile now.
Im trying to query all the rooms that have a room name of 100.
json
{
"count": 3,
"reviews": [
{
"_id": "5f9d42a0a8e71e004643f584",
"user": {
"_id": "5f7308cde0a4a7a66bc3d184",
"name": "Guest"
},
"room": {
"_id": "5f98f9321fd5bb0045b3d886",
"name": "100",
},
"rating": 4,
},
{
"_id": "5f9d431ea8e71e004643f585",
"user": {
"_id": "5f7308cde0a4a7a66bc3d184",
"name": "Guest",
},
"room": {
"_id": "5f98f9321fd5bb0045b3d886",
"name": "100",
},
"rating": 5,
},
{
"_id": "5f9e74fea6c06a0046d3cae2",
"user": {
"_id": "5f7308cde0a4a7a66bc3d184",
"name": "Guest",
},
"room": {
"_id": "5f98fa8b1fd5bb0045b3d88a",
"name": "300",
},
"rating": 5,
}
]}
what I've tried
1. find({},{"reviews.room.name": {$eq: "100"}}) // getting a projection error
2. find({"reviews.room.name": "100"}) // getting null
Any help to the right direction would be greatly appreciated.
Check out the docs about how to use find:
https://docs.mongodb.com/manual/reference/method/db.collection.find/
and try this
db.reviews.find({
"room.name": "100"
})
Here is the playground with your example: https://mongoplayground.net/p/dPfH5fSOePq
did u try this ? const response = await Room.find({name: "100"})

Mongoose, update of child will affect all parents?

What is the query to update below data with mongoose. So 3 fields are going to be updated. Top Parent Points, Categories Points and Tag Points.
{
"_id": "561fba5e7fac41a4055fad45",
"fullName": "Test",
"points": 45,
"level": 1,
"categories": [
{
"name": "Computer Science",
"points": 15,
"level": 1,
"_id": "561fba5e7fac41a4055fad46",
"tags": [
{
"name": "C#",
"points": 10,
"level": 1,
"_id": "561fba5e7fac41a4055fad47"
},
{
"name": "Java",
"points": 5,
"level": 1,
"_id": "561fba5e7fac41a4055ert12"
}
]
},
{
"name": "History",
"points": 30,
"level": 2,
"_id": "562407d4e3edf2113f61ac37",
"tags": [
{
"name": "WW2",
"points": 30,
"level": 2,
"_id": "56240797e3edf2113f61ac36"
}
]
}
]
}
to this one. When user gets a point from a specific tag, it will effect all parents. Let's say, user gets 10 points from C# then i have to update mongodb to this.
{
"_id": "561fba5e7fac41a4055fad45",
"fullName": "Test",
**"points": 55,**
"level": 1,
"categories": [
{
"name": "Computer Science",
**"points": 25,**
"level": 1,
"_id": "561fba5e7fac41a4055fad46",
"tags": [
{
"name": "c#",
**"points": 20,**
"level": 1,
"_id": "561fba5e7fac41a4055fad47"
},
{
"name": "Java",
"points": 5,
"level": 1,
"_id": "561fba5e7fac41a4055ert12"
}
]
},
{
"name": "History",
"points": 30,
"level": 2,
"_id": "562407d4e3edf2113f61ac37",
"tags": [
{
"name": "WW2",
"points": 30,
"level": 2,
"_id": "56240797e3edf2113f61ac36"
}
]
}
]
}
you should use $elemMatch for querying your object
db.tests.update({_id: yourTestId, categories: {$elemMatch: {_id: categoryId}}}, {$set: {$inc: {"categories.$.points": 10, points: 10}}})
So you querying only needed array element and update it values with $ reference

Resources