Page 1 of 1 |
|
Posted: Sun, 29th Nov 2015 22:11 Post subject: MongoDB and rest api |
|
 |
Hey guys... I'm working on a small app and I was using a mean stack for it. So I have a collection of users with basic information about that user. But I also want to track daily informations about this user... f.e. weight or so.
So I thought about creating a second collection called days which would have the userid basically as foreign key so that I can query through all days that are stored for a specific user.
But because days always belong to a single user, shouldn't I just embed them in an array inside the user document? Would this still deliver a good performance? But wouldn't I, with this second approach, at the same time retrive all days of a user when I just simply query for the user himself (something I wouldn't want!)?
--
Second question: Designing the right API...
I guess querying for a user is fairly easy
/api/1/users/:userid
but what about all days for said user?
/api/1/days/:userid? <- wouldn't make sense imho because if I write my api like that I'd rather have an identifier for a day to return a single day and not all days for said user, so a better route for this would be
/api/1/users/:userid/days I guess, right?
Any thoughts or pointers?
|
|
Back to top |
|
 |
|
Posted: Sun, 29th Nov 2015 22:17 Post subject: |
|
 |
"Mongo"DB
Huehuehue
Sorry and Bye
|
|
Back to top |
|
 |
|
Posted: Sun, 29th Nov 2015 22:20 Post subject: |
|
 |
Code: | var mongos = [{name:"Bob Barnsen"}];
mongos.forEach(function(mongo){
console.log("Fuck %s in the butt", mongo);
}); |
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
|
Posted: Mon, 30th Nov 2015 14:56 Post subject: |
|
 |
|
|
Back to top |
|
 |
|
Posted: Mon, 30th Nov 2015 19:41 Post subject: |
|
 |
OK forum, feel free to be my rubber duck
So I guess the best would be to have two collectios
one for users and one for days. And as every day would be bound to a specific user, I'd also store and create an index on the userid field in the days collection.
I initially thought of creating an array in the user and store each day id in there but that would've make things very complicated and fragile just to improve the performance, so I decided against it. Any best practices for nosql document based db's?
|
|
Back to top |
|
 |
|
Posted: Tue, 1st Dec 2015 15:19 Post subject: ***** |
|
 |
*****
Last edited by Areius on Fri, 19th Sep 2025 16:22; edited 1 time in total
|
|
Back to top |
|
 |
|
Posted: Tue, 1st Dec 2015 16:03 Post subject: |
|
 |
Well lets assume that you'll have like 1000 days for each user after 3 years of usage. Each day is a complex object itself with various key value pairs (some of which are arrays or objects themselves).
Regarding the list command: That doesn't look very clean. I would expect that a get on the days itself would return all days but I'll never implement this call (because duhhh) and I'm not quite sure if days/:day/:userid is so good, because I'll never implement days/:day either because I don't want all users' days for a specific date.
The hierarchy is quite fixed. Days that are not assigned to users should not exist. So I don't see a good reason why I should be able to query for days with the API at least.
|
|
Back to top |
|
 |
|
Posted: Wed, 2nd Dec 2015 13:41 Post subject: ***** |
|
 |
*****
Last edited by Areius on Fri, 19th Sep 2025 16:22; edited 1 time in total
|
|
Back to top |
|
 |
|
Posted: Wed, 2nd Dec 2015 15:09 Post subject: |
|
 |
1000 days at lets say 1kb per day = 1MB. I wouldn't want to transfer 1MB of data, every time a simple request to get a user is submitted!
A single day could hold information like the consumed food, measurements of a person and much more.
|
|
Back to top |
|
 |
|
Posted: Thu, 3rd Dec 2015 13:29 Post subject: ***** |
|
 |
*****
Last edited by Areius on Fri, 19th Sep 2025 16:22; edited 1 time in total
|
|
Back to top |
|
 |
|
Posted: Thu, 3rd Dec 2015 13:46 Post subject: |
|
 |
Well if I index each days userid and date I can quickly go through all days to get the days that belong to the user or as previously said, I could just store all day id's that belong to a user in an array on the user. But then I'd need to maintain it, which would add complexity :/
|
|
Back to top |
|
 |
|
Posted: Thu, 3rd Dec 2015 15:26 Post subject: ***** |
|
 |
*****
Last edited by Areius on Fri, 19th Sep 2025 16:22; edited 1 time in total
|
|
Back to top |
|
 |
|
Posted: Thu, 3rd Dec 2015 15:38 Post subject: |
|
 |
But if I embed the data, every time I do a request to just get the user I will also get all dates and as I said before, that data can easily be up to 1MB while I'll mostly only require to load one day instead of hundreds.
|
|
Back to top |
|
 |
|
Posted: Thu, 3rd Dec 2015 17:19 Post subject: ***** |
|
 |
*****
Last edited by Areius on Fri, 19th Sep 2025 16:22; edited 1 time in total
|
|
Back to top |
|
 |
|
Posted: Thu, 3rd Dec 2015 19:46 Post subject: |
|
 |
Right, I could just exclude the days I guess Makes sense Thanks!
|
|
Back to top |
|
 |
|
Posted: Fri, 4th Dec 2015 09:13 Post subject: ***** |
|
 |
*****
Last edited by Areius on Fri, 19th Sep 2025 16:22; edited 1 time in total
|
|
Back to top |
|
 |
|
Posted: Fri, 4th Dec 2015 09:20 Post subject: |
|
 |
Dude. 1mb for each request is insane, especially if you are on a mobile plan xD
=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Last edited by PumpAction on Fri, 4th Dec 2015 11:49; edited 1 time in total
|
|
Back to top |
|
 |
|
Posted: Fri, 4th Dec 2015 10:26 Post subject: ***** |
|
 |
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |