PolarSPARC

Hands-on MongoDB :: Part-2


Bhaskar S 01/30/2021 (UPDATED)


Introduction

In Part-1, we setup the high-availability 3-node cluster using Docker got and our hands dirty with MongoDB by using the mongo command-line interface.

In this article, we will demonstrate the same set of operations on MongoDB using the programming language drivers for Java and Python.

Hands-on with Java MongoDB Driver

To setup the Java directory structure for the demonstrations, execute the following commands:

$ cd $HOME

$ mkdir -p java/mongodb

$ cd $HOME/java/mongodb

$ mkdir -p src/main/java target

$ mkdir -p src/main/java/com/polarsparc/mongodb

For the Java language, we will leverage Maven to manage the build as well as the package dependencies.

The following is the Maven pom.xml file located in the directory $HOME/java/mongodb:


pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.polarsparc.mongodb</groupId>
    <artifactId>Mongo_1</artifactId>
    <version>1.0</version>

    <properties>
        <java.version>11</java.version>
        <mongo.driver.version>4.2.0</mongo.driver.version>
        <junit.jupiter.version>5.7.0</junit.jupiter.version>
        <maven.compiler.version>3.8.1</maven.compiler.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-sync</artifactId>
            <version>${mongo.driver.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.version}</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

We will first explain the different sections of the Java program (called Mongo_1.java) by their respective methods (before showing the whole program).

The following is the Java method called connectToDB() that connects to the MongoDB primary node of the cluster:


connectToDB()
// Open connection to MongoDB cluster
static void connectToDB() {
  String db_url = "mongodb://192.168.1.53:5001/mydb";

  ConnectionString cs = new ConnectionString(db_url);

  _client = MongoClients.create(cs);

  LOGGER.info(String.format("Connected to MongoDB @ %s", db_url));
}

The following are brief descriptions for some of the class(es)/method(s) used in the code segment above:

The following is the Java method called setupDbAndCollection() gets a reference to the specified MongoDB database and collection:


setupDbAndCollection()
//  Setup the database and the collection
static void setupDbAndCollection() {
  // Get reference to MongoDB database mydb
  _db = _client.getDatabase("mydb");

  // Get reference to the collection contacts
  _contacts = _db.getCollection("contacts");

  // Display all the collections in the database mydb
  LOGGER.info("List of collections:");
  for (String name : _db.listCollectionNames()) {
    LOGGER.info(name);
  }
}

The following are brief descriptions for some of the class(es)/method(s) used in the code segment above:

The following is the Java method called insertContactAndCheck() inserts a document into the collection, lists all the collections in the database, displays the count of documents in the collection, and displays all the documents from the collection:


insertContactAndCheck()
// Insert a document into the collection contacts
static void insertContactAndCheck() {
  // Insert a document into the collection contacts
  // { first: "Alice", last: "Thompson",
  //   email: { personal: "alice.t@home.io", work: "alice.thompson@work.net" },
  //   mobile: { personal: "123 456 7890" } }
  Document email = new Document("personal", "alice.t@home.io").append("work", "alice.thompson@work.net");
  Document mobile = new Document("personal", "123 456 7890");
  Document doc = new Document("first", "Alice")
      .append("last", "Thompson")
      .append("email", email)
      .append("mobile", mobile);
  _contacts.insertOne(doc);

  // Display all the collections in the database mydb
  LOGGER.info("List of collections:");
  for (String name : _db.listCollectionNames()) {
    LOGGER.info(name);
  }

  // Display the number of documents in the collection contacts
  LOGGER.info(String.format("Number of documents in contacts: %d", _contacts.countDocuments()));

  // Query and display all the documents in the collection contacts
  for (Document d : _contacts.find()) {
    LOGGER.info(d.toJson());
  }
}

The following are brief descriptions for some of the class(es)/method(s) used in the code segment above:

The following is the Java method called insertFiveContacts() inserts five documents into the collection:


insertFiveContacts()
// Insert five more documents into the collection contacts
static void insertFiveContacts() {
  // Insert five more documents into the collection contacts

  // { "first": "Bob", "last": "Jones",
  //   "email": { "work": "bobj@doktor.net" },
  //   "mobile": { "work": "234 567 8901" } }
  Document email = new Document("work", "bobj@doktor.net");
  Document mobile = new Document("work", "234 567 8901");
  Document doc = new Document("first", "Bob")
      .append("last", "Jones")
      .append("email", email)
      .append("mobile", mobile);
  _contacts.insertOne(doc);

  // { "first": "Charlie", "last": "Lee", "email": { "personal": "cl3000@ranch.net" }
  email = new Document("personal", "cl3000@ranch.net");
  doc = new Document("first", "Charlie")
      .append("last", "Lee")
      .append("email", email);
  _contacts.insertOne(doc);

  List<Document> documents = new ArrayList<>();

  // { "first": "Eve", "middle": "Jo", "last": "Parker",
  //   "email": { "work": "ej_parker@awesome.org" },
  //   "mobile": { "personal": "345 678 9012" } }
  email = new Document("work", "ej_parker@awesome.org");
  mobile = new Document("personal", "345 678 9012");
  doc = new Document("first", "Eve")
      .append("middle", "Jo")
      .append("last", "Parker")
      .append("email", email)
      .append("mobile", mobile);

  documents.add(doc);

  // { "first": "Frank", "last": "Smith",
  //   "email": { "personal": "frank45@root.org", "work": "frank.smith@excellent.net" },
  //   "mobile": { "personal": "456 789 0123", "work": "567 890 1234" } }
  email = new Document("personal", "frank45@root.org").append("work", "frank.smith@excellent.net");
  mobile = new Document("personal", "456 789 0123").append("work", "567 890 1234");
  doc = new Document("first", "Frank")
      .append("last", "Smith")
      .append("email", email)
      .append("mobile", mobile);

  documents.add(doc);

  // { "first": "Frank", "last": "Cooper", "email": { "personal": "frankc@runner.org" } }
  email = new Document("personal", "frankc@runner.or" +
      "");
  doc = new Document("first", "Frank")
      .append("last", "Cooper")
      .append("email", email);

  documents.add(doc);

  _contacts.insertMany(documents);
}

The following are brief descriptions for some of the class(es)/method(s) used in the code segment above:

The following is the Java method called queryContacts() queries documents from the collection using various filters and selections:


queryContacts()
// Perform various query operations
static void queryContacts() {
  // Query and display all the documents in the collection contacts
  LOGGER.info("Documents in contacts:");
  for (Document doc : _contacts.find()) {
    LOGGER.info(doc.toJson());
  }

  // Query and display the document for first = "Bob" from the collection contacts
  LOGGER.info("Document for Bob:");
  Document doc = _contacts.find(eq("first", "Bob")).first();
  if (doc != null) {
    LOGGER.info(doc.toJson());
  }

  // Query and display the document for first = "Charlie" and last = "Lee" from the collection contacts
  LOGGER.info("Document for Charlie Lee:");
  doc = _contacts.find(
      and(
      eq("first", "Charlie"),
      eq("last", "Lee")))
      .first();
  if (doc != null) {
    LOGGER.info(doc.toJson());
  }

  // Query and display the document for email.work = "bobj@doktor.net" from the collection contacts
  LOGGER.info("Document for bobj@doktor.net:");
  doc = _contacts.find(eq("email.work", "bobj@doktor.net")).first();
  if (doc != null) {
    LOGGER.info(doc.toJson());
  }

  // Query and display only first and last for all the documents in the collection contacts
  for (Document d : _contacts.find().projection(fields(
      include("first", "last")))) {
    LOGGER.info(d.toJson());
  }

  // Query and display only first and last (without _id) for all the documents in the collection contacts
  for (Document d : _contacts.find().projection(fields(
      include("first", "last"), excludeId()))) {
    LOGGER.info(d.toJson());
  }

  // Query and display only first, last, and mobile.persona (without _id) for all the documents
  // in the collection contacts
  for (Document d : _contacts.find().projection(fields(
      include("first", "last", "mobile.personal"), excludeId()))) {
    LOGGER.info(d.toJson());
  }

  // Query and display only first and last (without _id) for 3 documents in the collection contacts
  for (Document d : _contacts.find().projection(fields(
      include("first", "last"), excludeId())).limit(3)) {
    LOGGER.info(d.toJson());
  }
}

The following are brief descriptions for some of the class(es)/method(s) used in the code segment above:

The following is the Java method called updateContacts() updates a document from the collection that replaces all the information currently in the document (wrong way), then shows the correct way to replace the field(s) in the document from the collection, and finally demonstrates how to upsert a document into the collection:


updateContacts()
// Perform some update operations
static void updateContacts() {
  // Update the document for first = "Charlie" in the collection contacts
  Document mobile = new Document("mobile", new Document("personal", "678 901 2345"));
  _contacts.replaceOne(new Document("first", "Charlie"), mobile);

  // Query and display the document for first = "Charlie" from the collection contacts
  LOGGER.info("Document for Charlie [after update]:");
  Document doc = _contacts.find(eq("first", "Charlie")).first();
  if (doc != null) {
    LOGGER.info(doc.toJson());
  } else {
    LOGGER.warning("No document for Charlie !!!");
  }

  // Query and display the document for mobile: { personal: "678 901 2345" } from the collection contacts
  LOGGER.info("Document for Charlie [after update using mobile]:");
  doc = _contacts.find(mobile).first();
  if (doc != null) {
    LOGGER.info(doc.toJson());
  }

  // Update the document for mobile: { personal: "678 901 2345" } (to restore Charlie) in the collection contacts
  Document email = new Document("personal", "cl3000@ranch.net");
  doc = new Document("first", "Charlie")
      .append("last", "Lee")
      .append("email", email)
      .append("mobile", new Document("personal", "678 901 2345"));
  _contacts.replaceOne(mobile, doc);

  // Query and display the document for first = "Charlie" from the collection contacts (after fix)
  LOGGER.info("Document for Charlie [after update fix]:");
  doc = _contacts.find(eq("first", "Charlie")).first();
  if (doc != null) {
    LOGGER.info(doc.toJson());
  }

  // Upsert the document for first = "George" in the collection contacts
  // db.contacts.update({ first: "George" }, { first: "George", last: "Baker",
  //                      email: { work: "g_baker@crap.org" },
  //                      mobile: { work: "789 012 3456" } },
  //                      { upsert: true })
  ReplaceOptions upsert = new ReplaceOptions().upsert(true);
  doc = new Document("first", "George")
      .append("last", "Baker")
      .append("email", new Document("work", "g_baker@crap.org"))
      .append("mobile", new Document("work", "789 012 3456"));
  _contacts.replaceOne(eq("first", "George"), doc, upsert);

  // Query and display the document for first: "George" from the collection contacts
  LOGGER.info("Document for George [after upsert]:");
  doc = _contacts.find(eq("first", "George")).first();
  if (doc != null) {
    LOGGER.info(doc.toJson());
  }
}

The following are brief descriptions for some of the class(es)/method(s) used in the code segment above:

The following is the Java method called removeAndDropContacts() removes a document from the collection, displays the exisiting documents in the collection, removes the remaining documents from the collection, displays the count of documents from the collection, drops the collection, and displays the list of collections in the database:


removeAndDropContacts()
// Perform some delete operations
static void removeAndDropContacts() {
  // Delete the document for first = "Bob" from the collection contacts
  _contacts.deleteOne(new Document("first", "Bob"));

  // Query and display all the documents in the collection contacts
  LOGGER.info("\"Documents in contacts [after removing Bob]:\"");
  for (Document doc : _contacts.find()) {
    LOGGER.info(doc.toJson());
  }

  // Delete all the remaining documents for the collection contacts
  _contacts.deleteMany(new Document());

  // Display the number of documents in the collection contacts
  LOGGER.info(String.format("Number of documents in contacts: %d", _contacts.countDocuments()));

  // Drop the collection contacts
  _contacts.drop();

  // Display all the collections in the database mydb
  LOGGER.info("List of collections:");
  for (String name : _db.listCollectionNames()) {
    LOGGER.info(name);
  }
}

The following are brief descriptions for some of the class(es)/method(s) used in the code segment above:

The following is the full Java program called Mongo_1.java that performs all the operations we executed through the command-line interface in Part-1 :


Mongo_1.java
/*
    @Author: Bhaskar S
    @Blog:   https://www.polarsparc.com
    @Date:   30 Jan 2021
*/

package com.polarsparc.mongodb;

import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;

import org.bson.Document;

import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Projections.*;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Mongo_1 {
  private final static Logger LOGGER = Logger.getLogger(Mongo_1.class.getName());

  static {
    LOGGER.setLevel(Level.INFO);
    System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] <%2$s> %5$s %n");
  }

  private static MongoClient _client = null;
  private static MongoDatabase _db = null;
  private static MongoCollection<Document> _contacts = null;

  // Open connection to MongoDB cluster
  static void connectToDB() {
    String db_url = "mongodb://192.168.1.53:5001/mydb";

    ConnectionString cs = new ConnectionString(db_url);

    _client = MongoClients.create(cs);

    LOGGER.info(String.format("Connected to MongoDB @ %s", db_url));
  }

  //  Setup the database and the collection
  static void setupDbAndCollection() {
    // Get reference to MongoDB database mydb
    _db = _client.getDatabase("mydb");

    // Get reference to the collection contacts
    _contacts = _db.getCollection("contacts");

    // Display all the collections in the database mydb
    LOGGER.info("List of collections:");
    for (String name : _db.listCollectionNames()) {
      LOGGER.info(name);
    }
  }

  // Insert a document into the collection contacts
  static void insertContactAndCheck() {
    // Insert a document into the collection contacts
    // { first: "Alice", last: "Thompson",
    //   email: { personal: "alice.t@home.io", work: "alice.thompson@work.net" },
    //   mobile: { personal: "123 456 7890" } }
    Document email = new Document("personal", "alice.t@home.io").append("work", "alice.thompson@work.net");
    Document mobile = new Document("personal", "123 456 7890");
    Document doc = new Document("first", "Alice")
        .append("last", "Thompson")
        .append("email", email)
        .append("mobile", mobile);
    _contacts.insertOne(doc);

    // Display all the collections in the database mydb
    LOGGER.info("List of collections:");
    for (String name : _db.listCollectionNames()) {
      LOGGER.info(name);
    }

    // Display the number of documents in the collection contacts
    LOGGER.info(String.format("Number of documents in contacts: %d", _contacts.countDocuments()));

    // Query and display all the documents in the collection contacts
    for (Document d : _contacts.find()) {
      LOGGER.info(d.toJson());
    }
  }

  // Insert five more documents into the collection contacts
  static void insertFiveContacts() {
    // Insert five more documents into the collection contacts

    // { "first": "Bob", "last": "Jones",
    //   "email": { "work": "bobj@doktor.net" },
    //   "mobile": { "work": "234 567 8901" } }
    Document email = new Document("work", "bobj@doktor.net");
    Document mobile = new Document("work", "234 567 8901");
    Document doc = new Document("first", "Bob")
        .append("last", "Jones")
        .append("email", email)
        .append("mobile", mobile);
    _contacts.insertOne(doc);

    // { "first": "Charlie", "last": "Lee", "email": { "personal": "cl3000@ranch.net" }
    email = new Document("personal", "cl3000@ranch.net");
    doc = new Document("first", "Charlie")
        .append("last", "Lee")
        .append("email", email);
    _contacts.insertOne(doc);

    List<Document> documents = new ArrayList<>();

    // { "first": "Eve", "middle": "Jo", "last": "Parker",
    //   "email": { "work": "ej_parker@awesome.org" },
    //   "mobile": { "personal": "345 678 9012" } }
    email = new Document("work", "ej_parker@awesome.org");
    mobile = new Document("personal", "345 678 9012");
    doc = new Document("first", "Eve")
        .append("middle", "Jo")
        .append("last", "Parker")
        .append("email", email)
        .append("mobile", mobile);

    documents.add(doc);

    // { "first": "Frank", "last": "Smith",
    //   "email": { "personal": "frank45@root.org", "work": "frank.smith@excellent.net" },
    //   "mobile": { "personal": "456 789 0123", "work": "567 890 1234" } }
    email = new Document("personal", "frank45@root.org").append("work", "frank.smith@excellent.net");
    mobile = new Document("personal", "456 789 0123").append("work", "567 890 1234");
    doc = new Document("first", "Frank")
        .append("last", "Smith")
        .append("email", email)
        .append("mobile", mobile);

    documents.add(doc);

    // { "first": "Frank", "last": "Cooper", "email": { "personal": "frankc@runner.org" } }
    email = new Document("personal", "frankc@runner.or" +
        "");
    doc = new Document("first", "Frank")
        .append("last", "Cooper")
        .append("email", email);

    documents.add(doc);

    _contacts.insertMany(documents);
  }

  // Perform various query operations
  static void queryContacts() {
    // Query and display all the documents in the collection contacts
    LOGGER.info("Documents in contacts:");
    for (Document doc : _contacts.find()) {
      LOGGER.info(doc.toJson());
    }

    // Query and display the document for first = "Bob" from the collection contacts
    LOGGER.info("Document for Bob:");
    Document doc = _contacts.find(eq("first", "Bob")).first();
    if (doc != null) {
      LOGGER.info(doc.toJson());
    }

    // Query and display the document for first = "Charlie" and last = "Lee" from the collection contacts
    LOGGER.info("Document for Charlie Lee:");
    doc = _contacts.find(
        and(
        eq("first", "Charlie"),
        eq("last", "Lee")))
        .first();
    if (doc != null) {
      LOGGER.info(doc.toJson());
    }

    // Query and display the document for email.work = "bobj@doktor.net" from the collection contacts
    LOGGER.info("Document for bobj@doktor.net:");
    doc = _contacts.find(eq("email.work", "bobj@doktor.net")).first();
    if (doc != null) {
      LOGGER.info(doc.toJson());
    }

    // Query and display only first and last for all the documents in the collection contacts
    for (Document d : _contacts.find().projection(fields(
        include("first", "last")))) {
      LOGGER.info(d.toJson());
    }

    // Query and display only first and last (without _id) for all the documents in the collection contacts
    for (Document d : _contacts.find().projection(fields(
        include("first", "last"), excludeId()))) {
      LOGGER.info(d.toJson());
    }

    // Query and display only first, last, and mobile.persona (without _id) for all the documents
    // in the collection contacts
    for (Document d : _contacts.find().projection(fields(
        include("first", "last", "mobile.personal"), excludeId()))) {
      LOGGER.info(d.toJson());
    }

    // Query and display only first and last (without _id) for 3 documents in the collection contacts
    for (Document d : _contacts.find().projection(fields(
        include("first", "last"), excludeId())).limit(3)) {
      LOGGER.info(d.toJson());
    }
  }

  // Perform some update operations
  static void updateContacts() {
    // Update the document for first = "Charlie" in the collection contacts
    Document mobile = new Document("mobile", new Document("personal", "678 901 2345"));
    _contacts.replaceOne(new Document("first", "Charlie"), mobile);

    // Query and display the document for first = "Charlie" from the collection contacts
    LOGGER.info("Document for Charlie [after update]:");
    Document doc = _contacts.find(eq("first", "Charlie")).first();
    if (doc != null) {
      LOGGER.info(doc.toJson());
    } else {
      LOGGER.warning("No document for Charlie !!!");
    }

    // Query and display the document for mobile: { personal: "678 901 2345" } from the collection contacts
    LOGGER.info("Document for Charlie [after update using mobile]:");
    doc = _contacts.find(mobile).first();
    if (doc != null) {
      LOGGER.info(doc.toJson());
    }

    // Update the document for mobile: { personal: "678 901 2345" } (to restore Charlie) in the collection contacts
    Document email = new Document("personal", "cl3000@ranch.net");
    doc = new Document("first", "Charlie")
        .append("last", "Lee")
        .append("email", email)
        .append("mobile", new Document("personal", "678 901 2345"));
    _contacts.replaceOne(mobile, doc);

    // Query and display the document for first = "Charlie" from the collection contacts (after fix)
    LOGGER.info("Document for Charlie [after update fix]:");
    doc = _contacts.find(eq("first", "Charlie")).first();
    if (doc != null) {
      LOGGER.info(doc.toJson());
    }
  
    // Upsert the document for first = "George" in the collection contacts
    // db.contacts.update({ first: "George" }, { first: "George", last: "Baker",
    //                      email: { work: "g_baker@crap.org" },
    //                      mobile: { work: "789 012 3456" } },
    //                      { upsert: true })
    ReplaceOptions upsert = new ReplaceOptions().upsert(true);
    doc = new Document("first", "George")
        .append("last", "Baker")
        .append("email", new Document("work", "g_baker@crap.org"))
        .append("mobile", new Document("work", "789 012 3456"));
    _contacts.replaceOne(eq("first", "George"), doc, upsert);
  
    // Query and display the document for first: "George" from the collection contacts
    LOGGER.info("Document for George [after upsert]:");
    doc = _contacts.find(eq("first", "George")).first();
    if (doc != null) {
      LOGGER.info(doc.toJson());
    }
  }

  // Perform some delete operations
  static void removeAndDropContacts() {
    // Delete the document for first = "Bob" from the collection contacts
    _contacts.deleteOne(new Document("first", "Bob"));

    // Query and display all the documents in the collection contacts
    LOGGER.info("\"Documents in contacts [after removing Bob]:\"");
    for (Document doc : _contacts.find()) {
      LOGGER.info(doc.toJson());
    }

    // Delete all the remaining documents for the collection contacts
    _contacts.deleteMany(new Document());

    // Display the number of documents in the collection contacts
    LOGGER.info(String.format("Number of documents in contacts: %d", _contacts.countDocuments()));

    // Drop the collection contacts
    _contacts.drop();

    // Display all the collections in the database mydb
    LOGGER.info("List of collections:");
    for (String name : _db.listCollectionNames()) {
      LOGGER.info(name);
    }
  }

  public static void main(String[] args) {
    LOGGER.info("Ready to start ...");

    try {
      connectToDB();
      setupDbAndCollection();
      insertContactAndCheck();
      insertFiveContacts();
      queryContacts();
      updateContacts();
      removeAndDropContacts();
    }
    catch (Exception ex) {
      ex.printStackTrace(System.err);
    }
    finally {
      // Close the database connection
      if (_client != null) {
        _client.close();
      }
    }

    LOGGER.info("Done !!!");
  }
}

Ensure the MongoDB cluster is up and running and mongodb-n1 is the primary. Open a terminal window and execute the following commands:

$ cd $HOME/java/mongodb

$ mvn exec:java -Dexec.mainClass=com.polarsparc.mongodb.Mongo_1

The following will be the typical output:

Output.1

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.polarsparc.mongodb:Mongo_1 >-------------------
[INFO] Building Mongo_1 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ Mongo_1 ---
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 main> Ready to start ... 
[2021-01-30 16:40:33] [WARNING] <com.mongodb.diagnostics.logging.Loggers shouldUseSLF4J> SLF4J not found on the classpath.  Logging is disabled for the 'org.mongodb.driver' component 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 connectToDB> Connected to MongoDB @ mongodb://192.168.1.53:5001/mydb 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 setupDbAndCollection> List of collections: 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 insertContactAndCheck> List of collections: 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 insertContactAndCheck> contacts 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 insertContactAndCheck> Number of documents in contacts: 1 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 insertContactAndCheck> {"_id": {"$oid": "6015d251ed846520c9b56172"}, "first": "Alice", "last": "Thompson", "email": {"personal": "alice.t@home.io", "work": "alice.thompson@work.net"}, "mobile": {"personal": "123 456 7890"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> Documents in contacts: 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56172"}, "first": "Alice", "last": "Thompson", "email": {"personal": "alice.t@home.io", "work": "alice.thompson@work.net"}, "mobile": {"personal": "123 456 7890"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56173"}, "first": "Bob", "last": "Jones", "email": {"work": "bobj@doktor.net"}, "mobile": {"work": "234 567 8901"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56174"}, "first": "Charlie", "last": "Lee", "email": {"personal": "cl3000@ranch.net"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56175"}, "first": "Eve", "middle": "Jo", "last": "Parker", "email": {"work": "ej_parker@awesome.org"}, "mobile": {"personal": "345 678 9012"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56176"}, "first": "Frank", "last": "Smith", "email": {"personal": "frank45@root.org", "work": "frank.smith@excellent.net"}, "mobile": {"personal": "456 789 0123", "work": "567 890 1234"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56177"}, "first": "Frank", "last": "Cooper", "email": {"personal": "frankc@runner.or"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> Document for Bob: 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56173"}, "first": "Bob", "last": "Jones", "email": {"work": "bobj@doktor.net"}, "mobile": {"work": "234 567 8901"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> Document for Charlie Lee: 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56174"}, "first": "Charlie", "last": "Lee", "email": {"personal": "cl3000@ranch.net"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> Document for bobj@doktor.net: 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56173"}, "first": "Bob", "last": "Jones", "email": {"work": "bobj@doktor.net"}, "mobile": {"work": "234 567 8901"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56172"}, "first": "Alice", "last": "Thompson"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56173"}, "first": "Bob", "last": "Jones"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56174"}, "first": "Charlie", "last": "Lee"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56175"}, "first": "Eve", "last": "Parker"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56176"}, "first": "Frank", "last": "Smith"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"_id": {"$oid": "6015d251ed846520c9b56177"}, "first": "Frank", "last": "Cooper"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Alice", "last": "Thompson"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Bob", "last": "Jones"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Charlie", "last": "Lee"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Eve", "last": "Parker"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Frank", "last": "Smith"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Frank", "last": "Cooper"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Alice", "last": "Thompson", "mobile": {"personal": "123 456 7890"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Bob", "last": "Jones", "mobile": {}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Charlie", "last": "Lee"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Eve", "last": "Parker", "mobile": {"personal": "345 678 9012"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Frank", "last": "Smith", "mobile": {"personal": "456 789 0123"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Frank", "last": "Cooper"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Alice", "last": "Thompson"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Bob", "last": "Jones"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 queryContacts> {"first": "Charlie", "last": "Lee"} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 updateContacts> Document for Charlie [after update]: 
[2021-01-30 16:40:33] [WARNING] <com.polarsparc.mongodb.Mongo_1 updateContacts> No document for Charlie !!! 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 updateContacts> Document for Charlie [after update using mobile]: 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 updateContacts> {"_id": {"$oid": "6015d251ed846520c9b56174"}, "mobile": {"personal": "678 901 2345"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 updateContacts> Document for Charlie [after update fix]: 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 updateContacts> {"_id": {"$oid": "6015d251ed846520c9b56174"}, "first": "Charlie", "last": "Lee", "email": {"personal": "cl3000@ranch.net"}, "mobile": {"personal": "678 901 2345"}} 
[2021-02-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 updateContacts> Document for George [after upsert]:
[2021-02-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 updateContacts> {"_id": {"$oid": "601c9df98d37d50b59287bb7"}, "first": "George", "last": "Baker", "email": {"work": "g_baker@crap.org"}, "mobile": {"work": "789 012 3456"}}
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 removeAndDropContacts> "Documents in contacts [after removing Bob]:" 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 removeAndDropContacts> {"_id": {"$oid": "6015d251ed846520c9b56172"}, "first": "Alice", "last": "Thompson", "email": {"personal": "alice.t@home.io", "work": "alice.thompson@work.net"}, "mobile": {"personal": "123 456 7890"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 removeAndDropContacts> {"_id": {"$oid": "6015d251ed846520c9b56174"}, "first": "Charlie", "last": "Lee", "email": {"personal": "cl3000@ranch.net"}, "mobile": {"personal": "678 901 2345"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 removeAndDropContacts> {"_id": {"$oid": "6015d251ed846520c9b56175"}, "first": "Eve", "middle": "Jo", "last": "Parker", "email": {"work": "ej_parker@awesome.org"}, "mobile": {"personal": "345 678 9012"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 removeAndDropContacts> {"_id": {"$oid": "6015d251ed846520c9b56176"}, "first": "Frank", "last": "Smith", "email": {"personal": "frank45@root.org", "work": "frank.smith@excellent.net"}, "mobile": {"personal": "456 789 0123", "work": "567 890 1234"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 removeAndDropContacts> {"_id": {"$oid": "6015d251ed846520c9b56177"}, "first": "Frank", "last": "Cooper", "email": {"personal": "frankc@runner.or"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 removeAndDropContacts> {"_id": {"$oid": "601c9df98d37d50b59287bb7"}, "first": "George", "last": "Baker", "email": {"work": "g_baker@crap.org"}, "mobile": {"work": "789 012 3456"}} 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 removeAndDropContacts> Number of documents in contacts: 0 
[2021-01-30 16:40:33] [INFO   ] <com.polarsparc.mongodb.Mongo_1 removeAndDropContacts> List of collections: 
[2021-01-30 16:40:34] [INFO   ] <com.polarsparc.mongodb.Mongo_1 main> Done !!! 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.981 s
[INFO] Finished at: 2021-01-30T16:40:34-05:00
[INFO] ------------------------------------------------------------------------

PERFECT !!! We have successfully demonstrated the use of the Java MongoDB driver using an example code.

Hands-on with Python MongoDB Driver

Ensure the Python Mongo driver is installed by executing the following commands:

$ sudo apt install pip3 -y

$ sudo pip3 install pymongo

To setup the Python directory structure for the demonstrations, execute the following commands:

$ cd $HOME

$ mkdir -p python/mongodb

$ cd $HOME/python/mongodb

We will first explain the different sections of the Python program (called Mongo-1.py) by their respective functions (before showing the whole program).

The following is the Python function called connect_to_db() that connects to the MongoDB primary node of the cluster:


connect_to_db()
# Open connection to MongoDB cluster
def connect_to_db():
    db_url = 'mongodb://192.168.1.53:5001/mydb'
    client = None
    try:
        client = pymongo.MongoClient(db_url)
        logging.info("Connected to MongoDB @ %s" % db_url)
    except ConnectionFailure as e:
        logging.error("Could not connect to MongoDB @ %s: %s" % (db_url, e))
    return client

The following are brief descriptions for some of the function(s) used in the code segment above:

The following is the Python function called setup_db_n_collection() gets a reference to the specified MongoDB database and collection:


setup_db_n_collection()
# Setup the database and the collection
def setup_db_n_collection(client):
    # logging.info("setup ...")

    # Setup the db variable to point to the database mydb
    db = client["mydb"]

    # Setup the contacts variable to point to the collection contacts
    contacts = db["contacts"]

    # Display all the collections in the database mydb
    logging.info("List of collections: %s" % db.list_collection_names())

    return db, contacts

The following are brief descriptions for some of the function(s) used in the code segment above:

The following is the Python function called insert_contact_n_check() inserts a document into the collection, lists all the collections in the database, displays the count of documents in the collection, and displays all the documents from the collection:


insert_contact_n_check()
# Insert a document into the collection contacts
def insert_contact_n_check(db, contacts):
    # Insert a document for Alice
    contacts.insert_one({"first": "Alice", "last": "Thompson",
                          "email": {"personal": "alice.t@home.io", "work": "alice.thompson@work.net"},
                          "mobile": {"personal": "123 456 7890"}})

    # Display all the collections in the database mydb
    logging.info("List of collections: %s" % db.list_collection_names())

    # Display the number of documents in the collection contacts
    logging.info("Number of documents in contacts: %d" % contacts.count_documents({}))

    # Query and display all the documents in the collection contacts
    cursor = contacts.find()
    for doc in cursor:
        logging.info(doc)

The following are brief descriptions for some of the function(s) used in the code segment above:

The following is the Python function called insert_five_contacts() inserts five documents into the collection:


insert_five_contacts()
# Insert five more documents into the collection contacts
def insert_five_contacts(contacts):
    contacts.insert_one({"first": "Bob", "last": "Jones",
                         "email": {"work": "bobj@doktor.net"},
                         "mobile": {"work": "234 567 8901"}})
    contacts.insert_one({"first": "Charlie", "last": "Lee",
                         "email": {"personal": "cl3000@ranch.net"}})
    contacts.insert_many([{"first": "Eve", "middle": "Jo", "last": "Parker",
                           "email": {"work": "ej_parker@awesome.org"},
                           "mobile": {"personal": "345 678 9012"}}])
    contacts.insert_many([{"first": "Frank", "last": "Smith",
                           "email": {"personal": "frank45@root.org", "work": "frank.smith@excellent.net"},
                           "mobile": {"personal": "456 789 0123", "work": "567 890 1234"}}])
    contacts.insert_one({"first": "Frank", "last": "Cooper",
                         "email": {"personal": "frankc@runner.org"}})

The following are brief descriptions for some of the function(s) used in the code segment above:

The following is the Python function called query_contacts() queries documents from the collection using various filters and selections:


query_contacts()
# Perform various query operations
def query_contacts(contacts):
    # Query and display all the documents in the collection contacts
    logging.info("Documents in contacts:")
    cursor = contacts.find()
    for doc in cursor:
        logging.info(doc)

    # Query and display the document for first = "Bob" from the collection contacts
    logging.info("Document for Bob:")
    logging.info(contacts.find_one({"first": "Bob"}))

    # Query and display the document for first = "Charlie" and last = "Lee" from the collection contacts
    logging.info("Document for Charlie Lee:")
    logging.info(contacts.find_one({"first": "Charlie", "last": "Lee"}))

    # Query and display the document for email.work = "bobj@doktor.net" from the collection contacts
    logging.info("Document for bobj@doktor.net:")
    logging.info(contacts.find_one({"email.work": "bobj@doktor.net"}))

    # Query and display only first and last for all the documents in the collection contacts
    logging.info("First and Last name(s) in contacts:")
    cursor = contacts.find({}, {"first": 1, "last": 1})
    for doc in cursor:
        logging.info(doc)

    # Query and display only first and last (without _id) for all the documents in the collection contacts
    logging.info("First and Last name(s) [without _id] in contacts:")
    cursor = contacts.find({}, {"first": 1, "last": 1, "_id": 0})
    for doc in cursor:
        logging.info(doc)

    # Query and display only first, last, and mobile.persona (without _id)
    # for all the documents in the collection contacts
    logging.info("First, Last name, Personal Mobile(s) [without _id] in contacts:")
    cursor = contacts.find({}, {"first": 1, "last": 1, "mobile.personal": 1, "_id": 0})
    for doc in cursor:
        logging.info(doc)

    # Query and display only first and last (without _id) for 3 documents in the collection contacts
    logging.info("First and Last name(s) [without _id with limit 3] in contacts:")
    cursor = contacts.find({}, {"first": 1, "last": 1, "_id": 0}).limit(3)
    for doc in cursor:
        logging.info(doc)

The following are brief descriptions for some of the function(s) used in the code segment above:

The following is the Python function called update_contacts() updates a document from the collection that replaces all the information currently in the document (wrong way), then shows the correct way to replace the field(s) in the document from the collection, and finally demonstrates the upsert of a document into the collection:


update_contacts()
# Perform some update operations
def update_contacts(contacts):
    # Update the document for first = "Charlie" in the collection contacts
    contacts.replace_one({"first": "Charlie"}, {"mobile": {"personal": "678 901 2345"}})

    # Query and display the document for first = "Charlie" from the collection contacts
    logging.info("Document for Charlie [after update]:")
    doc = contacts.find_one({"first": "Charlie"})
    if doc:
        logging.info(doc)
    else:
        logging.warning("No document for Charlie !!!")

    # Query and display the document for mobile: { personal: "678 901 2345" } from the collection contacts
    logging.info("Document for Charlie [after update using mobile]:")
    logging.info(contacts.find_one({"mobile": {"personal": "678 901 2345"}}))

    # Update the document for mobile: { personal: "678 901 2345" } (to restore Charlie) in the collection contacts
    contacts.replace_one({"mobile": {"personal": "678 901 2345"}},
                          {"first": "Charlie", "last": "Lee",
                          "email": {"personal": "cl3000@ranch.net"},
                          "mobile": {"personal": "678 901 2345"}})

    # Query and display the document for first = "Charlie" from the collection contacts (after fix)
    logging.info("Document for Charlie [after update fix]:")
    logging.info(contacts.find_one({"first": "Charlie"}))
  
    # Upsert the document for first = "George" in the collection contacts
    contacts.replace_one({"first": "George"}, {"first": "George", "last": "Baker",
                                               "email": {"work": "g_baker@crap.org"},
                                               "mobile": {"work": "789 012 3456"}},
                         upsert=True)

    # Query and display the document for first = "George" from the collection contacts (after upsert)
    logging.info("Document for George [after upsert]:")
    logging.info(contacts.find_one({"first": "George"}))

The following are brief descriptions for some of the function(s) used in the code segment above:

The following is the Python function called remove_n_drop_contacts() removes a document from the collection, displays the exisiting documents in the collection, removes the remaining documents from the collection, displays the count of documents from the collection, drops the collection, and displays the list of collections in the database:


remove_n_drop_contacts()
# Perform some delete operations
def remove_n_drop_contacts(db, contacts):
    # Delete the document for first = "Bob" from the collection contacts
    contacts.delete_one({"first": "Bob"})

    # Query and display all the documents in the collection contacts
    logging.info("Documents in contacts [after removing Bob]:")
    cursor = contacts.find()
    for doc in cursor:
        logging.info(doc)

    # Delete all the remaining documents for the collection contacts
    contacts.delete_many({})

    # Display the number of documents in the collection contacts
    logging.info("Number of documents in contacts [after remove all]: %d" % contacts.count_documents({}))

    # Drop the collection contacts
    contacts.drop()

    # Display all the collections in the database mydb
    logging.info("List of collections: %s" % db.list_collection_names())

The following are brief descriptions for some of the function(s) used in the code segment above:

The following is the full Python program called Mongo-1.py that performs all the operations we executed through the command-line interface in Part-1 :


Mongo-1.py
#
# Author: Bhaskar S
# Blog:   https://www.polarsparc.com
# Date:   30 Jan 2021
#

import logging
import pymongo
from pymongo.errors import ConnectionFailure

logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)


# Open connection to MongoDB cluster
def connect_to_db():
    # db_url = 'mongodb://192.168.1.53:5001,192.168.1.53:5002,192.168.1.53:5003/mydb?replicaSet=mongodb-rs'
    db_url = 'mongodb://192.168.1.53:5001/mydb'
    client = None
    try:
        client = pymongo.MongoClient(db_url)
        logging.info("Connected to MongoDB @ %s" % db_url)
    except ConnectionFailure as e:
        logging.error("Could not connect to MongoDB @ %s: %s" % (db_url, e))
    return client


# Setup the database and the collection
def setup_db_n_collection(client):
    # logging.info("setup ...")

    # Setup the db variable to point to the database mydb
    db = client["mydb"]

    # Setup the contacts variable to point to the collection contacts
    contacts = db["contacts"]

    # Display all the collections in the database mydb
    logging.info("List of collections: %s" % db.list_collection_names())

    return db, contacts


# Insert a document into the collection contacts
def insert_contact_n_check(db, contacts):
    # Insert a document for Alice
    contacts.insert_one({"first": "Alice", "last": "Thompson",
                          "email": {"personal": "alice.t@home.io", "work": "alice.thompson@work.net"},
                          "mobile": {"personal": "123 456 7890"}})

    # Display all the collections in the database mydb
    logging.info("List of collections: %s" % db.list_collection_names())

    # Display the number of documents in the collection contacts
    logging.info("Number of documents in contacts: %d" % contacts.count_documents({}))

    # Query and display all the documents in the collection contacts
    cursor = contacts.find()
    for doc in cursor:
        logging.info(doc)


# Insert five more documents into the collection contacts
def insert_five_contacts(contacts):
    contacts.insert_one({"first": "Bob", "last": "Jones",
                          "email": {"work": "bobj@doktor.net"},
                          "mobile": {"work": "234 567 8901"}})
    contacts.insert_one({"first": "Charlie", "last": "Lee",
                          "email": {"personal": "cl3000@ranch.net"}})
    contacts.insert_many([{"first": "Eve", "middle": "Jo", "last": "Parker",
                            "email": {"work": "ej_parker@awesome.org"},
                            "mobile": {"personal": "345 678 9012"}}])
    contacts.insert_many([{"first": "Frank", "last": "Smith",
                            "email": {"personal": "frank45@root.org", "work": "frank.smith@excellent.net"},
                            "mobile": {"personal": "456 789 0123", "work": "567 890 1234"}}])
    contacts.insert_one({"first": "Frank", "last": "Cooper",
                          "email": {"personal": "frankc@runner.org"}})


# Perform various query operations
def query_contacts(contacts):
    # Query and display all the documents in the collection contacts
    logging.info("Documents in contacts:")
    cursor = contacts.find()
    for doc in cursor:
        logging.info(doc)

    # Query and display the document for first = "Bob" from the collection contacts
    logging.info("Document for Bob:")
    logging.info(contacts.find_one({"first": "Bob"}))

    # Query and display the document for first = "Charlie" and last = "Lee" from the collection contacts
    logging.info("Document for Charlie Lee:")
    logging.info(contacts.find_one({"first": "Charlie", "last": "Lee"}))

    # Query and display the document for email.work = "bobj@doktor.net" from the collection contacts
    logging.info("Document for bobj@doktor.net:")
    logging.info(contacts.find_one({"email.work": "bobj@doktor.net"}))

    # Query and display only first and last for all the documents in the collection contacts
    logging.info("First and Last name(s) in contacts:")
    cursor = contacts.find({}, {"first": 1, "last": 1})
    for doc in cursor:
        logging.info(doc)

    # Query and display only first and last (without _id) for all the documents in the collection contacts
    logging.info("First and Last name(s) [without _id] in contacts:")
    cursor = contacts.find({}, {"first": 1, "last": 1, "_id": 0})
    for doc in cursor:
        logging.info(doc)

    # Query and display only first, last, and mobile.persona (without _id)
    # for all the documents in the collection contacts
    logging.info("First, Last name, Personal Mobile(s) [without _id] in contacts:")
    cursor = contacts.find({}, {"first": 1, "last": 1, "mobile.personal": 1, "_id": 0})
    for doc in cursor:
        logging.info(doc)

    # Query and display only first and last (without _id) for 3 documents in the collection contacts
    logging.info("First and Last name(s) [without _id with limit 3] in contacts:")
    cursor = contacts.find({}, {"first": 1, "last": 1, "_id": 0}).limit(3)
    for doc in cursor:
        logging.info(doc)


# Perform some update operations
def update_contacts(contacts):
    # Update the document for first = "Charlie" in the collection contacts
    contacts.replace_one({"first": "Charlie"}, {"mobile": {"personal": "678 901 2345"}})

    # Query and display the document for first = "Charlie" from the collection contacts
    logging.info("Document for Charlie [after update]:")
    doc = contacts.find_one({"first": "Charlie"})
    if doc:
        logging.info(doc)
    else:
        logging.warning("No document for Charlie !!!")

    # Query and display the document for mobile: { personal: "678 901 2345" } from the collection contacts
    logging.info("Document for Charlie [after update using mobile]:")
    logging.info(contacts.find_one({"mobile": {"personal": "678 901 2345"}}))

    # Update the document for mobile: { personal: "678 901 2345" } (to restore Charlie) in the collection contacts
    contacts.replace_one({"mobile": {"personal": "678 901 2345"}},
                          {"first": "Charlie", "last": "Lee",
                          "email": {"personal": "cl3000@ranch.net"},
                          "mobile": {"personal": "678 901 2345"}})

    # Query and display the document for first = "Charlie" from the collection contacts (after fix)
    logging.info("Document for Charlie [after update fix]:")
    logging.info(contacts.find_one({"first": "Charlie"}))

    # Query and display the document for first = "Charlie" from the collection contacts (after fix)
    logging.info("Document for Charlie [after update fix]:")
    logging.info(contacts.find_one({"first": "Charlie"}))
  
    # Upsert the document for first = "George" in the collection contacts
    contacts.replace_one({"first": "George"}, {"first": "George", "last": "Baker",
                                               "email": {"work": "g_baker@crap.org"},
                                               "mobile": {"work": "789 012 3456"}},
                         upsert=True)

    # Query and display the document for first = "George" from the collection contacts (after upsert)
    logging.info("Document for George [after upsert]:")
    logging.info(contacts.find_one({"first": "George"}))


# Perform some delete operations
def remove_n_drop_contacts(db, contacts):
    # Delete the document for first = "Bob" from the collection contacts
    contacts.delete_one({"first": "Bob"})

    # Query and display all the documents in the collection contacts
    logging.info("Documents in contacts [after removing Bob]:")
    cursor = contacts.find()
    for doc in cursor:
        logging.info(doc)

    # Delete all the remaining documents for the collection contacts
    contacts.delete_many({})

    # Display the number of documents in the collection contacts
    logging.info("Number of documents in contacts [after remove all]: %d" % contacts.count_documents({}))

    # Drop the collection contacts
    contacts.drop()

    # Display all the collections in the database mydb
    logging.info("List of collections: %s" % db.list_collection_names())


# Clean resources
def clean_up(client):
    # Close the database connection
    client.close()


# Main
def main():
    logging.info("Ready to start ...")

    client = connect_to_db()
    db, contacts = setup_db_n_collection(client)
    insert_contact_n_check(db, contacts)
    insert_five_contacts(contacts)
    query_contacts(contacts)
    update_contacts(contacts)
    remove_n_drop_contacts(db, contacts)
    clean_up(client)

    logging.info("Done !!!")


if __name__ == '__main__':
    main()

Ensure the MongoDB cluster is up and running and mongodb-n1 is the primary. Open a terminal window and execute the following commands:

$ cd $HOME/python/mongodb

$ python3 Mongo-1.py

The following will be the typical output:

Output.2

2021-01-30 20:03:56,519 - Ready to start ...
2021-01-30 20:03:56,522 - Connected to MongoDB @ mongodb://192.168.1.53:5001/mydb
2021-01-30 20:03:56,525 - List of collections: []
2021-01-30 20:03:56,538 - List of collections: ['contacts']
2021-01-30 20:03:56,539 - Number of documents in contacts: 1
2021-01-30 20:03:56,540 - {'_id': ObjectId('601601fcbf000118d66bcd00'), 'first': 'Alice', 'last': 'Thompson', 'email': {'personal': 'alice.t@home.io', 'work': 'alice.thompson@work.net'}, 'mobile': {'personal': '123 456 7890'}}
2021-01-30 20:03:56,545 - Documents in contacts:
2021-01-30 20:03:56,546 - {'_id': ObjectId('601601fcbf000118d66bcd00'), 'first': 'Alice', 'last': 'Thompson', 'email': {'personal': 'alice.t@home.io', 'work': 'alice.thompson@work.net'}, 'mobile': {'personal': '123 456 7890'}}
2021-01-30 20:03:56,546 - {'_id': ObjectId('601601fcbf000118d66bcd01'), 'first': 'Bob', 'last': 'Jones', 'email': {'work': 'bobj@doktor.net'}, 'mobile': {'work': '234 567 8901'}}
2021-01-30 20:03:56,546 - {'_id': ObjectId('601601fcbf000118d66bcd02'), 'first': 'Charlie', 'last': 'Lee', 'email': {'personal': 'cl3000@ranch.net'}}
2021-01-30 20:03:56,546 - {'_id': ObjectId('601601fcbf000118d66bcd03'), 'first': 'Eve', 'middle': 'Jo', 'last': 'Parker', 'email': {'work': 'ej_parker@awesome.org'}, 'mobile': {'personal': '345 678 9012'}}
2021-01-30 20:03:56,546 - {'_id': ObjectId('601601fcbf000118d66bcd04'), 'first': 'Frank', 'last': 'Smith', 'email': {'personal': 'frank45@root.org', 'work': 'frank.smith@excellent.net'}, 'mobile': {'personal': '456 789 0123', 'work': '567 890 1234'}}
2021-01-30 20:03:56,547 - {'_id': ObjectId('601601fcbf000118d66bcd05'), 'first': 'Frank', 'last': 'Cooper', 'email': {'personal': 'frankc@runner.org'}}
2021-01-30 20:03:56,547 - Document for Bob:
2021-01-30 20:03:56,547 - {'_id': ObjectId('601601fcbf000118d66bcd01'), 'first': 'Bob', 'last': 'Jones', 'email': {'work': 'bobj@doktor.net'}, 'mobile': {'work': '234 567 8901'}}
2021-01-30 20:03:56,548 - Document for Charlie Lee:
2021-01-30 20:03:56,548 - {'_id': ObjectId('601601fcbf000118d66bcd02'), 'first': 'Charlie', 'last': 'Lee', 'email': {'personal': 'cl3000@ranch.net'}}
2021-01-30 20:03:56,548 - Document for bobj@doktor.net:
2021-01-30 20:03:56,549 - {'_id': ObjectId('601601fcbf000118d66bcd01'), 'first': 'Bob', 'last': 'Jones', 'email': {'work': 'bobj@doktor.net'}, 'mobile': {'work': '234 567 8901'}}
2021-01-30 20:03:56,549 - First and Last name(s) in contacts:
2021-01-30 20:03:56,550 - {'_id': ObjectId('601601fcbf000118d66bcd00'), 'first': 'Alice', 'last': 'Thompson'}
2021-01-30 20:03:56,550 - {'_id': ObjectId('601601fcbf000118d66bcd01'), 'first': 'Bob', 'last': 'Jones'}
2021-01-30 20:03:56,550 - {'_id': ObjectId('601601fcbf000118d66bcd02'), 'first': 'Charlie', 'last': 'Lee'}
2021-01-30 20:03:56,550 - {'_id': ObjectId('601601fcbf000118d66bcd03'), 'first': 'Eve', 'last': 'Parker'}
2021-01-30 20:03:56,550 - {'_id': ObjectId('601601fcbf000118d66bcd04'), 'first': 'Frank', 'last': 'Smith'}
2021-01-30 20:03:56,550 - {'_id': ObjectId('601601fcbf000118d66bcd05'), 'first': 'Frank', 'last': 'Cooper'}
2021-01-30 20:03:56,551 - First and Last name(s) [without _id] in contacts:
2021-01-30 20:03:56,551 - {'first': 'Alice', 'last': 'Thompson'}
2021-01-30 20:03:56,551 - {'first': 'Bob', 'last': 'Jones'}
2021-01-30 20:03:56,552 - {'first': 'Charlie', 'last': 'Lee'}
2021-01-30 20:03:56,552 - {'first': 'Eve', 'last': 'Parker'}
2021-01-30 20:03:56,552 - {'first': 'Frank', 'last': 'Smith'}
2021-01-30 20:03:56,552 - {'first': 'Frank', 'last': 'Cooper'}
2021-01-30 20:03:56,552 - First, Last name, Personal Mobile(s) [without _id] in contacts:
2021-01-30 20:03:56,553 - {'first': 'Alice', 'last': 'Thompson', 'mobile': {'personal': '123 456 7890'}}
2021-01-30 20:03:56,553 - {'first': 'Bob', 'last': 'Jones', 'mobile': {}}
2021-01-30 20:03:56,553 - {'first': 'Charlie', 'last': 'Lee'}
2021-01-30 20:03:56,554 - {'first': 'Eve', 'last': 'Parker', 'mobile': {'personal': '345 678 9012'}}
2021-01-30 20:03:56,554 - {'first': 'Frank', 'last': 'Smith', 'mobile': {'personal': '456 789 0123'}}
2021-01-30 20:03:56,554 - {'first': 'Frank', 'last': 'Cooper'}
2021-01-30 20:03:56,554 - First and Last name(s) [without _id with limit 3] in contacts:
2021-01-30 20:03:56,555 - {'first': 'Alice', 'last': 'Thompson'}
2021-01-30 20:03:56,555 - {'first': 'Bob', 'last': 'Jones'}
2021-01-30 20:03:56,555 - {'first': 'Charlie', 'last': 'Lee'}
2021-01-30 20:03:56,556 - Document for Charlie [after update]:
2021-01-30 20:03:56,557 - No document for Charlie !!!
2021-01-30 20:03:56,557 - Document for Charlie [after update using mobile]:
2021-01-30 20:03:56,558 - {'_id': ObjectId('601601fcbf000118d66bcd02'), 'mobile': {'personal': '678 901 2345'}}
2021-01-30 20:03:56,558 - Document for Charlie [after update fix]:
2021-01-30 20:03:56,559 - {'_id': ObjectId('601601fcbf000118d66bcd02'), 'first': 'Charlie', 'last': 'Lee', 'email': {'personal': 'cl3000@ranch.net'}, 'mobile': {'personal': '678 901 2345'}}
2021-01-30 20:03:56,559 - Document for George [after upsert]:
2021-01-30 20:03:56,560 - {'_id': ObjectId('601ca3ed8d37d50b59288cff'), 'first': 'George', 'last': 'Baker', 'email': {'work': 'g_baker@crap.org'}, 'mobile': {'work': '789 012 3456'}}
2021-01-30 20:03:56,560 - Documents in contacts [after removing Bob]:
2021-01-30 20:03:56,561 - {'_id': ObjectId('601601fcbf000118d66bcd00'), 'first': 'Alice', 'last': 'Thompson', 'email': {'personal': 'alice.t@home.io', 'work': 'alice.thompson@work.net'}, 'mobile': {'personal': '123 456 7890'}}
2021-01-30 20:03:56,561 - {'_id': ObjectId('601601fcbf000118d66bcd02'), 'first': 'Charlie', 'last': 'Lee', 'email': {'personal': 'cl3000@ranch.net'}, 'mobile': {'personal': '678 901 2345'}}
2021-01-30 20:03:56,561 - {'_id': ObjectId('601601fcbf000118d66bcd03'), 'first': 'Eve', 'middle': 'Jo', 'last': 'Parker', 'email': {'work': 'ej_parker@awesome.org'}, 'mobile': {'personal': '345 678 9012'}}
2021-01-30 20:03:56,561 - {'_id': ObjectId('601601fcbf000118d66bcd04'), 'first': 'Frank', 'last': 'Smith', 'email': {'personal': 'frank45@root.org', 'work': 'frank.smith@excellent.net'}, 'mobile': {'personal': '456 789 0123', 'work': '567 890 1234'}}
2021-01-30 20:03:56,561 - {'_id': ObjectId('601601fcbf000118d66bcd05'), 'first': 'Frank', 'last': 'Cooper', 'email': {'personal': 'frankc@runner.org'}}
2021-01-30 20:03:56,561 - {'_id': ObjectId('601ca3ed8d37d50b59288cff'), 'first': 'George', 'last': 'Baker', 'email': {'work': 'g_baker@crap.org'}, 'mobile': {'work': '789 012 3456'}}
2021-01-30 20:03:56,563 - Number of documents in contacts [after remove all]: 0
2021-01-30 20:03:56,564 - List of collections: []
2021-01-30 20:03:56,565 - Done !!!

AWESOME !!! We have successfully demonstrated the use of the Python MongoDB driver using an example code.

References

Mongo Java Driver Documentation

PyMongo Documentation

Hands-on MongoDB :: Part-1

MongoDB Manual



© PolarSPARC