MongoDB: How to check for duplicates in a collection

This techniques to find out dupes in a MongoDB collection uses Map Reduce. The steps involved are simple:

Create a script called, say checkdupes.js add the following code to it.  The script runs on a collection called myCollection and examines the values of the field called myField, for each distinct value of myField, it inserts a document in a new collection called myDupesCollection.

m = function () {
    emit(this.myField, 1);

r = function (k, vals) {
    return Array.sum(vals);
res = db.myCollection.mapReduce(m, r, { out : "myDupesCollection" });

The script above can be run from the command line as follows

mongo myDB checkdupes.js

Now check for the dupes in the newly created collection by running the following command

db.myDupesCollection.find({value: {$gt: 1}});

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