MongoDB Learning Notes - Introduction to MongoDB, Data Types and Help Commands

MongoDB Learning Notes (1) - Introduction to MongoDB and Data Types

The version of MongoDB used in this article is 4.0.10

> db.version();

Introduction to MongoDB

1. Characteristics of MongoDB

MongoDB is a scalable and high-performance NoSQL database written in C++ language. It aims to provide a high-performance scalable data storage solution for web applications.
It is characterized by high performance, easy deployment, easy use and convenient storage of data.

  • Free mode, support dynamic query, complete index, can easily query embedded objects and arrays in documents.
  • Collection-oriented storage, easy to store object-type data, including document embedded objects and arrays.
  • Efficient data storage, support binary data and large objects (such as photos and videos).
  • Supports replication and fault recovery; provides data replication between master-slave and master-master modes and data replication between servers.
  • Automatic fragmentation supports scalability at the cloud level, supports horizontal database clusters, and dynamically adds additional servers.

2. Advantages and applicable scenarios of MongoDB

Advantages of MongoDB

  • High performance, very fast (if you have enough memory).
  • There is no fixed table structure and data migration is not necessary to modify the table structure.
  • The query language is simple and easy to use.
  • Use Sharing to achieve horizontal scaling.
  • It is easy to deploy.

Application scenarios for MongoDB

  • A persistent cache layer suitable for use as an information infrastructure.
  • It is suitable for real-time insertion, update and query, and has the replication and high scalability required for real-time data storage of applications.
  • Mongo's BSON data format is very suitable for storage and query of document format.
  • Suitable for a database consisting of dozens or hundreds of servers. Because Mongo already includes built-in support for the MapReduce engine.

Comparison of SQL and NoSQL

- SQL database NoSQL database
type All types support the SQL standard There are many types, such as document storage, key value storage, column database, etc.
Example MySQL,SQL Server,Oracle MongoDB,HBase,Cassandra
Data Storage Model Data is stored in rows and columns of a table, each of which has a specific type.
Tables are usually created according to standardization principles.
Use joins to retrieve data from multiple tables.
The data model depends on the type of database.
For example, data is stored as key-value pairs for key-value storage. In a document-based database, data is stored as documents.
NoSQL's data model is flexible, contrary to the rigid table model of the SQL database.
Pattern Fixed structure and schema, so any change to the schema involves modifying the database Dynamic schemas can adapt to new data types or structures by extending or modifying the current schema.
New fields can be added dynamically
Scalability Vertical expansion is used. This means that as the load increases, you need to buy larger, more expensive servers to accommodate data. Horizontal expansion is used. This means that the data load can be spread over multiple inexpensive servers.
Support services Support ACID and transaction Supporting partitioning and availability can compromise transactions.
Transactions exist at a level, such as database level or document level.
Uniformity Strong consistency It depends on the product. Some product choices provide strong consistency, while others provide final consistency.
Query function It can be used through an easy-to-use GUI interface Queries may require programming expertise and knowledge. Unlike UI, its focus is on functions and programming interfaces.

The Correspondence between MongoDB and Mysql Concepts

mongodb mysql
datebase datebase
collection table
document row
field Column/field
Indexes Indexes
Embedding and Reference Table join

3. Data types supported by MongoDB

1. null

Null is used to represent null or non-existent fields

{ "x" : null }

2. Boolean

Boolean types have two values true and false

{ "x": true }

3. 32-bit integers

This type is not supported in Mono Shell. JavaScript only supports 64-bit floating-point numbers, so 32-bit integers are automatically converted to 64-bit floating-point numbers.

4. 64-bit integers

This type is not supported in Mono Shell. Mongo Shell uses a special embedded document to display 64-bit integers.

5. 64-bit floating point number

Numbers in Mongo Shell are of this type.

{  "pi" : 3.14 }

There is only one "number" type in JavaScript. Because there are three numeric types in MongoDB (32-bit integers, 64-bit integers, and 64-bit floating-point numbers), the shell must bypass JavaScript restrictions. By default, the numbers in the shell are treated by MongoDB as double-precision numbers. This means that if you get a 32-bit integer from the database, when you modify the document and save it back to the database, the integer is also converted to a floating point number, even if you keep the integer intact. So it's wise to try not to cover the entire document under the shell.

Another problem is that some 64-bit integers are not exactly 64-bit floating-point numbers. So, if you save a 64-bit integer and view it in the shell, it will appear as an embedded document. But the values actually stored in the database are accurate.

32-bit integers can be accurately represented by 64-bit floating-point numbers, so there is nothing special about them.

6. Strings

UTF-8 strings can be represented as string types

{ "x" : "abcde" }

7. Object id

Object id uses 12 bytes of storage space, two hexadecimal digits per byte, a 24-bit string.

{ "_id" : ObjectId() }

8. Date

Date types store timestamps in Bos, not time zones.

{ "d" : new Date() }

9. Regular expressions

Documents can contain regular expressions, using JavaScript's regular expression syntax.

{ "x" : /^abc/i }

10. Code

Documents can contain JavaScript code

{ "x" : function(){/********/} }

11. Arrays

A collection or list of values can be represented as an array

{ "d" : [1,2,3,4,5] }

12. Embedded Documents

Documents can contain other documents, or they can be embedded as values in the parent document.

{ "x" : { "y" : "z" } }

13. undefined

