this is my current situation:
user ID
User Name
Stats
0001 >
UserNr1
33
UserNr1
22
0002 >
UserNr2
12
UserNr2
20
What i want to achieve is
user ID
User Name
Stats
0001
UserNr1 >
33
22
0002
UserNr2 >
12
20
This way, groups should be sortable by user ID and User Name.
Can someone help me with this?
Example data in JSON:
[
{
"userId": "0001",
"userName": "UserNr1",
"stats": "33"
},
{
"userId": "0001",
"userName": "UserNr1",
"stats": "22"
},
{
"userId": "0002",
"userName": "UserNr2",
"stats": "12"
},
{
"userId": "0002",
"userName": "UserNr2",
"stats": "20"
},
]
Related
I am new to mongoDB modelling, I have been working in a small app that just used to have one collection with all my data like this:
{
"name": "Thanos",
"age": 999,
"lastName": "whatever",
"subjects": [
{
"name": "algebra",
"mark": 999
},
{
"name": "quemistry",
"mark": 999
},
{
"name": "whatever",
"mark": 999
}
]
}
I know this is standard in mongoDB since we don't have to map relotionships to other collections like in a relational database. My problem is that my app is growing and my json, even tho it works perfectly fine, it is starting to be huge since it has a few more (and quite big) nested fields:
{
"name": "Thanos",
"age": 999,
"lastName": "whatever",
"subjects": [
{
"name": "algebra",
"mark": 999
},
{
"name": "quemistry",
"mark": 999
},
{
"name": "whatever",
"mark": 999
}
],
"tutors": [
{
"name": "John",
"phone": 2000,
"status": "father"
},
{
"name": "Anne",
"phone": 200000,
"status": "mother"
}
],
"exams": [
{
"id": "exam1",
"file": "file"
},
{
"id": "exam2",
"file": "file"
},
{
"id": "exam3",
"file": "file"
}
]
}
notice that I have simplified the json a lot, the nested fields have way more fields. I have two questions:
Is this a proper way to model Mongodb one to many relationships and how do I avoid such long json documents without splitting into more documents?
Isn't it a performance issue that I have to go through all the students just to get subjects for example?
I'm using mongoDB with PHP. I know how to get the document based on product_id, but I need only a specific object from the whole document, I don't know how to get only specific object from nested array based on product_id.
for ex. My expected output is:
products": [
{
"product_id": 547,
"name": "cola",
"quantity": 24
}
]
then make some changes on object p.s update the quantity then update it to the database.
My collection looks like
"_id": {
"$oid": "62ed30855836b16fd38a00b9"
},
"name": "drink",
"products": [
{
"product_id": 547,
"name": "cola",
"quantity": 24
},
{
"product_id": 984,
"name": "fanta",
"quantity": 42
},
{
"product_id": 404,
"name": "sprite",
"quantity": 12
},
{
"product_id": 854,
"name": "water",
"quantity": 35
}
]
}
Try this:
db.getCollection('test').aggregate([
{
"$unwind": "$products"
},
{
"$match": {
"products.product_id": 547
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
"$$ROOT",
"$products"
]
}
}
},
{
"$project": {
"products": 0
}
}
])
The query gives the following output:
{
"name" : "cola",
"product_id" : 547,
"quantity" : 24
}
I want to add a current date into a user meal plan breakfast and find
the name of an object like a banana on condition and add a date to the
completed array?
this is the mongoose document .every food item contains an array of completed and every time a request is sent it will add or remove date on the completed array. i should be able to push date to the completed array
{
"_id": {
"$oid": "616f193e9eb4ca14afc47fa8"
},
"date": {
"$date": "2021-10-19T15:52:09.298Z"
},
"email": "john#live.com",
"firstName": "john",
"lastName": "doe",
"password": "$2b$10$jKylT8MKsUl7ZS03EVi7SecQ1eWuB37J5rLVTU/C8VXBwkWQQo6EK",
"role": "user",
"token": "eyJhbGciOiJIQb-JA2kcKxWquDaM",
"mealplan": {
"dietType": ["Halal", "Vegetarian", "Vegan"],
"religionType": ["Agnostic", "Muslim", "Christian", "Buddhist"],
"ethinicType": ["Asian", "Native American"],
"_id": "6179a178ba48312626059d44",
"name": "first meal",
"budget": 2000,
"orac": 163,
"medicinal": 0,
"breakfast": [
{
"type": "breakfast",
"completed": [],
"fdcId": 1103194,
"name": "Carrots, raw, salad",
"numbers": 1,
"unit": "100 grams",
"recurring": {
"startTime": "08:30",
"endTime": "09:30",
"startDate": "2021-10-28",
"endDate": "2021-11-03",
"daysofweek": []
}
},
{
"type": "breakfast",
"completed": [],
"name": "orange juice",
"numbers": 1,
"unit": "100 mililiters",
"recurring": {},
"orac": 10,
"medicinal_value": 0,
"nutrition": []
},
{
"type": "breakfast",
"name": "banana",
"completed": [],
"numbers": 1,
"unit": "100 grams"
}
]
},
"__v": 0
}
when I send a post request it will be the name of the food and date.
date show be added to the completed array in name of food
use arrayFilter like this
for example we want add to food with name of bnana
db.collection.update({},
{
$push: {
"mealplan.breakfast.$[arr].completed": "date" // put date here
}
},
{
arrayFilters: [
{
"arr.name": "banana" // name of food here
}
]
})
https://mongoplayground.net/p/lbzcF0YPuDF
How to use spring mongo query criteria based only for below thing.
I don't want to use spring mongo save method even though its doing upsert because of application is running in multi threaded environment so only specified values should updated and remaining values will not change.
Suppose the document looks like:
{
"dept": "FY",
"companyName": "TIS",
"id":"112234",
"contacts": [{
"name": "Jeff",
"phone": "222 - 572 - 8754"
},
{
"name": "Joe",
"phone": "456 - 875 - 4521"
},
{
"name": "Alex",
"phone": "789 - 875 - 4521"
}
]
}
based on id and the array list passing to update method the contacts list should update.
Lets take based on id=112234 and passing contacts list to update method, based on contact name it should update.
"contacts": [{
"name": "Jeff",
"phone": "111 - 000 - 123"
},
{
"name": "Joe",
"phone": "980 - 546 - 0000"
}
]
the final mongo json document should updated as below:
{
"dept": "FY",
"companyName": "TIS",
"id":"112234",
"contacts": [{
"name": "Jeff",
"phone": "111 - 000 - 123"
},
{
"name": "Joe",
"phone": "980 - 546 - 0000"
},
{
"name": "Alex",
"phone": "789 - 875 - 4521"
}
]
}
I have the following db structure:
[
{
"_id": 1,
"family": "First Family",
"kids": [
{
"name": "David",
"age": 10
},
{
"name": "Moses",
"age": 15
}
]
},
{
"_id": 1,
"family": "Second Family",
"kids": [
{
"name": "Sara",
"age": 17
},
{
"name": "Miriam",
"age": 45
}
]
}
]
I want to select all families that have a kid that his age + 10 is bigger then 30.
What would be the best way to achieve this?
please find query below
db.collection.find({ "kids.age":{$gt:20}})