Navicat Blog

Sep 23, 2019 by Robert Gravelle

SQL queries often return more than one row of data from the database server. Relational databases provide cursors as a means for iterating over each row of the results set. Does that mean that MongoDB users are out of luck? As it turns out, MongoDB's db.collection.find() function returns a cursor. In MongoDB, cursors themselves provide additional functionality for processing individual rows. In today's blog, we'll learn how to work with MongoDB cursors in Navicat for MongoDB.

A Simple Iteration Example

Executing a query via the db.collection.find() function returns a pointer to the collection of documents returned, which is a cursor. The default behavior of a cursor is to allow an automatic iteration across the results of the query. However, developers can explicitly go through the items returned in the cursor object. One way to do that, is to use the forEach() cursor method.

In Navicat, it's easy to use the find() method. For instance, you can drag a pre-defined snippet into the Query Editor:

Alternatively, you can use the Find Builder. Just select the Collection or View to fetch all the documents:

From there, you can chain the forEach() directly to the results cursor. In the following example, the three documents in our collection are printed to the console. You can view the output in the Print Output tab:

A Loop of a Different Kind

Like all JavaScript objects, a cursor may be stored in a variable for later use. Other JavaScript constructs like while loops are also fully supported thanks to the cursor.hasNext() and methods. As we see in this example, hasNext() informs the loop tester whether or not there is another document to iterate over; next() returns said document.

The printjson() helper method is a convenience method that replaces print(tojson()). It basically outputs documents exactly as they are stored in the database, except that they are represented as JSON rather than BSON, the binary equivalent.

Updating Data

Recall that db.collection.find() returns a pointer to the collection of documents returned. As such, document fields are fully editable. Hence, we can invoke the forEach() method to update documents that match the specified criteria. Here's a function that updates documents where the name equals "Tom Smith":

In our case, there is only one matching document, but, in theory, there could be many.

Data Transformation

One type of data transformation is to simplify a dataset's structure in order to make it easier to consume. To do that, we can use the map() function. This example applies the split() function to the name field to break it down into its first and last constituents. Then, the reverse() and join() methods convert "Tom Smithers" to "Smithers, Tom":


In today's blog, we saw how cursors returned by the db.collection.find() method come packed with all sorts of methods to iterate over documents for the purposes of printing, modifying, deleting, or transforming their contents. Interested in taking Navicat for MongoDB for a spin? You can download a free trial here!

Navicat Blogs
Feed Entries
Blog Archives