[DRIVERS-434] Provide code samples for MongoDB 3.6 page Created: 20/Dec/17  Updated: 07/Dec/20  Resolved: 07/Dec/20

Status: Closed
Project: Drivers
Component/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Yifan (Leo) Zheng (Inactive) Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on CSHARP-2141 Create Causal Consistency code sample... Closed
depends on CSHARP-2142 Create Array update code samples for ... Closed
depends on CXX-1450 ChangeStreams Examples for Docs Closed
depends on CXX-1490 Create Causal Consistency code sample... Closed
depends on CXX-1491 Create Array update code samples for ... Closed
depends on JAVA-2726 Create Causal Consistency code sample... Closed
depends on JAVA-2727 Create Array update code samples for ... Closed
depends on NODE-1262 Create Causal Consistency code sample... Closed
depends on NODE-1263 Create Array update code samples for ... Closed
depends on PYTHON-1445 Create Causal Consistency code sample... Closed
depends on PYTHON-1446 Create Array update code samples for ... Closed
Related
related to DRIVERS-436 Add pipeline stage to ChangeStreams e... Closed
Driver Compliance:
Key Status/Resolution FixVersion
NODE-1263 Fixed 3.0.2
PYTHON-1445 Fixed 3.6.1
NODE-1262 Fixed 3.0.3
CSHARP-2141 Fixed
CSHARP-2142 Fixed
CXX-1490 Won't Fix
CXX-1491 Won't Fix
JAVA-2727 Won't Fix
JAVA-2726 Works as Designed
PYTHON-1446 Fixed 3.6.1
CXX-1450 Fixed 3.3.0-rc0

 Description   

Hi,

I'd be great if we had equivalent code samples to the JAVA code that's featured on the MongoDB 3.6 page (linked from the mongodb.com page) for a few different features.

The url is https://www.mongodb.com/mongodb-3.6

Change Streams:

// 1. The database for reactive, real-time applications
MongoClient mongoClient;
 
// Create a new MongoClient with a MongoDB URI string.
if (args.length == 0) {
// Defaults to a localhost replicaset on ports: 27017, 27018, 27019
  mongoClient = new MongoClient(new
  MongoClientURI("mongodb://localhost:27017,localhost:27018,localhost:27019"));
} else {
  mongoClient = new MongoClient(new MongoClientURI(args[0]));
}
 
// Select the MongoDB database.
MongoDatabase database = mongoClient.getDatabase("testChangeStreams");
 
// Select the collection to query.
MongoCollection<Document> collection = database.getCollection("documents");
 
// Create the change stream cursor.
MongoCursor<Document> cursor = collection.watch().iterator();

Causal Consistency

// 2. Tunable consistency controls
String connectionString = "mongodb://localhost/?readPreference=secondaryPreferred";
 
MongoClient client = new MongoClient(new MongoClientURI(connectionString);
 
MongoDatabase database = client.getDatabase("myDatabase");
MongoCollection<Document> collection = database.getCollection("myCollection");
 
// Start client session, which is causally consistent by default
try (ClientSession session = client.startSession(ClientSessionOptions.builder().build())) {
  //Run causally related operations within the session
  collection.insertOne(session, ...);
  collection.updateOne(session, ...);
 
  try (MongoCursor<Document> cursor = collection.find(session).filter(...).iterator()) {
    while (cursor.hasNext()) {
      Document cur = cursor.next();
    }
  }
}

Array Updates

// 3. Exploiting the power of arrays
MongoCollection<Document> collection = client
  .getDatabase("test")
  .getCollection("arrayUpdatesTest");
 
collection.updateOne(
  Filters.eq("_id", 1),
  Updates.set("a.$[i].b", 2),
 
new UpdateOptions()
  .arrayFilters(
    Collections.singletonList(
      Filters.eq("i.b", 0))));



 Comments   
Comment by Dmitry Lukyanov (Inactive) [ 26/Nov/18 ]

Hi yifan.zheng,

could you please clarify how the opening/closing comments should look like for these examples?
As I know, usually it looks like:

// Start #delimiter name
 
// End #delimiter name

 Thanks.

Comment by Bernie Hackett [ 01/Feb/18 ]

The change stream example already exists for all drivers. See DRIVERS-414.

Comment by Jessica Lord [ 23/Jan/18 ]

yifan.zheng is there a closing code comment to use with the Array Updates example?

// 3. Exploiting the power of arrays

and then?

I have a PR open to add this for the Node.js driver: https://github.com/mongodb/node-mongodb-native/pull/1647

Comment by Yifan (Leo) Zheng (Inactive) [ 20/Dec/17 ]

Ideally, we'd get equivalent code samples for Python, C#, C++, JavaScript & Python.

Generated at Thu Feb 08 08:21:31 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.