Reputation: 1348
I'm currently working through a setup using MongoDB and storing visitor information. For example, let's say I have information on every visitor who visited your website within a date range. I want to store that information in the database.
Currently I am storing it in mongo like the example below. I really have 2 questions:
Schema:
Visitor
Non unique id
Date
Visitors --array[could contain up to millions of visitors[daily visitors]]
source
keyword
city
state
country
page_views
etc.......
Upvotes: 0
Views: 1134
Reputation: 43884
Is mongo the best solution for this
No. There are numerous reasons but the biggest one would be the size of the document.
After a while that document would probably grow to close to the 16MB limit. This means loading it from disk into your working set would be not only time consuming but also inefficient.
You would do better with, as you have actually gone with, one document per visitor.
If you want your documents to load faster you actually want them to be smaller that way the disk read is smaller, in turn the IO smaller and in turn less resources used.
Also since I doubt you would want ALL visitors (which would be loaded with a single document setup) then you would also have data filling RAM that you didn't need.
Storing as you are now you can selectively load visitors as needed with very little IO overhead as such I vouch for the alternative you have taken:
Yea, I ended up going with a document per visitor as we will be allowing people to run filters on the users and delete certain visitors
Upvotes: 3
Reputation: 16412
If you store IP address only in Visitors
array, and using a human readable representation with max length of 45 chars then you can fit up to 372,827 visitors in a single doc. So if you get more than this number of visitors per day you can get VC funding and rewrite your app (just kidding). One option is to store data in chunks. You can maintain a single counter in such a doc and whenever you get close to this max number you can create a new doc. This is a bit hard to maintain, so another option is to split data into hours, or minutes if you have to.
I don't think it's worth storing each visitor in a separate top level doc unless you plan to store much more information in that record and analyze what that visitor visited, etc. Depends what you want to do with this data.
For logging purposes and views counting I would go with a big array. For business analytics purposes I would go with 1 doc per visitor option.
Upvotes: 1