Pay attention to the public number [Ccww technology blog], get dry goods information
MongoDB is a database based on distributed file storage, written by C++ language. MongodDB is designed to provide scalable, high-performance data storage solutions for WEB applications. MongodDB is a cross between a relational database and a non-relational database.
Due to the features and functions of MongoDB, it is used frequently in enterprises, so many interviews will be related to MongoDB knowledge, based on the online and read the official website documents to summarize the 2019-2020 MongoDB interview questions. Details are as follows:
1 q:MongoDB
What are the advantages of?
-
For Collection and document storage, save data in JSON format documents.
-
High performance, support Document embedded Document reduces I/O operations on the database system and has complete index support, support for fast query
-
Efficient traditional storage: supports binary data and large objects
-
High availability, data replication set, MongoDB database supports data replication between servers to provide automatic failover.
-
With high scalability, sharding distributes data in multiple data centers, and MongoDB supports creating data regions based on sharding keys.
-
· Rich Queries, aggregations Pipeline, Text Search and Geospatial Queries
-
Support multiple storage engines,WiredTiger storage, in-memory storage engine
2 q:MongoDB
What data types are supported?
Java analogous data types:
type | parsing |
---|---|
String |
A string. Data types commonly used to store data. inMongoDB ,UTF-8 Encoded strings are legal |
Integer |
Integer value. Used to store values. Depending on the server you use, it can be 32-bit or 64-bit |
Double |
A double precision floating point value. Used to store floating point values |
Boolean |
Boolean value. Used to store Boolean values (true/false) |
Arrays |
Use to store an array or list or multiple values as a key |
Datetime |
Record the time when the document was modified or added |
MongoDB specific data types:
type | parsing |
---|---|
ObjectId |
For storing documentsid .ObjectId Is based on a distributed primary key implementationMongoDB Sharding can also continue to be used |
Min/Max Keys |
Compare a value to the lowest and highest values of a BSON (binary JSON) element |
Code |
Used for storage in documentsJavaScript code |
Regular Expression |
Use to store regular expressions in documents |
Binary Data |
Binary data. Used to store binary data |
Null |
Used to create null values |
Object |
For inline documents |
3Q: What is a setCollection
, documents,Document
, and the analogy with relational database terminology.
- A collection of
Collection
MongoDB document in a separate databaseDocument
Collection, which is similar to tables in a relational database (RDBMS)Table
. A collectionCollection
Multiple documents inDocument
You can have multiple different fields. In general, setCollection
The documents in theDocument
It has the same meaning. - The document
Document
The value consists of key-value. The documentDocument
Is dynamic mode, which means that documents in the same collection do not need to have the same fields and structure. Similar to each record in a table in a relational database. - Analogy with relational database terminology
mongodb | Relational database |
---|---|
Database | Database |
Collection | Table |
Document | Record/Row |
Filed | Column |
Embedded Documents | Table join |
4Q: What is”Mongod
“, as well asMongoDB
Command.
Mongod is the main process that handles the MongoDB system. It handles data requests, manages data stores, and performs background administrative operations. When we run the mongod command, we are starting the MongoDB process and running it in the background.
Directing a command:
The command | instructions |
---|---|
use database_name | Switching databases |
db.myCollection.find().pretty() | Format print results |
db.getCollection(collectionName).find() | Changing the Collection name |
5 q:”Mongod
“What are the default parameters?
- Pass the database storage path, default is
"/data/db"
- The default port number is 27017.
6 q:MySQL
andmongodb
The difference between
In the form of | MongoDB | MySQL |
---|---|---|
Database model | non-relational | relational |
storage | Virtual memory + persistence | |
The query | Unique MongoDB query method | Traditional SQL statements |
Architectural features | Replica sets and sharding | Common single point, M-S, MHA, MMM and other architecture |
Data processing mode | Based on memory, hot data is stored in physical memory to achieve high-speed read and write | Different engines have their own characteristics |
Usage scenarios | Event logging, content management, or blogging platforms with large and unstructured data | This applies to structured data with a small amount of data |
7 q: qmongodb
andredis
Differences and reasons for selection
In the form of | MongoDB | redis |
---|---|---|
Memory management mechanism | MongoDB data is stored in memory, which is implemented by MMAP in Linux. If the memory is insufficient, only hotspot data is stored in the memory and other data is stored on disks | All Redis data is stored in the memory and periodically written to the disk. When the memory is insufficient, you can select the specified LRU algorithm to delete the data |
Supported data structures | MongoDB data structure is relatively single, but support rich data expression, index | Redis supports rich data structures, including hash, set, list, and so on |
performance | Mongodb relies on memory and has a high TPS | Redis is memory dependent and TPS is very high. Redis is superior to MongoDB in performance |
reliability | Support for persistence and replication sets for increased reliability | Redis relies on snapshots for persistence; AOF enhances reliability; Enhance reliability while affecting access performance |
The data analysis | Mongodb built-in data analysis (MapReduce) | Redis does not support |
Transaction support | Only single-document transactions are supported and scenarios requiring complex transaction support are not suitable for the time being | Redis transaction support is weak and can only ensure that each operation in a transaction is executed consecutively |
The cluster | MongoDB cluster technology is relatively mature | Redis has supported clustering since 3.0 |
Reasons for selection:
-
Architecture is simple
-
No complex connections
-
Deep query ability,MongoDB supports dynamic query.
-
Easier to debug
-
Easy to extend
-
There is no need to convert/map application objects to database objects
-
Use internal memory as a storage workspace for faster data access.
8Q: How do I perform transactions/locks?
Mongodb does not use traditional locks or complex roll-back transactions because it is designed to be lightweight, fast, and predictably high performance. It can be likened to the automatic commit mode of mysql Mylsam. By streamlining transaction support, performance is improved, especially in a system that may traverse multiple servers.
9Q: Does the update operation fsync to disk immediately?
No, disk writes are deferred by default. The write operation may reach the disk after two or three seconds (within 60 seconds by default). This can be configured using the syncPeriodSecs startup parameter. For example, if the database receives a thousand incrementing operations on an object in a second, the disk is flushed only once.
Directing index
10Q: What are the index types?
- Single field index (
Single Field Indexes
) - Composite index (
Compound Indexes
) - Multi-key index (
Multikey Indexes
) - Full-text Index (
text Indexes
) - A Hash index (
Hash Indexes
) - Wildcard index (
Wildcard Index
) - 2 dsphere index (
2dsphere Indexes
)
11 q:MongoDB
Create index on A:{B,C}, query A:{B,C} and A:{C,B} use index?
Because the MongoDB index uses the B-tree principle, it only uses the index on A:{B,C}
Find out more about the MongoDB index in the MongoDB series, which includes a number of index questions:
- Create index, need to consider the issues
- Index limitation problem
- Index type detailed parsing
- Type of index problem
12Q: What is aggregation
Aggregation operations can process data records and return computed results. Aggregation operations can combine values from multiple documents, perform various operations on groups of data, and return a single result. It is equivalent to the count(*) group by in SQL. For aggregate operations in MongoDB, the aggregate() method should be used.
See the MongoDB series — In-depth Understanding of MongoDB Aggregation for details, which includes many Aggregation issues:
- Polymerization pipe (
aggregation pipeline
The problem of) Aggregation Pipeline
Optimization and so on- The map-reduce function is faulty
Directing a shard
Q:monogodb
The shardsharding
Sharding is the horizontal partitioning of data to different physical nodes. As the application data gets bigger and bigger, so does the amount of data. As the data volume grows, a single machine may not be able to store the data or have an acceptable read and write throughput. Sharding allows more machines to be added to cope with increased data volumes and read and write operations.
14Q: Sharding (Shard
) and copy (replication
) How does it work?
Each shard is a logical collection of partitioned data. Shards may consist of a single server or cluster, and we recommend clustering for each shard.
15Q: If the block moves (moveChunk
) failed, do I need to manually clear some of the transferred documents?
No, moves are consistent and deterministic.
- After a failure, the move operation is constantly retried.
- When finished, the data will only appear in the new shard.
16Q: When does data scale to multiple shards (Shard
)?
MongoDB sharding is range based. So all objects in a collection are stored in a chunk, and the default chunk size is 64Mb. A migration is only possible when the data size exceeds 64 Mb, and only when there is more than one block is there an option to fragment data.
17Q: What happens when you update a document on a Chunk that is being migrated?
Updates occur immediately on the old Chunk, and changes are copied to the new shard before ownership is transferred.
18Q: What happens if a Shard stops or is slow and initiates a query?
If a shard stops, the query will return an error unless the Partial option is set. If a shard is slow to respond, MongoDB waits for its response.
Directing a replicate set
Q:MongoDB
Replica sets implement the principle of high availability
MongoDB uses its Replica Set scheme to realize automatic fault tolerance mechanism, which provides the basis for high availability. Currently, MongoDB supports two replication modes:
Master
/Slave
, primary/secondary replication, and roles includeMaster
和Slave
。Replica Set
, replication set replication, roles includePrimary
和Secondary
As well asArbiter
. (Production environment This parameter is mandatory)
20Q: What is master or primary?
Replica sets only one master node can acknowledge writes to receive all writes and record all changes to the data set in its operation log (recorded in Oplog). In a cluster, when the master node fails, the Secondary node becomes master
21Q: What is itSlave
orSecondary
?
Copy the oplog of the master node and apply the operations recorded by oplog to its data set. If the master node goes down, a new master node will be elected from eligible slave nodes.
22 q: what isArbiter
?
Quorum nodes do not maintain datasets. The purpose of the quorum node is to maintain quorum in the replica set by responding to heartbeat and election requests from other replica set nodes
23Q: What are the node types of the replication set?
- Priority type 0 (
Priority 0
) node - Hidden type (
Hidden
) node - Delay (
Delayed
) node - Vote (
Vote
) nodes and non-voting nodes
24Q: How long does it take to enable backup failover?
It will take 10 to 30 seconds from the time the backup database declares that the primary database is down to the time a backup database is selected as the new primary database. Operations on the primary database during this period will fail — including writes and strong Consistent read operations. However, you can also perform a final consistent query (in SlaveOK mode) on the second database, even during this time.
For detailed analysis of MongoDB replication, see the article “MongoDB Series – Solving replica Set Problems you May Encounter in interviews”.
Q 25:raft
The election process, the voting rules?
Election process:
After the system is started, the initial post-election system consists of one Leader and several followers. Then, the Leader service is abnormal due to an exception. As a result, the last RPC update time between the Follower role and the Leader exceeds the specified threshold. At this point, the Follower will think that the Leader service is abnormal, and then it will initiate a new Leader election behavior and change its status from Follower to Candidate. Other followers are then asked to vote for themselves.
Voting rules:
- A candidate becomes leader when he or she receives a majority of the votes for the same term number.
- Each node can cast a maximum of one vote in one term. And on a first come, first served basis.
- Once a candidate wins an election, he or she becomes the leader and sends a heartbeat to maintain authority while preventing the emergence of a new leader
See article [Easy to Understand Paxos algorithm – Consistency Algorithm based on messaging]
26Q: Used in which scenariosMongoDB
?
Rule: Do not use MongoDB databases if there are a lot of complex transaction logic operations in your business; MongoDB is used for processing unstructured/semi-structured big data, and also for dynamic data operations, such as:
- Content management system, section data, log recording
- The mobile terminal
Apps
:O2O
Information of Courier riders and Courier vendors (including location information) - Data management, monitoring data
Is everyone still ok? If you like, move your hands to show 💗, point a concern!! Thanks for your support!
Welcome to pay attention to the public number [Ccww technology blog], original technical articles launched at the first time