Skip to content

Instantly share code, notes, and snippets.

@AntonioDiaz
Created October 14, 2022 06:01
Show Gist options
  • Save AntonioDiaz/331939b6add3025a2126e8195842efbf to your computer and use it in GitHub Desktop.
Save AntonioDiaz/331939b6add3025a2126e8195842efbf to your computer and use it in GitHub Desktop.
Update Mongo DB
@Test
public void testReplaceDocument() {
MongoCollection<Document> artists = testDb.getCollection("artists");
Bson queryFilter = new Document("_id", band1Id);
Document myBand = artists.find(queryFilter).iterator().tryNext();
Assert.assertEquals(
"Make sure that the band created in the database is"
+ " the same as the band retrieved from the database",
bandOne,
myBand);
Document replaceBand = new Document();
replaceBand.append("title", "Gorillaz");
artists.replaceOne(eq("_id", band1Id), replaceBand);
Document myNewBand = artists.find(queryFilter).iterator().tryNext();
Assert.assertEquals(myNewBand.get("_id"), band1Id);
Assert.assertEquals(myNewBand.get("title"), "Gorillaz");
Assert.assertNull(myNewBand.get("num_albums"));
Assert.assertNull(myNewBand.get("genre"));
Assert.assertNull(myNewBand.get("rating"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment