Reputation: 1771
I am wondering what would be the best way to store data, for example, orders in a restaurant, in my mongoDB.
So, first i would need to have an "Orders" collection.
there would also be a need to do a lot of reading for syncing data between waiters, so i dont want to be storing orders that are completed (payed) and orders that are still open in the same list.
So i came up with a "Orders" collection that has 2 array fields: "current" and "history". In current i would store all orders that are still not payed for (need to be synced through all waiters ) and in "history" i would store everything that is closed and needs to be accessed only by managers or someone that wants to review the data.
That way i can minimise the accessing time and sending amount of data.
Would this be a correct, best practice way of doing this?
Or should i store everything in a single list, and then do query's that do sorting by time or similar, and limit the amount of documents i am sending back?
EDIT:
in this collection i wish to save orders for multiple restaurants. so i could use one document in this collection for each restaurant and embed orders in there, or put all orders in this collection on the same level, and then each order would have a restaurantId
Upvotes: 0
Views: 508
Reputation: 43884
Since the orders collection represents a single restaurant would it not instead contain a record of all orders, with each document looking like:
{
_id:{}
waiter: 'Sammaye',
table: 9,
items: [
{id:9,qty:1,cooked:false}
],
billed: false
}
And all waiters who need to know about orders waiting to go out will just get all order documents from this collection which have billed
false
.
This should be fine enough if you place an index on billed
.
Using the other method you said could cause problems, for example it is one document in a collection storing all orders. I could imagine in time that document could get quite large.
Using in memory operators ($push
,$pull
etc) could make operations on the document slow.
It could also create fragmentation within the database since these are unbound arrays consistently growing over time, this too will lower performance.
Your method also would not create any advantages over storing each order as a record except that you can get all orders pending in one go, however, configuring the batch size, you could probably get quite close to that when storing all orders as separate documents.
Upvotes: 2