Mongodb Basics


Micro services are popular. I also like mongodb more and more. Unless MySQL is necessary, I prefer mongodb.


What is MongoDB?

Mongodb is written in C + +, which is an open source database system based on distributed file storage.

In the case of high load, adding more nodes can guarantee the server performance.

Mongodb aims to provide scalable high-performance data storage solutions for web applications.

Mongodb stores the data as a document, and the data structure consists of key value pairs. Mongodb documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays.

Mongodb features

  • Mongodb is a document storage oriented database, which is easy to operate.
  • You can set the index of any attribute in the mongodb record (such as: firstname = “Sameer”, address = “8 Gandhi road”) to achieve faster sorting.
  • You can create data image through local or network, which makes mongodb more scalable.
  • If the load increases (requires more storage space and processing power), it can be distributed on other nodes in the computer network, which is called fragmentation.
  • Mongo supports rich query expressions. The query instruction uses JSON tags, which can easily query the embedded objects and arrays in the document.
  • Mongodb uses the update () command to replace the completed document (data) or some specified data fields.
  • Map / reduce in mongodb is mainly used for batch processing and aggregation of data.
  • Map and reduce. The map function calls emit (key, value) to traverse all the records in the collection and pass the key and value to the reduce function for processing.
  • Map function and reduce function are written in JavaScript, and MapReduce operation can be performed through db.runcommand or MapReduce command.
  • Gridfs is a built-in function in mongodb, which can be used to store a large number of small files.
  • Mongodb allows scripts to be executed on the server side. You can write a function in JavaScript, execute it directly on the server side, or store the definition of the function on the server side, and call it directly next time.
  • Mongodb supports various programming languages: Ruby, python, Java, C + +, PHP, C ා and other languages.
  • Mongodb is easy to install.

Comparison between mongodb and other databases

Mongodb and MySQL

Because mongodb’s unique data processing method can load hot data into memory, it will be very fast for query (of course, it will also consume memory); at the same time, because it uses bson to store data, it has very good support for JSON format data and friendly table structure modification, document storage, data friendly and visible; data The partition cluster load of the library has very good scalability and very good automatic failover (great praise).

Shortage: the database query adopts a unique query method, which has a certain learning cost (not high); the index is not very small; the lock can only be provided to the collection level, but not to the row level; there is no transaction mechanism (can’t roll back); the learning materials are certainly not as much as mysql.

The difference between mongodb and Hadoop

Mongodb focuses on the application system of data operation, while Hadoop focuses on the application of data analysis and statistics.

Mongodb can meet the application scenarios (memory consuming) with high requirements for database read-write performance. Generally, the response delay of these applications will be controlled below 10ms, or even lower. Because each reading and writing operation of Hadoop will contain a large amount of data (Hadoop is more suitable for scenarios with few operations and large quantities of data), a large amount of data will be processed through aggregate analysis, which generally takes MapReduce, resulting in a high delay (from minutes to hours)

Mongodb query

Query syntax

db.collection.find(query, projection).pretty()

Collection: the name of the document, or it can be written asgetCollection('col_name')

Query: optional. Use the query operator to specify the query criteria

Projection: optionally, use the projection operator to specify the key returned. When querying, all the key values in the document are returned. You only need to omit this parameter (omitted by default).

Pretty(): easy to read

Conditional grammar

operation format Example
Be equal to {<key>:<value>} DB. Col. find ({"by": "rookie tutorial"}). Pretty ()
less than {<key>:{$lt:<value>}} db.col.find({"likes":{$lt:50}}).pretty()
Less than or equal to {<key>:{$lte:<value>}} db.col.find({"likes":{$lte:50}}).pretty()
greater than {<key>:{$gt:<value>}} db.col.find({"likes":{$gt:50}}).pretty()
Greater than or equal to {<key>:{$gte:<value>}} db.col.find({"likes":{$gte:50}}).pretty()
Not equal to {<key>:{$ne:<value>}} db.col.find({"likes":{$ne:50}}).pretty()

AND syntax

db.col.find({key1:value1, key2:value2}).pretty()

OR syntax

      $or: [
         {key1: value1}, {key2:value2}

AND and OR

        key1: value1, 
        $or: [
            {key2: value2},
            {key3: value3}


This parameter specifies the number of records read from mongodb.



Skip the specified amount of data






For example: now we use the above set to calculate the number of articles written by each author, and use aggregate() to calculate the results as follows:

db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])

Same as SQL:

select by_user, count(*) from mycol group by by_user

The Conduit

Pipes are commonly used in UNIX and Linux to take the output of the current command as a parameter to the next command.

The aggregation pipeline of mongodb will pass the results of mongodb documents to the next pipeline after one pipeline is processed. Pipeline operations are repeatable.

Expression: processes the input document and outputs it. The expression is stateless and can only be used to calculate the documents of the current aggregation pipeline, and cannot process other documents.

Here we introduce some common operations in the aggregation framework:

  • $project: modify the structure of the input document. It can be used to rename, add, or delete fields, or to create calculation results and nested documents.
  • $match: used to filter data and output only qualified documents. $match uses mongodb’s standard query operations.
  • $limit: used to limit the number of documents returned by the mongodb aggregation pipeline.
  • $skip: skips the specified number of documents in the aggregation pipeline and returns the remaining documents.
  • $unwind: split an array type field in the document into multiple fields, each containing a value in the array.
  • $group: Group documents in the collection, which can be used for statistics.
  • $sort: sort the input documents and output them.
  • $geonear: output ordered documents close to a geographic location.