Ho scritto un test unitario per mostrare come si comporta il codice. Questa unit test dimostra che:
- si dovrebbe essere in grado di aggiornare più di un campo in una sola volta (cioè più campi possono essere aggiornati con la parola chiave $ set)
- updateMulti aggiornerà tutti i documenti corrispondenti
(si noti, come tutti i Java MongoDB mette alla prova questo utilizza TestNG non JUnit, ma è abbastanza simile in questo caso)
@Test
public void shouldUpdateAllMatchingFieldsUsingMultiUpdate() throws UnknownHostException {
MongoClient mongoClient = new MongoClient();
DB db = mongoClient.getDB("myDatabase");
DBCollection coll = db.getCollection("coll");
coll.drop();
//Put some test data in the database
for (int i = 0; i < 5; i++) {
DBObject value = new BasicDBObject();
value.put("fieldToQuery", "a");
value.put("ishistory", 2+i);
value.put("acknowledged", 3+i);
value.put("state", 14+i);
value.put("someOtherArbitraryField", Math.random() * 1000);
System.out.println(value);
coll.insert(value);
}
DBObject query = new BasicDBObject("fieldToQuery", "a");
DBObject history = new BasicDBObject().append("ishistory", 1)
.append("acknowledged", 1)
.append("state", 1);
DBObject update = new BasicDBObject("$set", history);
//This syntax for update means that all three fields will be set to the new given value
Assert.assertEquals(update.toString(), "{ \"$set\" : { \"ishistory\" : 1 , \"acknowledged\" : 1 , \"state\" : 1}}");
//Do the update, updating every document that matches the query
coll.updateMulti(query, update);
//find The new values
DBCursor updatedDocuments = coll.find(query);
for (DBObject updatedDocument : updatedDocuments) {
Assert.assertEquals(updatedDocument.get("ishistory"), 1);
Assert.assertEquals(updatedDocument.get("acknowledged"), 1);
Assert.assertEquals(updatedDocument.get("state"), 1);
System.out.println(updatedDocument);
}
}
questo test passa. Per un esempio di esecuzione, i dati nel database è:
{ "fieldToQuery" : "a" , "ishistory" : 2 , "acknowledged" : 3 , "state" : 14 , "someOtherArbitraryField" : 700.7831275035031}
{ "fieldToQuery" : "a" , "ishistory" : 3 , "acknowledged" : 4 , "state" : 15 , "someOtherArbitraryField" : 72.65538582882736}
{ "fieldToQuery" : "a" , "ishistory" : 4 , "acknowledged" : 5 , "state" : 16 , "someOtherArbitraryField" : 980.0065367659304}
{ "fieldToQuery" : "a" , "ishistory" : 5 , "acknowledged" : 6 , "state" : 17 , "someOtherArbitraryField" : 91.58266286854722}
{ "fieldToQuery" : "a" , "ishistory" : 6 , "acknowledged" : 7 , "state" : 18 , "someOtherArbitraryField" : 448.19176202797115}
Alla fine del test, dopo updateMulti viene chiamato con l'operatore $ set, i documenti nel database sono:
{ "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 700.7831275035031}
{ "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 72.65538582882736}
{ "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 980.0065367659304}
{ "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 91.58266286854722}
{ "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 448.19176202797115}
L'aggiornamento ha funzionato, impostando i tre campi su 1 per tutti i documenti corrispondenti, non toccando nessuno degli altri dati sul documento.
Potrebbe valere la pena notare che la mia sintassi per l'impostazione di query, l'aggiornamento e la storia è un po 'più leggibile e un po' più breve, anche se va facendo la stessa cosa come il codice nella domanda iniziale:
DBObject query = new BasicDBObject("fieldToQuery", "a");
DBObject history = new BasicDBObject().append("ishistory", 1)
.append("acknowledged", 1)
.append("state", 1);
DBObject update = new BasicDBObject("$set", history);
Sono corretto nel presupponendo che si desidera che tutti i record corrispondenti al proprio query
vengano aggiornati con i valori specificati? Quindi il tuo uso di updateMulti?
Qual è il valore di "aa" sopra? – Trisha
Si prega di controllare 'coll.update (query, update)' not 'updateMulti()'. – tostao
Ma se la query corrisponde a più di un record, presumo che questo aggiornamento sia necessario per aggiornare tutti i valori corrispondenti? – Trisha