MongoDB Quick Start Guide and docker-compose Quick Experience

Advantages of MongoDB over RDBMS

  1. Less Schemas - MongoDB is a document database where a collection contains different documents.The number of fields, content, and document size may vary between documents.
  2. The structure of a single object is clear.
  3. There are no complex joins.
  4. In-depth query capabilities.MongoDB supports dynamic querying of documents using a document-based query language almost as powerful as SQL.
  5. SQL Tuning (Optimized)
  6. Easy to expand
  7. There is no need to convert/map application objects to database objects.
  8. Use internal memory to store (window-style) working sets so that data can be accessed more quickly

RDBMS: Relational Database Management System

Why use MongoDB

  1. Document-oriented storage, BSON format storage, or Binary JSON
  2. Single-key index, composite index, multikey index, geospatial index, full-text index, and hash index
  3. MongoDB implements highly available master-slave replication 
  4. Auto-slicing auto sharding 
  5. Rich queries 
  6. Fast in-place updates: Most update operations do not require new space
  7. Professional support for MongoDB map/reduce support
  8. Gridfs: Cluster file support of various size sizes

Where to use MongoDB

  1. Big data
  2. Content management and delivery
  3. Mobile and Social Infrastructure
  4. User data management
  5. Data Center

Doker-compose Quick Start MongoDB

docker-compose.yml

version: '3'
    services:
      mongodb:
        image: mongo:4.2.6 # Mirror: Version
        container_name: mongo_db
        environment:
          - MONGO_INITDB_DATABASE=Default database
          - MONGO_INITDB_ROOT_USERNAME=Your root Administrator Name
          - MONGO_INITDB_ROOT_PASSWORD=Your root Administrator name password
        volumes:
          - ./mongo/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
          - ./mongo/mongo-volume:/data/db
        ports:
          - "27017-27019:27017-27019"
        restart: always

init-mongo.js

// db.getSiblingDB() is equivalent to use admin;
db.getSiblingDB('admin')
    .createUser({
        user: 'user',
        pwd: 'user',
        roles: ['readWrite']
});

Then execute the command

docker-compose up -d

Navicat Premium Connection


Fill in the parameters for docker-compose.yml above
Test connection, test successful

If you don't show it, go

navicat == Menu== Show== Show hidden items on the hook

Restart navicat

Open a collection to view in the lower right corner

  • Grid View
  • Tree View
  • JSON View

crud

1. Create Action

  • db.collection.insertOne()
  • db.collection.insertMany()

// Insert a collection named products and insert data (new if no collection exists)
db.products.insertOne({
    item: "card",
    qty: 15
});

// Bulk Insert Data
db.products.insertMany([{
    item: "card1",
    qty: 16
}, {
    item: "card2",
    qty: 17
}, {
    item: "envelope",
    qty: 20
}, {
    item: "stamps",
    qty: 30
}]);

2. Query operation

  • db.collection.find()

db.products.find({ qty: 15 }).limit(1)

3. Update operation

  • db.collection.updateOne()

db.collection.updateOne(filter,update,options)

Find the first document that matches the filter and apply the specified update changes.

db.products.find({
    item: "stamps"
}).limit(1);

// Find item as stamps and modify its qty to 60
db.products.updateOne(
    {
        "item": "stamps"
    },
    {
        $set: {
            "qty": 60
        }
    }
);

db.products.find({
    item: "stamps"
}).limit(1);
  • db.collection.updateMany()

// Modify qty greater than 15 to 15
db.products.updateMany(
    {
        qty: {
            $gt: 15
        }
    },
    {
        $set: {
            "qty": 15
        }
    }
);

  • db.collection.replaceOne()

Replace the first matching document in the collection that matches the filter with a replacement document.

// Replace item in collection products with stamps as a whole 
 db.products.replaceOne(
    { "item" : "stamps" },
    { "item" : "stampss", "Borough" : "Manhattan" }
 );

4. Delete operation

 // Delete item s that are stampss
 db.products.deleteOne( { "item" : "stampss" } );
 // Batch deletion of qty less than 15
 db.products.deleteMany( { "qty" : {$lt: 15} } );

Continuous update of connection 

Tags: Java MongoDB Database Docker Navicat

Posted on Thu, 14 May 2020 12:58:34 -0400 by thinkgfx