views:

35

answers:

1

Hey guys,

So I'm building an app using PHP and MongoDB which will have a fair bit of traffic in both reads and writes.. After a couple of months there should be around 2500 reads a second and 200 writes per second (not sure how that really rates in terms of traffic compared to others, though).

I'm a bit curious on what to do when updating a collection; the documentation examples show a shell updating a specific collection field but doesn't explain what happens when any number of fields from the collection could have been changed.

For example, say I have a user collection (very simplified example):

user = {
  _id     :  MongoId(...),
  name    : 'User One',
  email   : '[email protected]',
  company : 'Company',
  ...
}

We show all editable fields in a form but the user only changes their email address.

Strictly speaking in terms of performance, would it be best to store original values in hidden inputs to compare them in PHP and then building a query specific to the update?

Or should I replace all of the editable fields anyway?

It will be a collection containing objects and arrays within each other — not the simple one shown here.

I know optimisation comes after, but I'm also looking to pick up good habits with MongoDB too.

Thanks.

+1  A: 

You'll probably be retrieving the entire object so as to display the editable form - I'm assuming here that you'll be displaying the current name, email, etc. and allowing them to edit and submit the new versions. This means you can check to see what's changed and update that specifically, rather than uploading and replacing the entire object every time.

I'd suggest using the optimized update operations: "MongoDB Updating". That way you can perform more optimized in-place operations rather than replacing the entire object every time you want to edit a specific field. You'll probably be looking mainly at $set, $addToSet, and (if you're dealing with a lot of nested objects, the $ positional operator.

From the docs:

Modifier Operations

Modifier operations are highly-efficient and useful when updating existing values; for instance, they're great for incrementing a number. [And other more complicated things]

...

a modifier update has the advantages of avoiding the latency involved in querying and returning the object. The modifier update also features operation atomicity and very little network data transfer.

If you have more specific questions about implementing a particular operation, just ask away.

nearlymonolith
Perfect, ta mate. Will use the hidden inputs to check it all then — should do it all nicely.
dynamism