Uploaded image for project: 'Drivers'
  1. Drivers
  2. DRIVERS-434

Provide code samples for MongoDB 3.6 page

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Component/s: None
    • Labels:
      None
    • $i18n.getText("admin.common.words.hide")
      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
      $i18n.getText("admin.common.words.show")
      #scriptField, #scriptField *{ border: 1px solid black; } #scriptField{ border-collapse: collapse; } #scriptField td { text-align: center; /* Center-align text in table cells */ } #scriptField td.key { text-align: left; /* Left-align text in the Key column */ } #scriptField a { text-decoration: none; /* Remove underlines from links */ border: none; /* Remove border from links */ } /* Add green background color to cells with FixVersion */ #scriptField td.hasFixVersion { background-color: #00FF00; /* Green color code */ } /* Center-align the first row headers */ #scriptField th { text-align: center; } 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

      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))));
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            yifan.zheng@mongodb.com Yifan (Leo) Zheng (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: