nacho
nacho

Reputation: 651

MongoDB Array field maximum lenght

I am using mongoDB with mongoose, and I need to create a schema "advertisement" which has a field called "views" in which will be and array of

{userId: String, date: Date}.

I want to know if this is a good practice, since although I know now how much it will grow (until 1500 and then reseted) in the future I will not. I want to know if for example it would seriously affect the performance of the application if that array could be 50000 or 100000 or whatever. (It is is an unbounded array) In this cases what would be the best practice. I thought just storing an increasing number, but business decision is to know by who and when the ad was seen. I know that there is a limit only for the document (16mb), but not for the fields themselves. But my questions is more related to performance rather than document limit. Thank you!

Edit => In the end definitely it is not a good idea to let an array grow unbounded. I checked at the answer they provided first..and it is a good approach. However since I will be querying the whole document with the array property quite often I didn't want to split it. So, since I don't want to store data longer than 3 days in the array..I will pull all elements that have 3 days or more, and I hope this keeps the array clean.

Upvotes: 2

Views: 1235

Answers (1)

Artem Arkhipov
Artem Arkhipov

Reputation: 7455

I know that there is a limit only for the document (16mb), but not for the fields themselves.

Fields and their values are parts of the document, so they make direct impact on the document size.

Beside that, having such a big arrays usually is not the best approach. It decreases performance and complicates queries.

In your case, it is much better to have a separate views collection of the documents which are referencing to the advertisements by their _id.

Also, if you expect advertisement.views to be queried pretty often or, for example, you often need to show the last 10 or 20 views, then the Outlier pattern may also work for you.

Upvotes: 1

Related Questions