You can also use undefined ((undefined) types in documents (null and undefined are different types in JavaScript).

{ "a" : undefined }

14. Binary data

Binary data can be composed of strings of arbitrary bytes. It can be used to store binary files such as pictures. But it's not available in Mono Shell.

IV. Mongo Shell Help Order

1. System-level help:

> help                    help on db methods             help on collection methods                    sharding helpers                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        # Display all databases
        show dbs                     show database names
        # Display all collections
        show collections             show collections in current database
        # Display all users of the current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database                list objects in collection foo { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell

2. View the operations available on the database:

DB methods:
        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
        db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
        db.auth(username, password)
        db.cloneDatabase(fromhost) - deprecated
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost) - deprecated
        db.createCollection(name, {size: ..., capped: ..., max: ...})
        db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
        db.currentOp() displays currently executing operations in the db
        # Delete the database
        db.eval() - deprecated
        db.fsyncLock() flush data to disk and lock server for backups
        db.fsyncUnlock() unlocks server following a db.fsyncLock()
        db.getCollection(cname) same as db['cname'] or db.cname
        db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
        # View all collections in the current database
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow queries on a replication slave server
        db.getProfilingLevel() - deprecated
        db.getProfilingStatus() - returns if profiling is on and slow threshold
        db.getSiblingDB(name) get the db at the same server as this one
        db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
        db.hostInfo() get details about the server's host
        db.isMaster() check replica primary status
        db.killOp(opid) kills the current operation in the db
        db.listCommands() lists all the db commands
        db.loadServerScripts() loads all the scripts in db.system.js
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into {cmdObj: 1}
        db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
        db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
        db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
        db.setVerboseShell(flag) display extra information in shell output
        db.version() current version of the server

3. View the operations available on the collection: db. Collection name. help()

DBCollection help
        db.user.find().help() - show DBCursor help
        db.user.bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j
        # Number of records in a collection
        db.user.count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS
        db.user.countDocuments( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS
        db.user.estimatedDocumentCount( <optional params> ) - estimate the document count using collection metadata, optional parameters are: maxTimeMS
        db.user.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
        db.user.convertToCapped(maxBytes) - calls {convertToCapped:'user', size:maxBytes}} command
        db.user.createIndexes([keypatterns], <options>)
        # Collection size
        db.user.deleteOne( filter, <optional params> ) - delete first matching document, optional parameters are: w, wtimeout, j
        db.user.deleteMany( filter, <optional params> ) - delete all matching documents, optional parameters are: w, wtimeout, j
        db.user.distinct( key, query, <optional params> ) - e.g. db.user.distinct( 'x' ), optional parameters are: maxTimeMS
        # Delete collections
        db.user.drop() drop the collection
        db.user.dropIndex(index) - e.g. db.user.dropIndex( "indexName" ) or db.user.dropIndex( { "indexKey" : 1 } )
        # Delete all indexes in a collection
        db.user.ensureIndex(keypattern[,options]) - DEPRECATED, use createIndex() instead
        db.user.explain().help() - show explain help
        db.user.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
                                                      e.g. db.user.find( {x:77} , {name:1, x:1} )
        db.user.findOne([query], [fields], [options], [readConcern])
        db.user.findOneAndDelete( filter, <optional params> ) - delete first matching document, optional parameters are: projection, sort, maxTimeMS
        db.user.findOneAndReplace( filter, replacement, <optional params> ) - replace first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument
        db.user.findOneAndUpdate( filter, update, <optional params> ) - update first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument
        db.user.getDB() get DB object associated with collection
        db.user.getPlanCache() get query plan cache associated with collection
        db.user.getIndexes() { key : ..., initial: ..., reduce : ...[, cond: ...] } )
        db.user.insertOne( obj, <optional params> ) - insert a document, optional parameters are: w, wtimeout, j
        db.user.insertMany( [objects], <optional params> ) - insert multiple documents, optional parameters are: w, wtimeout, j
        db.user.mapReduce( mapFunction , reduceFunction , <optional params> )
        db.user.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor
        db.user.replaceOne( filter, replacement, <optional params> ) - replace the first matching document, optional parameters are: upsert, w, wtimeout, j
        db.user.renameCollection( newName , <dropTarget> ) renames the collection.
        db.user.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
        db.user.stats({scale: N, indexDetails: true/false, indexDetailsKey: <index key>, indexDetailsName: <index name>})
        db.user.storageSize() - includes free space allocated to this collection
        db.user.totalIndexSize() - size in bytes of all the indexes
        db.user.totalSize() - storage allocated for all data and indexes
        db.user.update( query, object[, upsert_bool, multi_bool] ) - instead of two flags, you can pass an object with fields: upsert, multi
        db.user.updateOne( filter, update, <optional params> ) - update the first matching document, optional parameters are: upsert, w, wtimeout, j
        db.user.updateMany( filter, update, <optional params> ) - update all matching documents, optional parameters are: upsert, w, wtimeout, j
        db.user.validate( <full> ) - SLOW
        db.user.getShardVersion() - only for use with sharding
        db.user.getShardDistribution() - prints statistics about data distribution in the cluster
        db.user.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter function
        db.user.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set
        db.user.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collection
        db.user.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the collection
        db.user.latencyStats() - display operation latency histograms for this collection

Tags: Javascript Database MongoDB shell

Posted on Tue, 30 Jul 2019 04:08:30 -0700 by daggardan