Retrofit kotlin how to write data class with specific variable name - android-studio

This is part of json file
"list": {
"2023-01-12": [
{
"patient_symptom_id": 10,
"date": "2023-01-12",
"datetime": "2023-01-12T11:11:11.000Z",
"symptom_list": [
{
"symptom_id": 1,
"name": "Color",
"color": "#f4f50b",
"symptom_value_ids": [
3,
4
],
"symptom_value_names": [
"Clear",
"Cloudy"
],
},
{
"symptom_id": 2,
"name": "Energy",
"color": "#7ae38b",
"symptom_value_ids": [
27,
42
],
"symptom_value_names": [
"Anxious",
"High"
],
},
{
"symptom_id": 4,
"name": "Stool",
"color": "#f2b706",
"symptom_value_ids": [
14,
29
],
"symptom_value_names": [
"Constipation (1-2)",
"Unable to Pass"
],
},
{
"symptom_id": 6,
"name": "General Symptoms",
"color": "#951af6",
"symptom_value_ids": [
31,
34
],
"symptom_value_names": [
"Fever",
"Headache"
],
},
{
"symptom_id": 7,
"name": "Urinary Symptoms",
"color": "#23a7f1",
"symptom_value_ids": [
37
],
"symptom_value_names": [
"Urgency when urinating"
],
},
{
"symptom_id": 8,
"name": "Menstrual",
"color": "#f413d6",
"symptom_value_ids": [
45
],
"symptom_value_names": [
"Light flow"
],
}
],
}
],
"2023-01-26": [
{
"patient_symptom_id": 11,
"date": "2023-01-26",
"datetime": "2023-01-26T11:38:11.000Z",
"symptom_list": [
{
"symptom_id": 1,
"name": "Color",
"color": "#f4f50b",
"symptom_value_ids": [
3,
4
],
"symptom_value_names": [
"Clear",
"Cloudy"
],
},
{
"symptom_id": 2,
"name": "Energy",
"color": "#7ae38b",
"symptom_value_ids": [
27,
42
],
"symptom_value_names": [
"Anxious",
"High"
],
},
{
"symptom_id": 4,
"name": "Stool",
"color": "#f2b706",
"symptom_value_ids": [
14,
29
],
"symptom_value_names": [
"Constipation (1-2)",
"Unable to Pass"
],
},
{
"symptom_id": 6,
"name": "General Symptoms",
"color": "#951af6",
"symptom_value_ids": [
31,
34
],
"symptom_value_names": [
"Fever",
"Headache"
],
},
{
"symptom_id": 7,
"name": "Urinary Symptoms",
"color": "#23a7f1",
"symptom_value_ids": [
37
],
"symptom_value_names": [
"Urgency when urinating"
],
},
{
"symptom_id": 8,
"name": "Menstrual",
"color": "#f413d6",
"symptom_value_ids": [
45
],
"symptom_value_names": [
"Light flow"
],
}
],
},
{
"patient_symptom_id": 12,
"date": "2023-01-26",
"datetime": "2023-01-26T11:38:11.000Z",
"symptom_list": [
{
"symptom_id": 1,
"name": "Color",
"color": "#f4f50b",
"symptom_value_ids": [
3,
4
],
"symptom_value_names": [
"Clear",
"Cloudy"
],
},
{
"symptom_id": 2,
"name": "Energy",
"color": "#7ae38b",
"symptom_value_ids": [
27,
42
],
"symptom_value_names": [
"Anxious",
"High"
],
},
{
"symptom_id": 4,
"name": "Stool",
"color": "#f2b706",
"symptom_value_ids": [
14,
29
],
"symptom_value_names": [
"Constipation (1-2)",
"Unable to Pass"
],
},
{
"symptom_id": 6,
"name": "General Symptoms",
"color": "#951af6",
"symptom_value_ids": [
31,
34
],
"symptom_value_names": [
"Fever",
"Headache"
],
},
{
"symptom_id": 7,
"name": "Urinary Symptoms",
"color": "#23a7f1",
"symptom_value_ids": [
37
],
"symptom_value_names": [
"Urgency when urinating"
],
},
{
"symptom_id": 8,
"name": "Menstrual",
"color": "#f413d6",
"symptom_value_ids": [
45
],
"symptom_value_names": [
"Light flow"
],
}
],
},
{
"patient_symptom_id": 13,
"date": "2023-01-26",
"datetime": "2023-01-26T11:38:11.000Z",
"symptom_list": [
{
"symptom_id": 1,
"name": "Color",
"color": "#f4f50b",
"symptom_value_ids": [
3,
4
],
"symptom_value_names": [
"Clear",
"Cloudy"
],
},
{
"symptom_id": 2,
"name": "Energy",
"color": "#7ae38b",
"symptom_value_ids": [
27,
42
],
"symptom_value_names": [
"Anxious",
"High"
],
},
{
"symptom_id": 4,
"name": "Stool",
"color": "#f2b706",
"symptom_value_ids": [
14,
29
],
"symptom_value_names": [
"Constipation (1-2)",
"Unable to Pass"
],
},
{
"symptom_id": 6,
"name": "General Symptoms",
"color": "#951af6",
"symptom_value_ids": [
31,
34
],
"symptom_value_names": [
"Fever",
"Headache"
],
},
{
"symptom_id": 7,
"name": "Urinary Symptoms",
"color": "#23a7f1",
"symptom_value_ids": [
37
],
"symptom_value_names": [
"Urgency when urinating"
],
},
{
"symptom_id": 8,
"name": "Menstrual",
"color": "#f413d6",
"symptom_value_ids": [
45
],
"symptom_value_names": [
"Light flow"
],
}
],
},
{
"patient_symptom_id": 80,
"date": "2023-01-26",
"datetime": "2023-01-26T11:11:11.000Z",
"symptom_list": [
{
"symptom_id": 1,
"name": "Color",
"color": "#f4f50b",
"symptom_value_ids": [
6
],
"symptom_value_names": [
"Low"
],
},
{
"symptom_id": 2,
"name": "Energy",
"color": "#7ae38b",
"symptom_value_ids": [
27,
42
],
"symptom_value_names": [
"Anxious",
"High"
],
},
{
"symptom_id": 4,
"name": "Stool",
"color": "#f2b706",
"symptom_value_ids": [
14,
29
],
"symptom_value_names": [
"Constipation (1-2)",
"Unable to Pass"
],
},
{
"symptom_id": 6,
"name": "General Symptoms",
"color": "#951af6",
"symptom_value_ids": [
31,
34
],
"symptom_value_names": [
"Fever",
"Headache"
],
},
{
"symptom_id": 7,
"name": "Urinary Symptoms",
"color": "#23a7f1",
"symptom_value_ids": [
37
],
"symptom_value_names": [
"Urgency when urinating"
],
},
{
"symptom_id": 8,
"name": "Menstrual",
"color": "#f413d6",
"symptom_value_ids": [
45
],
"symptom_value_names": [
"Light flow"
],
}
],
}
],
"2023-01-29": [
{
"patient_symptom_id": 16,
"date": "2023-01-29",
"datetime": "2023-01-29T11:38:11.000Z",
"symptom_list": [
{
"symptom_id": 1,
"name": "Color",
"color": "#f4f50b",
"symptom_value_ids": [
3,
4
],
"symptom_value_names": [
"Clear",
"Cloudy"
],
},
{
"symptom_id": 2,
"name": "Energy",
"color": "#7ae38b",
"symptom_value_ids": [
27,
42
],
"symptom_value_names": [
"Anxious",
"High"
],
},
{
"symptom_id": 4,
"name": "Stool",
"color": "#f2b706",
"symptom_value_ids": [
14,
29
],
"symptom_value_names": [
"Constipation (1-2)",
"Unable to Pass"
],
},
{
"symptom_id": 6,
"name": "General Symptoms",
"color": "#951af6",
"symptom_value_ids": [
31,
34
],
"symptom_value_names": [
"Fever",
"Headache"
],
},
{
"symptom_id": 7,
"name": "Urinary Symptoms",
"color": "#23a7f1",
"symptom_value_ids": [
37
],
"symptom_value_names": [
"Urgency when urinating"
],
},
{
"symptom_id": 8,
"name": "Menstrual",
"color": "#f413d6",
"symptom_value_ids": [
45
],
"symptom_value_names": [
"Light flow"
],
}
],
},
{
"patient_symptom_id": 17,
"date": "2023-01-29",
"datetime": "2023-01-29T11:38:11.000Z",
"symptom_list": [
{
"symptom_id": 1,
"name": "Color",
"color": "#f4f50b",
"symptom_value_ids": [
1,
6
],
"symptom_value_names": [
"Low",
"Yellow"
],
},
{
"symptom_id": 2,
"name": "Energy",
"color": "#7ae38b",
"symptom_value_ids": [
27,
42
],
"symptom_value_names": [
"Anxious",
"High"
],
},
{
"symptom_id": 4,
"name": "Stool",
"color": "#f2b706",
"symptom_value_ids": [
14,
29
],
"symptom_value_names": [
"Constipation (1-2)",
"Unable to Pass"
],
},
{
"symptom_id": 6,
"name": "General Symptoms",
"color": "#951af6",
"symptom_value_ids": [
31,
34
],
"symptom_value_names": [
"Fever",
"Headache"
],
},
{
"symptom_id": 7,
"name": "Urinary Symptoms",
"color": "#23a7f1",
"symptom_value_ids": [
37
],
"symptom_value_names": [
"Urgency when urinating"
],
},
{
"symptom_id": 8,
"name": "Menstrual",
"color": "#f413d6",
"symptom_value_ids": [
45
],
"symptom_value_names": [
"Light flow"
],
}
],
},
{
"patient_symptom_id": 19,
"date": "2023-01-29",
"datetime": "2023-01-29T11:38:11.000Z",
"symptom_list": [
{
"symptom_id": 1,
"name": "Color",
"color": "#f4f50b",
"symptom_value_ids": [
1,
6
],
"symptom_value_names": [
"Low",
"Yellow"
],
},
{
"symptom_id": 2,
"name": "Energy",
"color": "#7ae38b",
"symptom_value_ids": [
27,
42
],
"symptom_value_names": [
"Anxious",
"High"
],
},
{
"symptom_id": 4,
"name": "Stool",
"color": "#f2b706",
"symptom_value_ids": [
14,
29
],
"symptom_value_names": [
"Constipation (1-2)",
"Unable to Pass"
],
},
{
"symptom_id": 6,
"name": "General Symptoms",
"color": "#951af6",
"symptom_value_ids": [
31,
34
],
"symptom_value_names": [
"Fever",
"Headache"
],
},
{
"symptom_id": 7,
"name": "Urinary Symptoms",
"color": "#23a7f1",
"symptom_value_ids": [
37
],
"symptom_value_names": [
"Urgency when urinating"
],
},
{
"symptom_id": 8,
"name": "Menstrual",
"color": "#f413d6",
"symptom_value_ids": [
45
],
"symptom_value_names": [
"Light flow"
],
}
],
},
{
"patient_symptom_id": 20,
"date": "2023-01-29",
"datetime": "2023-01-29T11:38:11.000Z",
"symptom_list": [
{
"symptom_id": 1,
"name": "Color",
"color": "#f4f50b",
"symptom_value_ids": [
1,
6
],
"symptom_value_names": [
"Low",
"Yellow"
],
},
{
"symptom_id": 2,
"name": "Energy",
"color": "#7ae38b",
"symptom_value_ids": [
27,
42
],
"symptom_value_names": [
"Anxious",
"High"
],
},
{
"symptom_id": 4,
"name": "Stool",
"color": "#f2b706",
"symptom_value_ids": [
14,
29
],
"symptom_value_names": [
"Constipation (1-2)",
"Unable to Pass"
],
},
{
"symptom_id": 6,
"name": "General Symptoms",
"color": "#951af6",
"symptom_value_ids": [
31,
34
],
"symptom_value_names": [
"Fever",
"Headache"
],
},
{
"symptom_id": 7,
"name": "Urinary Symptoms",
"color": "#23a7f1",
"symptom_value_ids": [
37
],
"symptom_value_names": [
"Urgency when urinating"
],
},
{
"symptom_id": 8,
"name": "Menstrual",
"color": "#f413d6",
"symptom_value_ids": [
45
],
"symptom_value_names": [
"Light flow"
],
}
],
}
Inside the list, everything seems like a dictionary whose key is the date value is a List. When I write data class, I write in this way now:
data class Lists(
val `2023-01-02`: List<SymptomList>,
val `2023-01-03`: List<SymptomList>,
val `2023-01-04`: List<SymptomList>,
val `2023-01-07`: List<SymptomList>,
val `2023-01-10`: List<SymptomList>,
val `2023-01-11`: List<SymptomList>,
val `2023-01-12`: List<SymptomList>,
val `2023-01-13`: List<SymptomList>,
val `2023-01-20`: List<SymptomList>,
val `2023-01-22`: List<SymptomList>,
val `2023-01-24`: List<SymptomList>,
val `2023-01-25`: List<SymptomList>,
val `2023-01-26`: List<SymptomList>,
val `2023-01-27`: List<SymptomList>,
val `2023-01-28`: List<SymptomList>,
val `2023-01-29`: List<SymptomList>,
val `2023-01-30`: List<SymptomList>,
val `2023-01-31`: List<SymptomList>
)
I know it is not the good way, because there may be more item in dictionary, so what should I do to write the data class? I tried add one more layer named dictionary but it didn't work.
data class Dictionary(
val dictionary: MutableMap<DateKey, List<SymptomList>>
)

Firstly, please use online resource like JSON Formatter & Validator to validate and beautify your JSON String. It is not readable and also invalid.
Back to your question, yes, you should use MutableMap to store your list of Objects instead of creating tons of variables in your data class.
Consider the following example that should have similar structure with your JSON String:
{
"data":{
"2022-12-20":[
{
"id":1,
"name":"A"
},
{
"id":2,
"name":"B"
}
],
"2022-12-21":[
{
"id":3,
"name":"C"
},
{
"id":4,
"name":"D"
}
]
}
}
You should have the following data classes:
data class DataList(val data: MutableMap<String, List<SubData>>)
data class SubData(val id: Int, val name: String)
And you can use Gson library to convert your JSON String into your Object:
val dataList = Gson().fromJson(rawJson, DataList::class.java)
dataList.data.forEach { (key, value) ->
println("Key: $key")
println("Value: $value")
}
Output:
Key: 2022-12-20
Value: [SubData(id=1, name=A), SubData(id=2, name=B)]
Key: 2022-12-21
Value: [SubData(id=3, name=C), SubData(id=4, name=D)]

Related

How to remove object from double nested array in mongoose?

current value
[
{
"_id": "63d8a8fc51222aac42970932",
"vendorCode": "roshan",
"items": [
{
"itemsId": "63d8a90451222aac42970934",
"totalStock": 50,
"reservedStock": 30,
"orderedStock": 0,
"orderDetails": [],
"reservedDetails": [
{
"userId": "sharma",
"vendorCode": "roshan",
"itemId": "63d8a90451222aac42970934",
"count": 30,
"reservedStatus": 0,
"createdby": "sharma",
"updatedby": "sharma",
"createdOn": "2023-01-31T06:03:25.672Z",
"updatedon": "2023-01-31T06:03:25.672Z",
"_id": "63d8b0285fae452cfa14e569",
"__v": 0
}
],
"_id": "63d8a90451222aac42970936"
}
],
"createdon": "2023-01-31T05:35:16.609Z",
"updatedon": "2023-01-31T05:35:16.609Z",
"createdby": "roshan",
"updatedby": "roshan",
"__v": 0
}
]
expected output:::your text
[
{
"_id": "63d8a8fc51222aac42970932",
"vendorCode":"roshan",
"items": [
{
"itemsId": "63d8a90451222aac42970934",
"totalStock": 50,
"reservedStock": 30,
"orderedStock": 0,
"orderDetails": [],
"reservedDetails": [],
"_id": "63d8a90451222aac42970936"
}
],
"createdon": "2023-01-31T05:35:16.609Z",
"updatedon": "2023-01-31T05:35:16.609Z",
"createdby": "roshan",
"updatedby": "roshan",
"__v": 0
}
]

how to find and update objects in an array in mongoDb arrays?

This is the Mongo db collection
{
"_id": "62035cd8a76fcc0e09f46c22",
"particulars":[
{
"serialNo": 1,
"item": "desk",
"link": "http//",
"images": "http//",
"quantity": 13,
"unitPrice": 100,
"amount": 1300
},
{
"serialNo": 2,
"item": "desk",
"link": "http//",
"images": "http//",
"quantity": 13,
"unitPrice": 100,
"amount": 1300
},
{
"serialNo": 3,
"item": "desk",
"link": "http//",
"images": "http//",
"quantity": 13,
"unitPrice": 100,
"amount": 1300
}
]
}
the array from frontend is this
procurement_details:[
{
"serialNo": 1,
"item": "ac",
"link": "http//",
"images": "http//",
"quantity": 1,
"unitPrice": 100,
"amount": 100
},
{
"serialNo": 2,
"item": "fan",
"link": "http//",
"images": "http//",
"quantity": 1,
"unitPrice": 100,
"amount": 100
}
]
output i want . Need to replace the matching serialNo objects in particular with the objects in particulars
particulars:[
{
"serialNo": 1,
"item": "ac",
"link": "http//",
"images": "http//",
"quantity": 1,
"unitPrice": 100,
"amount": 100
},
{
"serialNo": 2,
"item": "fan",
"link": "http//",
"images": "http//",
"quantity": 1,
"unitPrice": 100,
"amount": 100
},
{
"serialNo": 3,
"item": "desk",
"link": "http//",
"images": "http//",
"quantity": 13,
"unitPrice": 100,
"amount": 1300
}
]
i need to replace the objects in particulars with the new objects from procurement_details with respect to the serialNo.so can anyone help me to figure this out and im using nodejs and mongoClient.
var bulk=await tickets.initializeOrderedBulkOp()
for(procurement_detail of procurement_details){
const resultt=await tickets.find({"_id":mongodb.ObjectId(ticketId),"particulars.serialNo":procurement_detail.serialNo},{projection:{"particulars.$":1,_id:0}}).toArray()
console.log("resultts",resultt);
await bulk.find({"_id":mongodb.ObjectId(ticketId),"particulars.serialNo":procurement_detail.serialNo}).update({
$set:{"particulars.$":procurement_detail},
$push:{ticketHistory:{$each:resultt[0].particulars}}
})
}
await bulk.execute()

How to loop through a collection and return the items correspond to query params

I'm working on some queries with nodeJS to retrieve custom data from JSON file, I want to return the most played games from this set (The most played games are the games that have the highest total playtime between users)
here is the JSON :
{
"data": [
{
"userId": 8,
"game": "League of legends",
"playTime": 500,
"genre": "MOBA",
"platforms": [
"PC"
]
},
{
"userId": 7,
"game": "World of warcraft",
"playTime": 1500,
"genre": "MMORPG",
"platforms": [
"PC"
]
},
{
"userId": 88,
"game": "Dark Souls",
"playTime": 109,
"genre": "Action RPG",
"platforms": [
"PS3",
"Xbox 360",
"PC",
"PS4",
"Xbox One",
"Nintendo Switch"
]
},
{
"userId": 88,
"game": "The Witcher 3: Wild Hunt",
"playTime": 9,
"genre": "RPG",
"platforms": [
"PC",
"PS4",
"Xbox One",
"Nintendo Switch"
]
},
{
"userId": 1,
"game": "The last of us 2",
"playTime": 100,
"genre": "FPS",
"platforms": [
"PS4",
"PC"
]
},
{
"userId": 7,
"game": "Hitman 3",
"playTime": 60,
"genre": "Stealth",
"platforms": [
"PS4",
"PS5",
"Xbox One",
"Nintendo Switch",
"PC"
]
},
{
"userId": 99,
"game": "Minecraft",
"playTime": 1002,
"genre": "Sandbox",
"platforms": [
"PC"
]
},
{
"userId": 7,
"game": "Hearthstone",
"playTime": 1000,
"genre": "Card Game",
"platforms": [
"PC"
]
},
{
"userId": 7,
"game": "FIFA",
"playTime": 2000,
"genre": "Sport",
"platforms": [
"PC",
"PS4",
"Xbox One"
]
},
{
"userId": 2,
"game": "The Witcher 3: Wild Hunt",
"playTime": 78,
"genre": "RPG",
"platforms": [
"PC",
"PS4",
"Xbox One",
"Nintendo Switch"
]
},
{
"userId": 47,
"game": "League of legends",
"playTime": 850,
"genre": "MOBA",
"platforms": [
"PC"
]
},
{
"userId": 2,
"game": "Among Us",
"playTime": 5000,
"genre": "Multiplayer",
"platforms": [
"PC",
"Android"
]
},
{
"userId": 2,
"game": "Valorant",
"playTime": 2000,
"genre": "FPS",
"platforms": [
"PC"
]
},
{
"userId": 9,
"game": "Valorant",
"playTime": 80,
"genre": "FPS",
"platforms": [
"PC"
]
},
{
"userId": 9,
"game": "Dark Souls",
"playTime": 109,
"genre": "RPG",
"platforms": [
"PS3",
"Xbox 360",
"PC",
"PS4",
"Xbox One",
"Nintendo Switch"
]
},
{
"userId": 9,
"game": "The Witcher 3: Wild Hunt",
"playTime": 900,
"genre": "RPG",
"platforms": [
"PC",
"PS4",
"Xbox One",
"Nintendo Switch"
]
},
{
"userId": 24,
"game": "League of legends",
"playTime": 300,
"genre": "MOBA",
"platforms": [
"PC"
]
},
{
"userId": 24,
"game": "World of warcraft",
"playTime": 800,
"genre": "MMORPG",
"platforms": [
"PC"
]
},
{
"userId": 54,
"game": "Minecraft",
"playTime": 231,
"genre": "Sandbox",
"platforms": [
"PC"
]
},
{
"userId": 7,
"game": "Minecraft",
"playTime": 777,
"genre": "Sandbox",
"platforms": [
"PC"
]
},
{
"userId": 7,
"game": "Hitman 3",
"playTime": 90,
"genre": "Stealth",
"platforms": [
"PS4",
"PS5",
"Xbox One",
"Nintendo Switch",
"PC"
]
}
]
}
I already finish this by writing this function :
const selectTopByPlaytime = async (options) => {
return _.chain(games)
.groupBy('game')
.map((value, key) => {
return {
'game': key,
'playTime': _.reduce(value, function (memo, i) { return memo + i.playTime; }, 0),
'genre': value[0].genre,
'platforms': value[0].platforms,
};
})
.sortBy((i) => { return -i.playTime; })
.first(5).value()
}
I'm trying to modify this function by adding queryparams filter to return the top games by playtime that are available with the params sent.
example : BASE_URL/select_top_by_playtime?genre=FPS would return the top games by playtime that that are classified under the FPS genre.
any help please !
First get query params and then filter based on them and accordon to their type.
const selectTopByPlaytime = async (options) => {
let queryParams = new URLSearchParams(window.location.search);
return _.chain(games)
.groupBy("game")
.filter((item) => {
for ([key, value] of queryParams.entries()) {
switch (key) {
case "genre":
if (item.genre != value) return false;
case "platform":
if (item.platforms.indexOf(value) == -1) return false;
}
}
return true;
})
.map((value, key) => {
return {
game: key,
playTime: _.reduce(
value,
function (memo, i) {
return memo + i.playTime;
},
0
),
genre: value[0].genre,
platforms: value[0].platforms,
};
})
.sortBy((i) => {
return -i.playTime;
})
.first(5)
.value();
};

How to I return a json object along with totals in mongoose?

I have a database of exercises in a workout tracker, and when I do a find(), the result is this:
[
{
"_id": "5e9dacbb6512969974bd5b2d",
"day": "2020-04-10T14:07:55.905Z",
"exercises": [
{
"type": "resistance",
"name": "Bicep Curl",
"duration": 20,
"weight": 100,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b2e",
"day": "2020-04-11T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Lateral Pull",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b2f",
"day": "2020-04-12T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Push Press",
"duration": 25,
"weight": 185,
"reps": 8,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b30",
"day": "2020-04-13T14:07:55.916Z",
"exercises": [
{
"type": "cardio",
"name": "Running",
"duration": 25,
"distance": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b31",
"day": "2020-04-14T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench Press",
"duration": 20,
"weight": 285,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b32",
"day": "2020-04-15T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench Press",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b33",
"day": "2020-04-16T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Quad Press",
"duration": 30,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b34",
"day": "2020-04-17T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench Press",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b35",
"day": "2020-04-18T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Military Press",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b36",
"day": "2020-04-19T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench",
"duration": 30,
"distance": 2
}
]
}
]
Then I need to get total sums of statistics from each exercise, so I used mongoose aggregate to give me this data:
[
{
"_id": null,
"totalDuration": 230,
"totalWeight": 2070,
"totalSets": 32,
"totalReps": 78,
"totalDistance": 6
}
]
I want to combine these two results in one GET request, ideally doing something similar to a push where I just push the totals at the end of the first JSON object. How do I achieve this?
Something like this:
function mergeResults(resultFromFindQuery, totalSums){
var allData = {};
allData['mongoFindresult'] = resultFromFindQuery;
allData['totalSums'] = totalSums;
return allData;
}
Then use the returned value to what you need to do. Now you have both of them in the same variable.

Json array iteration in futon

I am using CocuhDB I have this document structure
{"master": false,
"type": "a",
"company": 9,
"products": [
{
"unit": {
"id": 9,
"isMp": false,
"_id": "40109daadce8d3671a1aeca35bbb1438"
},
"article": {
"id": 1132,
"id_provider": 0,
"isMp": false,
"id_unit": 9,
"weight": 0,
"_id": "eb1718f96375b01af5552cf3f4c2d86b",
"code": "ME021",
"type_article": 2
},
"order": 1,
"warehouse_company": {
"id": 9,
"_id": "ebce7557ff95203ac5d03f294381d6ed"
},
"article_code": "ME021",
"provider": {
"id": 2313,
"isMp": false
},
"qty": 20.5,
"warehouse": {
"id": 18,
"isMp": false
}
}, {
"unit": {
"id": 9,
"isMp": false,
"_id": "40109daadce8d3671a1aeca35bbb1438"
},
"article": {
"id": 1132,
"id_provider": 0,
"isMp": false,
"id_unit": 9,
"weight": 0,
"_id": "eb1718f96375b01af5552cf3f4c2d86b",
"code": "ME099",
"type_article": 2
},
"order": 1,
"warehouse_company": {
"id": 9,
"isMp": false
},
"article_code": "ME021",
"provider": {
"id": 2313,
"isMp": false,
"_id": "657abbdfb4c713a9baa1ffd7329319c0"
},
"qty": 20.5,
"warehouse": {
"id": 18,
"isMp": false,
"_id": "9f70abb04a0243a1cd997b6430fb2207"
}
}
]
}
products field coulb be one or ten.
I need to find all the documents where
doc.products[n].warehouse.id == 18
But I dont know how to do it using Futon.
I am trying something like :
function(doc) {
var product, value;
if (doc.type != master && doc.type == "a" && doc.company == 9) {
for (product in doc.products) {
value= doc.prices[producto];
emit(value, doc );
}
}
}
But It does not work .
What I am doing wrong?
You can use view like this. This is an example. Not a solution
function(doc) {
var product, value;
if (doc.type != "master" && doc.type == "a" && doc.company == 9 && doc.products && Array.isArray(doc.products)) {
doc.products.forEach(function(product) {
Object.keys(product).forEach(function(key) {
emit([key, product[key].id]);
});
});
}
}
Will create output like
{
"total_rows": 16,
"offset": 0,
"rows": [
{
"key": [
"article",
1132
],
"id": "check",
"value": null
},
{
"key": [
"article",
1132
],
"id": "check",
"value": null
},
{
"key": [
"article_code",
null
],
"id": "check",
"value": null
},
{
"key": [
"article_code",
null
],
"id": "check",
"value": null
},
{
"key": [
"order",
null
],
"id": "check",
"value": null
},
{
"key": [
"order",
null
],
"id": "check",
"value": null
},
{
"key": [
"provider",
2313
],
"id": "check",
"value": null
},
{
"key": [
"provider",
2313
],
"id": "check",
"value": null
},
{
"key": [
"qty",
null
],
"id": "check",
"value": null
},
{
"key": [
"qty",
null
],
"id": "check",
"value": null
},
{
"key": [
"unit",
9
],
"id": "check",
"value": null
},
{
"key": [
"unit",
9
],
"id": "check",
"value": null
},
{
"key": [
"warehouse",
18
],
"id": "check",
"value": null
},
{
"key": [
"warehouse",
18
],
"id": "check",
"value": null
},
{
"key": [
"warehouse_company",
9
],
"id": "check",
"value": null
},
{
"key": [
"warehouse_company",
9
],
"id": "check",
"value": null
}
]
}
You can search for " warehouse" with id 18 like
using key=["warehouse",18]

Resources