MongoDB and rest api
Page 1 of 1
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: 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? Smile

Any thoughts or pointers?


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Bob Barnsen




Posts: 31974
Location: Germoney
PostPosted: Sun, 29th Nov 2015 22:17    Post subject:
"Mongo"DB
Huehuehue
Sorry and Bye
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: 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);
});


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Bob Barnsen




Posts: 31974
Location: Germoney
PostPosted: Sun, 29th Nov 2015 22:21    Post subject:
Poker Face Me Gusta


Enthoo Evolv ATX TG // Asus Prime x370 // Ryzen 1700 // Gainward GTX 1080 // 16GB DDR4-3200
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Mon, 30th Nov 2015 14:56    Post subject:
bmup


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: 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?


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Areius




Posts: 14777

PostPosted: Tue, 1st Dec 2015 15:19    Post subject:
Do you want to query the days outside of the user? If so, make it a separate collection, otherwise I would embed it. Difference in performance isn't very noticable. How much data do you want to store?

About the api, what about:
/api/1/days/list/:userid
/api/1/days/:day/:userid
or with ?id=<userid> (which is what I usually use as you can more easily stack other parameters.


PC: Yes. Console: No.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: 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.


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Areius




Posts: 14777

PostPosted: Wed, 2nd Dec 2015 13:41    Post subject:
That's not a lot of data, I would embed it then.

I don't really understand what you want to do with these days. Do you always want to list all the days?


PC: Yes. Console: No.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: 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.


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Areius




Posts: 14777

PostPosted: Thu, 3rd Dec 2015 13:29    Post subject:
So if I understand it correctly, you do not want to request all days data at once but instead want to query a specific date? But then you do need a way to determine which dates are available for a user (hence the suggestion I've mentioned before).


PC: Yes. Console: No.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: 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 :/


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Areius




Posts: 14777

PostPosted: Thu, 3rd Dec 2015 15:26    Post subject:
Yeah, but you don't really need an id if you embed the data. Plus, a date is already unique (I assume at least that you can't add two identical dates) so you don't need a separate id.


PC: Yes. Console: No.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: 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.


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Areius




Posts: 14777

PostPosted: Thu, 3rd Dec 2015 17:19    Post subject:
No, you can:
- Query one day instead of all the days (for embedded collections).
- Get specific fields if you do not want to include all fields.

With the amount of data you have here there is no reason to go with a separate collection.


PC: Yes. Console: No.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Thu, 3rd Dec 2015 19:46    Post subject:
Right, I could just exclude the days I guess Smile Makes sense Very Happy Thanks!


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Areius




Posts: 14777

PostPosted: Fri, 4th Dec 2015 09:13    Post subject:
But even so, I do not think you need to exclude the days. I've had way larger embedded collections return pretty much instantly.


PC: Yes. Console: No.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: 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
Areius




Posts: 14777

PostPosted: Fri, 4th Dec 2015 10:26    Post subject:
I meant the data from the database. Rest calls should limit data as much as possible of course Wink


PC: Yes. Console: No.
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group