0

I want to record activity log of all the users in our service, using MongoDB.

Currently, we have a structure as follows:

{
    '_id' : 'user_id',
    'activites' : [
        {time : blah blah, action : blah blah, ...},
        {time : blah blah, action : blah blah, ...},
        {time : blah blah, action : blah blah, ...},
    ]
}

but we found out that growth of embedded documents results in the slowdown of update performance.

now we're considering a change in the data schema, which is migrating embedded document to a separate collection, and using the reference model.

if we change the structure, however, there are still problems.

(1) # of references still grows. can we avoid performance issue? (2) if we stop using reference, select performance will degrade.

any good ideas?

4

1 回答 1

0

Having data stored in a single document wildly increasing is always wrong, since the document has limit on its size (16MB).

I would recommend you to just reference user_id in activites collection, like:

{
     '_id': 'activity_id',
     'user_id': 'user_id',
     'time': ...,
     'action: ...,
     ...
}

Make sure that index added for "user_id", and if you are always querying on multiple fields, like {user_id: user_id, time: {$gt: blah blah}}, create index on all the fields following the order in query, in this case: (user_id: 1, time: -1) (if you always sort time DESCENDING)

于 2013-07-29T06:13:47.597 回答