0

I'm working with Mongo and Esper in Java. Once I launched the esper server, I start adding documents and in one of my listeners I have to update some of those documents (one per time). The problem is that Mongo only updates those documents if they were in the database BEFORE I started the server, but not if I have just inserted them.

I'm running this code:

BasicDBObject searchQuery = new BasicDBObject();
searchQuery.append("symbol", newEvents[0].get("symbol"));
searchQuery.append("fecha", newEvents[0].get("fecha"));

BasicDBObject newDocument = new BasicDBObject();
newDocument.append("$set", new BasicDBObject().append("SMA10", ((Double) newEvents[0].get("valor")).toString()));

col.update(searchQuery, newDocument);

Does anyone have a clue what can be happening? If there is any other information you need, please just let me know.

Thank you very much.

1 Answers1

1

In MongoDB, update method can either replace the existing document or update specific fields in existing document.

If you want to update document which is not there in your DB, you've to use save method. It performs special update, called upsert operation which'll insert the document if it is not exist.

check the MongoDB Manual for Update and Save.

arthankamal
  • 6,341
  • 4
  • 36
  • 51
  • Hi Arthan, thanks for replying. I want to update a document that it IS in the DB. In fact, I have already checked that when I call the update, the document is there. I guess that the problema is not in the code I have wrote for the update because it works, but only when the document is in the DB from a previous execution. So my problem is that I want to update a document I have inserted in the current execution, and that's the moment is not working. – user2417250 May 27 '13 at 15:19