I am using mongodb cloud database, mlab.com. But I have a problem when updating a document.
Apparently when using the instruction updateOne()
it only updates me once in the database. I can't do it repeatedly.
I am creating the web page in a maven repository using html and jsp.
This is the code of my .jsp
MongoClient conexion = new MongoClient(mongoURI);
MongoDatabase db = conexion.getDatabase("cp_vneonatos");
MongoCollection<Document> coleccion = db.getCollection("estancias");
coleccion.updateOne(and(eq("cedulamadre",cedulamadre), eq("horariom",horariom), eq("fechaingresom",fechaingresom)),
and (addtoset("cedulamadre",cedulamadre), addtoset("horariom",horarionuevom), addtoset("fechaingresom",fechaingresomx)));
I don't get any errors or anything like that. Only that of not being able to update a document more than once.
The function
updateOne
updates the first occurrence it finds according to the parameters that have been passed to it, but this does not mean that you cannot update a document as many times as necessary, since MongoDB functions do not maintain any state that causes any condition to occur. its execution; it is like a RESTful service, it will always execute the task that is asked of it unless an internal error occurs .In your case, it is likely that you are updating a field that you are using as a condition in subsequent updates, so the condition is not met for any document and obviously it will not perform any updates. You can tell this by setting
upsert: true
the options object:The option
upsert
forces MongoDB to create a new document if none has been found. This way you can realize that you are using the wrong filter when updating.