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

Provide code samples for MongoDB 3.6 page

    XMLWordPrintableJSON

Details

    • Task
    • Status: Closed
    • Major - P3
    • Resolution: Done
    • None
    • None
    • None

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

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: