How to Do Multi-Object Update in MongoDB From Server Code

The Database runs on MongoDB database. The following screen shot shows a sample collection (table) with employees in the database.

Screen Shot 2016-04-19 at 5.01.31 PM.png
Custom database collection

You want to update two or more objects in a single request. That’s very simple to do from Server Code. The following server script does multi-object update, it just one line of code:

var result = Collection.multiUpdateObject("367e9e77-cc1f-4c1f...",
    "People", '{"title":"Engineer"}', {"title":"Senior Engineer"});
  • The first argument is the database API key.
  • People is the collection name inside the database.
  • With {“title”:”Engineer”} you are searching for all records where the title is set to “Engineer” and update those objects to a title of “Senior Engineer“.
  • Collection is part of Server Code API that integrates with the database.

That’s it.

After you run the script, the database collection looks like this:

Screen Shot 2016-04-20 at 11.35.22 AM
Collection after the update Server Code provides a very nice integration with the Database that allows you to run and execute advanced app logic.

Learn more about Database and Server Code from many short videos on our YouTube channel.

Published by

2 responses to “How to Do Multi-Object Update in MongoDB From Server Code”


    Script UpdateY: :1:68 Expected , but found ; var result = Collection.multiUpdateObject(“585f4461e4b02b2c07e11fec”;, ^

    “;” and “,” together at the first sentence is that OK?

    1. I think I had a typo in the code. You don’t need the “;” after the database API key.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.