Created
April 20, 2025 07:01
-
-
Save couragecowardlydog/e0d417c37e7780b591fefa39d9281f26 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.gitrebase; | |
import com.mongodb.ReadConcern; | |
import com.mongodb.ReadPreference; | |
import com.mongodb.TransactionOptions; | |
import com.mongodb.WriteConcern; | |
import com.mongodb.client.*; | |
import com.mongodb.client.model.Filters; | |
import com.mongodb.client.model.Updates; | |
import org.bson.Document; | |
public class ReadConcernMajority { | |
private static final String DATABASE_NAME = "gitrebase"; | |
private static final String COLLECTION_PRODUCTS = "inventory"; | |
private static final String MONGO_URI = "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0"; | |
public static void main(String[] args) { | |
MongoClient client = MongoClients.create(MONGO_URI); | |
MongoDatabase database = client.getDatabase(DATABASE_NAME); | |
// Reading collection with ReadConcern.MAJORITY | |
MongoCollection<Document> products = database.getCollection(COLLECTION_PRODUCTS) | |
.withReadConcern(ReadConcern.MAJORITY); | |
products.deleteMany(Filters.eq("category", "PIZZA")); | |
Document pizza = new Document("_id", "PIZZA_001") | |
.append("name", "Cheese Burst Pizza") | |
.append("category", "PIZZA") | |
.append("price", 350); | |
products.insertOne(pizza); | |
Document result = products.find(Filters.eq("_id", "PIZZA_001")) | |
.first(); | |
if (result != null) { | |
System.out.println("Found pizza: " + result.toJson()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment