title: How do I check if a MongoDB object exists and create/update respectively?
body: <p>I am developing a wireless network survey tool built with Java (Swing GUI) and a MongoDB data storage solution. I am new to MongoDB and hardly a Java guru so I need some help. I want to find if a network exists in my database and append heard points to the network document. If the network doesn't exist, I would like to create a document for that network and add the heard points. I have been trying to fix this for days but I just can't seem to wrap my head around the solution. Also, it would be nice if the BSSID was the unique id so I don't get any duplicate networks. My ideal data structure would look something like this:</p>
  
  <pre><code>{ 'bssid' : 'ca:fe:de:ad:be:ef', 
    'channel' : 6, 
    'heardpoints' : { 
        'point' : { 'lat' : 36.12345, 'long' : -75.234564 }, 
        'point' : { 'lat' : 36.34567, 'long' : -75.345678 }
    }
  </code></pre>
  
  <p>This is what I have tried so far. It seems to add the initial point but it does not add additional points after the first one was made.</p>
  
  <pre><code>BasicDBObject query = new BasicDBObject();
  query.put("bssid", pkt[1]);
  DBCursor cursor = coll.find(query);
  
  if (!cursor.hasNext()) {
      // Document doesnt exist so create one
      BasicDBObject document = new BasicDBObject();
      document.put("bssid", pkt[1]);
      BasicDBObject heardpoints = new BasicDBObject();
      BasicDBObject point = new BasicDBObject();
      point.put("lat", latitude);
      point.put("long", longitude);
      heardpoints.put("point", point);
      document.put("heardpoints", heardpoints);
      coll.insert(document);
  } else {
      // Document exists so we will update here
      DBObject network = cursor.next();
      BasicDBObject heardpoints = new BasicDBObject();
      BasicDBObject point = new BasicDBObject();
      point.put("lat", latitude);
      point.put("long", longitude);
      heardpoints.put("point", point);
      network.put("heardpoints", heardpoints);
      coll.save(network);
  }
  </code></pre>
  
  <p>I feel like I am way off the reservation on this one. Any support would help, thanks a lot!</p>
  
  <p><strong>UPDATE</strong>
  I am using the upsert suggestion but still having some issue. No doubt this will work for me, I am just not doing it correctly. I am still not getting any new points past the first one added.</p>
  
  <pre><code>BasicDBObject query = new BasicDBObject("bssid", pkt[1]);
  System.out.println(query);
  DBCursor cursor = coll.find(query);
  System.out.println(cursor);
  
  try {
      DBObject network = cursor.next();
      System.out.println(network);
  
  
      network.put("heardpoints", new BasicDBObject("point",
              new BasicDBObject("lat", latitude)
               .append("long", longitude)));
  
      coll.update(query, network, true, false);
   } catch (NoSuchElementException ex) {
      System.err.println("mongo error");
   } finally {
      cursor.close();
   }
  </code></pre>