MongoDB: Adding a new field all documents in a collection

To add a new field to all documents in a MongoDB collection, first connect to the DB using the command line..

mongo myDB

Once connected, run the following command to add a field called isOpen with the value of false.

db.myCollection.update({}, {$set: {"isOpen": false}}, false, true)

Here’s the MongoDB shell syntax for update():

db.collection.update( criteria, objNew, upsert, multi )

criteria – query which selects the record to update;
objNew – updated object or $ operators (e.g., $inc) which manipulate the object
upsert – if this should be an “upsert” operation; that is, if the record(s) do not exist, insert one. Upsert only inserts a single document.
multi – indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.

More info here:

Simplest way to iterate through a Java HashMap

Lets say  you have a map that looks like this….

Map <Integer, String> map = new HashMap<Integer, String>();
map.put(0, "apple");
map.put(1, "orange");

Now the simplest way to iterate through this map is as follows…

for (Map.Entry<Integer, String> entry : map.entrySet()) {
    System.out.println ("k=" + entry.getKey() + ", v=" + entry.getValue());

The output looks like this…

k=0, v=apple
k=1, v=orange