Introduction to MongoDB and addition, deletion, modification, and query
I. Introduction
MongoDB is written in C ++ and is an open source database system based on distributed file storage. MongoDB is designed to provide scalable, high-performance data storage solutions for WEB applications. MongoDB stores data as a document. 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 data type:
Data Type |
Description |
String |
String. Common data types used to store data. In MongoDB, UTF-8-encoded strings are valid. |
Integer |
Integer value. Used to store numeric values. The server you use can be divided into 32-bit or 64-bit. |
Boolean |
Boolean value. Stores boolean values (true/false ). |
Double |
Double-precision floating point value. Used to store floating point values. |
Min/Max keys |
Compare a value with the minimum and maximum values of BSON (Binary JSON) elements. |
Arrays |
Stores an array, list, or multiple values as one key. |
Timestamp |
Timestamp. Record the document modification or addition time. |
Object |
Embedded document. |
Null |
Creates a null value. |
Symbol |
Symbol. The data type is basically the same as the string type, but the difference is that it is generally used in languages with special symbol types. |
Date |
Date and time. The current date or time is stored in UNIX time format. You can specify your own Date and time: Create a Date object and input the year, month, and day information. |
Object ID |
Object ID. The ID used to create a document. |
Binary Data |
Binary data. Stores binary data. |
Code |
Code type. It is used to store JavaScript code in the document. |
Regular expression |
Regular Expression type. Stores regular expressions. |
Ii. Operations
1. database operation commands
(1) Create a database
Command: use dbname. For example, use test. If the database test exists, select test. If the database test does not exist, create test.
(2) view all databases
Command: show dbs
(3) Delete A Database
Command: db. dropDatabase () to delete the current database
2. Document operation instructions
The document in mongodb is equivalent to the row in the relational database. The data structure BSON and JSON of the document are basically the same.
(1) Insert a document
MongoDB uses the insert () or save () method to insert a document into the collection. The syntax is as follows: db. COLLECTION_NAME.insert (document ).
Example: db. mycol. insert ({name: 'test1', age: 20, sex: 'male'}), where mycol is the set name. If the set does not exist, mongodb will automatically create it.
You can also use db. COLLECTION_NAME.save (document) to insert a document, which is similar to insert. If the _ id field is specified, the _ id document is updated.
(2) update documents
Mongodb uses update or save to update documents. The update syntax is as follows:
Db. collection. update (
<Query>,
<Update>,
{
Upsert: <boolean>,
Multi: <boolean>,
WriteConcern: <document>
}
)
Query: query condition of update;
Update: update objects and some updated operators (such as $, $ inc...) can also be understood as those after the set in the SQL update query;
Upsert: Optional. This parameter indicates whether to insert objNew if no update record exists. true indicates insertion. The default value is false.
Multi: Optional. The default value of mongodb is false. Only the first record found is updated. If this parameter is set to true, all multiple records are updated according to the condition.
WriteConcern: (optional) exception level.
For example, insert a document: db. mycol. insert ({name: 'test1', age: 1, sex: 'male'}), and then perform the update operation: db. mycol. update ({'name': 'test1'}, {$ set: {'sex': 'formale'}) The result is as follows:
If you need to modify multiple documents that meet the conditions, set multi to true. Example: db. mycol. update ({'name': 'test1'}, {$ set: {'sex': 'formale' }}, {multi: true })
Save method: replace existing documents with input documents. The syntax format is as follows:
Db. collection. save (
<Document>,
{
WriteConcern: <document>
}
)
Document: document data, writeConcern: the level at which an exception is thrown.
(3) Delete a document
Mongodb uses db. col. remove () to delete documents. The syntax structure is as follows (before version 2.6 ):
Db. collection. remove (
<Query>,
<JustOne>
)
Versions later than version 2.6:
Db. collection. remove (
<Query>,
{
JustOne: <boolean>,
WriteConcern: <document>
}
)
Query (optional): Condition for deletion. justOne (optional): If it is set to true or 1, only one row is deleted. writeConcert (optional): the level at which an exception is thrown.
Example: db. mycol. remove ({'name': 'test1 '})
(3) query documents
The db. COLLECTION_NAME.find () method displays all data in an unstructured manner, and the db. COLLECTION_NAME.find (). pretty () method displays all data in a formatted manner. In addition, the findOne () method only displays one document.
Comparison between mongodb and relational database where:
Operation |
Format |
Example |
Similar statements in RDBMS |
Equal |
{<key>:<value> } |
Db. col. find ({"by": ""}). pretty () |
Where by = 'cainiao tutorial' |
Less |
{<key>:{$lt:<value>}} |
db.col.find({"likes":{$lt:50}}).pretty() |
where likes < 50 |
Less than or equal |
{<key>:{$lte:<value>}} |
db.col.find({"likes":{$lte:50}}).pretty() |
where likes <= 50 |
Greater |
{<key>:{$gt:<value>}} |
db.col.find({"likes":{$gt:50}}).pretty() |
where likes > 50 |
Greater than or equal |
{<key>:{$gte:<value>}} |
db.col.find({"likes":{$gte:50}}).pretty() |
where likes >= 50 |
Not equal |
{<key>:{$ne:<value>}} |
db.col.find({"likes":{$ne:50}}).pretty() |
where likes != 50 |
Mongodb AND condition: the find () method of mongodb can input multiple keys. Each key is separated by a comma. The syntax format is as follows:
Db. col. find ({key1: value1, key2: value2}). pretty ()
Example: db. mycol. find ({'name': 'tes1', 'sex': 'formale '}). pretty (), the effect of this sentence is similar to SQL: where name = 'test1' and sex = 'formale ',
Mongodb OR condition: the keyword $ OR is used in the MongoDB or condition statement. The syntax format is as follows:
Db. col. find (
{
$ Or :[
{Key1: value1 },{ key2: value2}
]
}
). Pretty ()
For more MongoDB tutorials, see the following:
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
This article permanently updates the link address: