MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Come eseguire un aggiornamento in blocco di documenti in MongoDB con Java?

Utilizzando l'esempio nel manuale sul nuovo bulkWrite() API, considera la seguente raccolta di test che contiene i seguenti documenti:

{ "_id" : 1, "char" : "Brisbane", "class" : "monk", "lvl" : 4 },
{ "_id" : 2, "char" : "Eldon", "class" : "alchemist", "lvl" : 3 },
{ "_id" : 3, "char" : "Meldane", "class" : "ranger", "lvl" : 3 }

Il seguente bulkWrite() esegue più operazioni sui characters collezione:

Conchiglia mongo:

try {
    db.characters.bulkWrite([
        { 
            insertOne:{
                "document":{
                    "_id" : 4, "char" : "Dithras", "class" : "barbarian", "lvl" : 4
                }
            }
        },
        { 
            insertOne:{
                "document": {
                    "_id" : 5, "char" : "Taeln", "class" : "fighter", "lvl" : 3
                }
            }
        },
        { 
            updateOne: {
                "filter" : { "char" : "Eldon" },
                "update" : { $set : { "status" : "Critical Injury" } }
            }
        },
        { 
            deleteOne: { "filter" : { "char" : "Brisbane"} }
        },
        { 
            replaceOne: {
               "filter" : { "char" : "Meldane" },
               "replacement" : { "char" : "Tanys", "class" : "oracle", "lvl" : 4 }
            }
        }
    ]);
}
catch (e) {  print(e); }

che stampa l'output:

{
   "acknowledged" : true,
   "deletedCount" : 1,
   "insertedCount" : 2,
   "matchedCount" : 2,
   "upsertedCount" : 0,
   "insertedIds" : {
      "0" : 4,
      "1" : 5
   },
   "upsertedIds" : {

   }
}

Segue l'implementazione Java 3.2 equivalente:

MongoCollection<Document> collection = db.getCollection("characters");
List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
writes.add(
    new InsertOneModel<Document>(
        new Document("_id", 4)
            .append("char", "Dithras")
            .append("class", "barbarian")
            .append("lvl", 3)
    )
);
writes.add(
    new InsertOneModel<Document>(
        new Document("_id", 5)
            .append("char", "Taeln")
            .append("class", "fighter")
            .append("lvl", 4)
    )
);
writes.add(
    new UpdateOneModel<Document>(
        new Document("char", "Eldon"), // filter
        new Document("$set", new Document("status", "Critical Injury")) // update
    )
);
writes.add(new DeleteOneModel<Document>(new Document("char", "Brisbane")));
writes.add(
    new ReplaceOneModel<Document>(
        new Document("char", "Meldane"), 
        new Document("char", "Tanys")
            .append("class", "oracle")
            .append("lvl", 4)           
    )
);

BulkWriteResult bulkWriteResult = collection.bulkWrite(writes);

Per la tua domanda usa il replaceOne() metodo e questo sarebbe implementato come

MongoCollection<Document> collection = db.getCollection("collection");
List<WriteModel<Document>> writes = Arrays.<WriteModel<Document>>asList(
    new ReplaceOneModel<Document>(
        new Document("_id", 1001), // filter
        new Document("author", "newName"), // update
        new UpdateOptions().upsert(true) // options
    )   
);

BulkWriteResult bulkWriteResult = collection.bulkWrite(writes);