List of usage examples for com.mongodb.client MongoCollection insertOne
void insertOne(ClientSession clientSession, TDocument document);
From source file:documentation.CausalConsistencyExamples.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes an optional single argument for the connection string *//*from w w w . j a v a2s.c o m*/ public static void main(final String[] args) { setupDatabase(); MongoClient client = MongoClients.create(); // Start Causal Consistency Example 1 // Example 1: Use a causally consistent session to ensure that the update occurs before the insert. ClientSession session1 = client .startSession(ClientSessionOptions.builder().causallyConsistent(true).build()); Date currentDate = new Date(); MongoCollection<Document> items = client.getDatabase("test").withReadConcern(ReadConcern.MAJORITY) .withWriteConcern(WriteConcern.MAJORITY.withWTimeout(1000, TimeUnit.MILLISECONDS)) .getCollection("test"); items.updateOne(session1, eq("sku", "111"), set("end", currentDate)); Document document = new Document("sku", "nuts-111").append("name", "Pecans").append("start", currentDate); items.insertOne(session1, document); // End Causal Consistency Example 1 // Start Causal Consistency Example 2 // Example 2: Advance the cluster time and the operation time to that of the other session to ensure that // this client is causally consistent with the other session and read after the two writes. ClientSession session2 = client .startSession(ClientSessionOptions.builder().causallyConsistent(true).build()); session2.advanceClusterTime(session1.getClusterTime()); session2.advanceOperationTime(session1.getOperationTime()); items = client.getDatabase("test").withReadPreference(ReadPreference.secondary()) .withReadConcern(ReadConcern.MAJORITY) .withWriteConcern(WriteConcern.MAJORITY.withWTimeout(1000, TimeUnit.MILLISECONDS)) .getCollection("items"); for (Document item : items.find(session2, eq("end", BsonNull.VALUE))) { System.out.println(item); } // End Causal Consistency Example 2 }
From source file:me.lucko.luckperms.common.storage.backing.MongoDBBacking.java
License:Open Source License
@Override public boolean logAction(LogEntry entry) { return call(() -> { MongoCollection<Document> c = database.getCollection("action"); Document doc = new Document().append("timestamp", entry.getTimestamp()) .append("actor", entry.getActor()).append("actorName", entry.getActorName()) .append("type", Character.toString(entry.getType())).append("actedName", entry.getActedName()) .append("action", entry.getAction()); if (entry.getActed() != null) { doc.append("acted", entry.getActed()); }/* www .j a va 2 s . com*/ c.insertOne(doc, new InsertOneOptions()); return true; }, false); }