Mongodb installs, uses and implements Python operation database

Time:2022-3-16

1、 Introduction to mongodb

Mongodb is a database based on distributed file storage, which is between relational database and non relational database. It is the most functional and relational database among non relational databases. The data structure he supports is very loose, which is a JSON like bson format, so it can store more complex data types. Mongo’s biggest feature is that the query language he supports is very powerful. Its syntax is a bit similar to the object-oriented query language. It can almost realize most functions similar to single table query of relational database, and also supports the establishment of index on data.

2、 Install mongodb

Mongodb installation is very simple. There is no need to download the source file. You can install it directly with apt get command. If the network speed is too poor and the installation package of mongodb has been downloaded, you can install it offline. Refer to the installation and use tutorial of mongodb under Ubuntu (offline installation). It is recommended to use apt get command for online installation, which can avoid many inexplicable problems.
Following command


sudo apt-get install mongodb

Mongodb can be downloaded and installed. The default installed version is mongodb 2.6.10, but mongodb has been upgraded to 3.2.8. Here, readers will be guided to install version 3.2.8 by adding software sources.
First, open the terminal, import the public key to the package manager, and enter the following command:


sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927   

Create mongodb file list

#Only for Ubuntu 14 04, input:
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Update the package manager, install mongodb, and enter the following command:
sudo apt-get update
sudo apt-get install -y mongodb-org

Note: if the following error occurs after executing the “sudo apt get update” command:

···
update completed, but some metadata was ignored due to errors.
E: Unable to get lock / var / lib / dpkg / lock – open (11: resource temporarily unavailable)
E: Unable to lock the management directory (/ var / lib / dpkg /), is it occupied by another process?
···

Please solve the error as follows, that is, enter the following three commands:


sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
sudo apt-get update

After running the above three commands, you should be able to successfully complete apt get update.

After installing mongodb, enter the following command on the terminal to check the mongodb version:


mongo -version

Output version information, indicating successful installation.

The commands to start and close mongodb are as follows:


sudo service mongodb start
sudo service mongodb stop

By default, mongodb starts automatically with Ubuntu startup.
Enter the following command to see if it starts successfully:

Pgrep Mongo - L # note: - L is the English letter L, not the Arabic numeral 1

Check whether the startup is successful

Solutions for installation errors:
When you enter “sudo service mongodb start” to start mongodb, if this error is reported: failed to start mongod service: Unit not found
Follow these steps to resolve this error:
(1) Creating a profile using the VIM editor


sudo vim /etc/systemd/system/mongodb.service

(2) Add the following content to the configuration file:


[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

Save and exit the VIM editor.
(3) Start mongodb by entering the following command:


sudo systemctl start mongodb 
sudo systemctl status mongodb 

Then you can start successfully.
After that, you can still use the following commands every time you start and close mongodb:


sudo service mongodb start
sudo service mongodb stop

3、 Using mongodb

Shell command mode
Enter the following command to enter the shell command mode of mongodb:

mongo
Or: sudo Mongo

The default connected database is the test database. Before that, make sure you have started mongodb, otherwise an error will occur. After startup, it runs successfully, as shown below
Common operation commands of Mongo shell:

Database related
Show DBS: display database list
Show Collections: displays the collections in the current database (similar to the table in relational database)
Show users: displays all users
Use yourdb: switch the current database to yourdb
db. Help(): display database operation commands
db. yourCollection. Help(): displays the collection operation command. Yourcollection is the collection name

Mongodb does not have the command to create a database. If you want to create a “school” database, run the use school command first, and then do some operations (such as creating an aggregate collection dB. Createcollection (‘teacher ‘)), so that you can create a database called “school”.
Automatically create school database
Next, take a school database as an example, create two sets of teacher and student in the school database, and add, delete, modify and query the data in the student set (the set collection is equivalent to the table in the relational database).

1. Switch to school database

Use school # switches to the school database. Mongodb does not need to pre create the school database, which will be created automatically when it is used

2. Create collection

In this chapter, we introduce how to use mongodb to create collections.

The createcollection () method is used in mongodb to create a collection.

Syntax format:


db.createCollection(name, options) 

Parameter Description:
这里写图片描述

Name: name of the collection to be created
Options: optional parameter that specifies options about memory size and index
Options can be the following parameters:
When inserting a document, mongodb first checks the size field of the fixed set, and then checks the max field.

example

Create the runoob set in the test database:

> use test
switched to db test
> db.createCollection("runoob")
{ "ok" : 1 }
>
If you want to view existing collections, you can use the show collections command:

> show collections
runoob
system.indexes
Here is the usage of createcollection() with several key parameters:

Create a fixed collection mycol. The size of the whole collection space is 6142800 KB, and the maximum number of documents is 10000.

> db.createCollection("mycol", { capped : true, autoIndexId : true, size :  6142800, max : 10000 } )
{ "ok" : 1 }
>
In mongodb, you don't need to create a collection. When you insert some documents, mongodb will automatically create a collection.

> db. mycol2. Insert ({"name": "rookie tutorial"})
> show collections
mycol2
...
##Create an aggregate collection. In fact, when mongodb inserts data, it will automatically create the corresponding set without creating it in advance

Several important data types.
ObjectId
Objectid is similar to a unique primary key, which can be generated and sorted quickly. It contains 12 bytes, which means:

这里写图片描述

The first four bytes indicate the creation of UNIX timestamp, Greenwich mean time UTC time, which is 8 hours later than Beijing time
The next three bytes are the machine identification code
The next two bytes consist of the process ID
The last three bytes are random
Documents stored in mongodb must have a_ ID key. The value of this key can be of any type. By default, it is an objectid object

Since the created timestamp is saved in objectid, you don’t need to save the timestamp field for your document. You can get the creation time of the document through gettimestamp function:

> var newObject = ObjectId()
> newObject.getTimestamp()
ISODate("2017-11-25T07:21:10Z")
Objectid to string

> newObject.str
5a1919e63df83ce79df8b38f
character string
BSON character string都是 UTF-8 编码。

time stamp
Bson has a special timestamp type for internal use in mongodb, which is not related to ordinary date types. The timestamp value is a 64 bit value. Of which:

The first 32 bits are a time_ Number of seconds difference between T and UNIX
The last 32 bits are an increasing ordinal number of operations in a second
In a single mongod instance, the timestamp value is usually unique.

In the replication set, oplog has a TS field. The value in this field represents the operation time using the bson timestamp.

The bson timestamp type is mainly used for internal use in mongodb. In most cases of application development, you can use bson date type.

date
Indicates the current number of milliseconds from the UNIX era (January 1, 1970). The date type is signed, and a negative number indicates a date before 1970.

>Var mydate1 = new date() // Greenwich mean time
> mydate1
ISODate("2018-03-04T14:58:51.233Z")
> typeof mydate1
object
>Var mydate2 = isodate() // Greenwich mean time
> mydate2
ISODate("2018-03-04T15:00:45.479Z")
> typeof mydate2
object
The time created in this way is of date type. You can use the method of date type in JS.

Returns a string of time type:

> var mydate1str = mydate1.toString()
> mydate1str
Sun Mar 04 2018 14:58:51 GMT+0000 (UTC) 
> typeof mydate1str
string
perhaps

> Date()
Sun Mar 04 2018 15:02:59 GMT+0000 (UTC)

1. Insert data

Similar to database creation, collections are automatically created when data is inserted.
There are two ways to insert data: insert and save.

db. student. insert({_id:1, sname: 'zhangsan', sage: 20}) #_ ID optional
db. student. save({_id:1, sname: 'zhangsan', sage: 22}) #_ ID optional

These two methods are inserted into the data_ The ID field cannot be written, and a unique ID will be automatically generated_ ID to identify this piece of data. The difference between insert and save is that they are inserted manually_ ID field, if_ ID already exists, insert does not operate, save does update; If not_ ID field, which has the same function as inserting data.
The structure of the data added by insert and save is loose, and the column attributes are not fixed. The data added shall prevail. You can insert multiple pieces of data at one time by defining the data first and then inserting it,
Insert multiple
After running the above example, the student has been automatically created, which also shows that mongodb does not need to define the collection in advance. After inserting data for the first time, the collection will be automatically created.

2. Find data

Mongodb uses the find () method to query documents.

The find () method displays all documents in an unstructured manner.

grammar
The syntax format of mongodb query data is as follows:

db.collection.find(query, projection)
Query: optional. Use the query operator to specify query criteria
Projection: optional. Use the projection operator to specify the returned key. When querying, all key values in the document are returned. You only need to omit this parameter (omitted by default).
If you need to read data in a readable way, you can use the pretty () method. The syntax format is as follows:

>db.col.find().pretty()
The pretty () method displays all documents in a formatted manner.

In addition to the find () method, there is also a findone () method that returns only one document.

Comparison between mongodb and RDBMS where statements
If you are familiar with regular SQL data, you can better understand mongodb conditional query through the following table:

这里写图片描述 

Mongodb and condition
Mongodb’s find () method can pass in multiple keys. Each key is separated by commas, which is the and condition of conventional SQL.
The syntax format is as follows:


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

Mongodb or condition
The mongodb or conditional statement uses the keyword $or. The syntax format is as follows:


db.col.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

Combined use of and and or
The following example demonstrates the combined use of and and or, similar to the conventional SQL statement: ‘where likes > 50 and (by =’ rookie tutorial ‘or title =’ mongodb tutorial ‘)

db. Col.find ({"likes": {$GT: 50}, $or: [{"by": "rookie tutorial"}, {"title": "mongodb tutorial"}]}) pretty()

3. Update data

Mongodb update document
Mongodb uses the update () and save () methods to update the documents in the collection. Next, let’s take a detailed look at the application of the next two functions and their differences.

Update() method
The update () method is used to update an existing document. The syntax format is as follows:


db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

Parameter Description:

Query: the query condition of update, similar to that after where in SQL update query.
Update: the object of update and some Update Operators (such as set, s, e, t, inc…) can also be understood as the object after set in SQL update query
Upsert: optional. This parameter means whether to insert objnew if there is no update record. True means insert. The default is false and not insert.
Multi: optional. Mongodb is false by default. Only the first record found will be updated. If this parameter is true, all records found by conditions will be updated.
Writeconcern: optional, the level of exception thrown.
example
We insert the following data into the set col:

>db.col.insert({
    Title: 'mongodb tutorial', 
    Description: 'mongodb is a NoSQL database',
    By: 'rookie tutorial',
    url: 'http://www.runoob.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})

Then we update the title through the update () method:

>db. Col.update ({title ':'mongodb tutorial'}, {$set: {title ':'mongodb'}}) ## plus $set use: only modify the target row, otherwise other rows will not be retained.
Writeresult ({"nmatched": 1, "nupserved": 0, "nmodified": 1}) # output information
> db.col.find().pretty()
{
        "_id" : ObjectId("56064f89ade2f21f36b03136"),
        "title" : "MongoDB",
        "Description": "mongodb is a NoSQL database",
        "By": "rookie tutorial",
        "url" : "http://www.runoob.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}
>
#You can see that the title is updated from the original "mongodb tutorial" to "mongodb".

The above statement will only modify the first found document. If you want to modify multiple identical documents, you need to set the multi parameter to true.

>db. Col.update ({title ':'mongodb tutorial'}, {$set: {title ':'mongodb'}}, {multi: true})

Save() method
The save () method replaces the existing document with the incoming document. The syntax format is as follows:


db.collection.save(
   <document>,
   {
     writeConcern: <document>
   }
)

Parameter Description:
Document: document data.
Writeconcern: optional, the level of exception thrown.
example
In the following example, we replaced_ Document data with ID 56064f89ade2f21f36b03136:

>db.col.save({
    "_id" : ObjectId("56064f89ade2f21f36b03136"),
    "title" : "MongoDB",
    "Description": "mongodb is a NoSQL database",
    "by" : "Runoob",
    "url" : "http://www.runoob.com",
    "tags" : [
            "mongodb",
            "NoSQL"
    ],
    "likes" : 110
})
After the replacement is successful, we can view the replaced data through the find() command

>db.col.find().pretty()
{
        "_id" : ObjectId("56064f89ade2f21f36b03136"),
        "title" : "MongoDB",
        "Description": "mongodb is a NoSQL database",
        "by" : "Runoob",
        "url" : "http://www.runoob.com",
        "tags" : [
                "mongodb",
                "NoSQL"
        ],
        "likes" : 110
}
>

More examples
Update only the first record:


db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );

Update all:


db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );

Add only the first:


db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );

Add all:


db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );

Update all:


db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );

Update only the first record:


db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );

4. Delete data

In the previous chapters, we have learned how to add data and update data for collections in mongodb. In this chapter, we will continue to learn about the deletion of mongodb sets.

The mongodb remove() function is used to remove data from the collection.

Mongodb data update can use the update() function. It is a good habit to execute the find () command before executing the remove () function to judge whether the execution conditions are correct.

grammar
The basic syntax format of the remove () method is as follows:


db.collection.remove(
   <query>,
   <justOne>
)

If your mongodb is later than version 2.6, the syntax format is as follows:


db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }
)

Parameter Description:
Query: (optional) the condition of the deleted document.
Justone: (optional) if set to true or 1, only one document will be deleted.
Writeconcern: (optional) the level at which the exception was thrown.
example
We performed two inserts for the following documents:

>db. Col.insert ({Title: 'mongodb tutorial', 
    Description: 'mongodb is a NoSQL database',
    By: 'rookie tutorial',
    url: 'http://www.runoob.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})

Use the find() function to query data:

> db.col.find()
{"_id": objectid ("56066169ade2f21f36b03137"), "title": "mongodb tutorial", "description": "mongodb is a NoSQL database", "by": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
{"_id": objectid ("5606616dade2f21f36b03138"), "title": "mongodb tutorial", "description": "mongodb is a NoSQL database", "by": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
Next, we remove the document with the title 'mongodb tutorial':

>db. Col.remove ({'title':'mongodb tutorial '})
Writeresult ({"nremoved": 2}) # deleted two pieces of data
>db.col.find()
... # no data
If you only want to delete the first record found, you can set justone to 1, as shown below:

>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
If you want to delete all data, you can use the following method (similar to the truncate command of conventional SQL):

>db.col.remove({})
>db.col.find()
>

5. Conditional operator

The conditional operator is used to compare two expressions and get data from the mongodb set.
The conditional operators in mongodb are:

(>) greater than – $GT
(<) less than – $LT
(> =) greater than or equal to – $GTE
(< =) less than or equal to – $LTE

The database name we use is “runoob”, our collection name is “col”, and the following is the data we insert.

To facilitate the test, we can clear the data of the collection “col” with the following command:

db.col.remove({})
Insert the following data

>db.col.insert({
    Title: 'PHP tutorial', 
    Description: 'PHP is a powerful server-side scripting language for creating dynamic interactive sites.',
    By: 'rookie tutorial',
    url: 'http://www.runoob.com',
    tags: ['php'],
    likes: 200
})
>db. Col.insert ({Title: 'Java Tutorial', 
    Description: 'Java is a high-level programming language launched by Sun Microsystems in May 1995.',
    By: 'rookie tutorial',
    url: 'http://www.runoob.com',
    tags: ['java'],
    likes: 150
})
>db. Col.insert ({Title: 'mongodb tutorial', 
    Description: 'mongodb is a NoSQL database',
    By: 'rookie tutorial',
    url: 'http://www.runoob.com',
    tags: ['mongodb'],
    likes: 100
})

Use the find() command to view the data:

db.col.find()

Mongodb (>) greater than operator – $GT
If you want to get data with “likes” greater than 100 in the “col” set, you can use the following command:

db.col.find({"likes" : {$gt : 100}})
Similar to SQL statement:
Select * from col where likes > 100;
Output result:

> db.col.find({"likes" : {$gt : 100}})
{"_id": objectid ("56066542ade2f21f36b0313a"), "title": "PHP tutorial", "description": "PHP is a powerful server-side scripting language for creating dynamic interactive sites.", "By": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "php" ], "likes" : 200 }
{"_id": objectid ("56066549ade2f21f36b0313b"), "title": "java tutorial", "description": "Java is a high-level programming language launched by Sun Microsystems in May 1995.", "By": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "java" ], "likes" : 150 }
> 
Mongodb (> =) greater than or equal to operator - $GTE
If you want to get data with "likes" greater than or equal to 100 in the "col" set, you can use the following command:

db.col.find({likes : {$gte : 100}})
Similar to SQL statement:

Select * from col where likes >=100;
Output result:

> db.col.find({likes : {$gte : 100}})
{"_id": objectid ("56066542ade2f21f36b0313a"), "title": "PHP tutorial", "description": "PHP is a powerful server-side scripting language for creating dynamic interactive sites.", "By": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "php" ], "likes" : 200 }
{"_id": objectid ("56066549ade2f21f36b0313b"), "title": "java tutorial", "description": "Java is a high-level programming language launched by Sun Microsystems in May 1995.", "By": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "java" ], "likes" : 150 }
{"_id": objectid ("5606654fade2f21f36b0313c"), "title": "mongodb tutorial", "description": "mongodb is a NoSQL database", "by": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "mongodb" ], "likes" : 100 }
> 
Mongodb (<) less than operator - $LT
If you want to get data with "likes" less than 150 in the "col" set, you can use the following command:

db.col.find({likes : {$lt : 150}})
Similar to SQL statement:

Select * from col where likes < 150;
Output result:

> db.col.find({likes : {$lt : 150}})
{"_id": objectid ("5606654fade2f21f36b0313c"), "title": "mongodb tutorial", "description": "mongodb is a NoSQL database", "by": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "mongodb" ], "likes" : 100 }
Mongodb (< =) less than operator - $LTE
If you want to get data with "likes" less than or equal to 150 in the "col" set, you can use the following command:

db.col.find({likes : {$lte : 150}})
Similar to SQL statement:

Select * from col where likes <= 150;
Output result:

> db.col.find({likes : {$lte : 150}})
{"_id": objectid ("56066549ade2f21f36b0313b"), "title": "java tutorial", "description": "Java is a high-level programming language launched by Sun Microsystems in May 1995.", "By": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "java" ], "likes" : 150 }
{"_id": objectid ("5606654fade2f21f36b0313c"), "title": "mongodb tutorial", "description": "mongodb is a NoSQL database", "by": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "mongodb" ], "likes" : 100 }
Mongodb uses (<) and (>) queries - $LT and $GT
If you want to get data with "likes" greater than 100 and less than 200 in the "col" set, you can use the following command:

db.col.find({likes : {$lt :200, $gt : 100}})
Similar to SQL statement:

Select * from col where likes>100 AND  likes<200;
Output result:

> db.col.find({likes : {$lt :200, $gt : 100}})
{"_id": objectid ("56066549ade2f21f36b0313b"), "title": "java tutorial", "description": "Java is a high-level programming language launched by Sun Microsystems in May 1995.", "By": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "java" ], "likes" : 150 }
>

6. Mongodb limit and skip method

Mongodb limit() method
If you need to read a specified number of data records in mongodb, you can use the limit method of mongodb. The limit () method accepts a numeric parameter that specifies the number of records read from mongodb.

grammar
The basic syntax of the limit() method is as follows:


db.COLLECTION_NAME.find().limit(NUMBER)

example
The data in the set col is as follows:

db. col.find({},{"title":1,_id:0}). limit(2)      #  _ ID: 0 (projection 1: Show 0: hide)
{"title": "PHP tutorial"}
{"title": "java tutorial"}

Note: if you do not specify the parameters in the limit () method, all the data in the collection will be displayed.

Mongodb skip() method
In addition to using the limit () method to read the specified amount of data, we can also use the skip () method to skip the specified amount of data. The skip method also accepts a numeric parameter as the number of records skipped.

db. col.find({},{"title":1,_id:0}). limit(2)      #  _ ID: 0 (projection 1: Show 0: hide)
{"title": "PHP tutorial"}
{"title": "java tutorial"}

Note: if you do not specify the parameters in the limit () method, all the data in the collection will be displayed.

7. Mongodb sorting

Mongodb sort() method
In mongodb, use the sort () method to sort the data. The sort () method can specify the fields to be sorted through parameters, and use 1 and - 1 to specify the sorting method, where 1 is in ascending order and - 1 is in descending order.

grammar
sort()方法基本grammar如下所示:

>db.COLLECTION_NAME.find().sort({KEY:1})
example
The data in col set is as follows:

{"_id": objectid ("56066542ade2f21f36b0313a"), "title": "PHP tutorial", "description": "PHP is a powerful server-side scripting language for creating dynamic interactive sites.", "By": "rookie tutorial", "URL":“ http://www.runoob.com ", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb" ], "likes" : 100 }
以下example演示了 col 集合中的数据按字段 likes 的降序排列:

>db.col.find({},{"title":1,_id:0}).sort({"likes":-1})
{ "title" : "PHP 教程" }
{ "title" : "Java 教程" }
{ "title" : "MongoDB 教程" }
>

8.MongoDB索引

索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录。

这种扫描全集合的查询效率是非常低的,特别在处理大量的数据时,查询可以要花费几十秒甚至几分钟,这对网站的性能是非常致命的。

索引是特殊的数据结构,索引存储在一个易于遍历读取的数据集合中,索引是对数据库表中一列或多列的值进行排序的一种结构

ensureIndex() 方法
MongoDB使用 ensureIndex() 方法来创建索引。

语法
ensureIndex()方法基本语法格式如下所示:

db.COLLECTION_NAME.ensureIndex({KEY:1})
语法中 Key 值为你要创建的索引字段,1为指定按升序创建索引,如果你想按降序来创建索引指定为-1即可。

实例


db.col.ensureIndex({"title":1})

ensureIndex() 方法中你也可以设置使用多个字段创建索引(关系型数据库中称作复合索引)。


db.col.ensureIndex({"title":1,"description":-1})

ensureIndex() 接收可选参数,可选参数列表如下:

这里写图片描述 

实例在后台创建索引:


db.values.ensureIndex({open: 1, close: 1}, {background: true})

通过在创建索引时加background:true 的选项,让创建工作在后台执行

8.MongoDB 聚合

MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果。有点类似sql语句中的 count(*)。

aggregate() 方法
MongoDB中聚合的方法使用aggregate()。

语法
aggregate() 方法的基本语法格式如下所示:


db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)

实例


集合中的数据如下:

{
   _id: ObjectId(7df78ad8902c)
   title: 'MongoDB Overview', 
   description: 'MongoDB is no sql database',
   by_user: 'runoob.com',
   url: 'http://www.runoob.com',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
},
{
   _id: ObjectId(7df78ad8902d)
   title: 'NoSQL Overview', 
   description: 'No sql database is very fast',
   by_user: 'runoob.com',
   url: 'http://www.runoob.com',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 10
},
{
   _id: ObjectId(7df78ad8902e)
   title: 'Neo4j Overview', 
   description: 'Neo4j is no sql database',
   by_user: 'Neo4j',
   url: 'http://www.neo4j.com',
   tags: ['neo4j', 'database', 'NoSQL'],
   likes: 750
},
现在我们通过以上集合计算每个作者所写的文章数,使用aggregate()计算结果如下:
## _id 指定根据那个属性分组
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
{
   "result" : [
      {
         "_id" : "runoob.com",
         "num_tutorial" : 2
      },
      {
         "_id" : "Neo4j",
         "num_tutorial" : 1
      }
   ],
   "ok" : 1
}

以上实例类似sql语句: select by_user, count(*) from mycol group by by_user

在上面的例子中,我们通过字段by_user字段对数据进行分组,并计算by_user字段相同值的总和。

下表展示了一些聚合的表达式:

这里写图片描述 

管道的概念
管道在Unix和Linux中一般用于将当前命令的输出结果作为下一个命令的参数。

MongoDB的聚合管道将MongoDB文档在一个管道处理完毕后将结果传递给下一个管道处理。管道操作是可以重复的。

表达式:处理输入文档并输出。表达式是无状态的,只能用于计算当前聚合管道的文档,不能处理其它的文档。

这里我们介绍一下聚合框架中常用的几个操作:

project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。 p r o j e c t : 修 改 输 入 文 档 的 结 构 。 可 以 用 来 重 命 名 、 增 加 或 删 除 域 , 也 可 以 用 于 创 建 计 算 结 果 以 及 嵌 套 文 档 。 match:用于过滤数据,只输出符合条件的文档。 match使用MongoDB的标准查询操作。 m a t c h 使 用 M o n g o D B 的 标 准 查 询 操 作 。 limit:用来限制MongoDB聚合管道返回的文档数。
skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。 s k i p : 在 聚 合 管 道 中 跳 过 指 定 数 量 的 文 档 , 并 返 回 余 下 的 文 档 。 unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
group:将集合中的文档分组,可用于统计结果。 g r o u p : 将 集 合 中 的 文 档 分 组 , 可 用 于 统 计 结 果 。 sort:将输入文档排序后输出。
$geoNear:输出接近某一地理位置的有序文档。
管道操作符实例


1、$project实例

db.article.aggregate(
    { $project : {
        title : 1 ,
        author : 1 ,
    }}
 );
这样的话结果中就只还有_id,tilte和author三个字段了,默认情况下_id字段是被包含的,如果要想不包含_id话可以这样:

db.article.aggregate(
    { $project : {
        _id : 0 ,
        title : 1 ,
        author : 1
    }});
2.$match实例

db.articles.aggregate( [
                        { $match : { score : { $gt : 70, $lte : 90 } } },
                        { $group: { _id: null, count: { $sum: 1 } } }
                       ] );
$match用于获取分数大于70小于或等于90记录,然后将符合条件的记录送到下一阶段$group管道操作符进行处理。

3.$skip实例

db.article.aggregate(
    { $skip : 5 });
经过$skip管道操作符处理后,前五个文档被"过滤"掉。

四 Python操作MongoDB

安装PyMongo模块


pip install pymongo

使用MongoClient建立连接


from pymongo import MongoClient
#链接格式
mongodb://[username:[email protected]]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
mongodb:// 这是固定的格式,必须要指定。

username:[email protected] 可选项,如果设置,在连接数据库服务器之后,驱动都会尝试登陆这个数据库

host1 必须的指定至少一个host, host1 是这个URI唯一要填写的。它指定了要连接服务器的地址。如果要连接复制集,请指定多个主机地址。

portX 可选的指定端口,如果不填,默认为27017

/database 如果指定username:[email protected],连接并验证登陆指定数据库。若不指定,默认打开 test 数据库。

?options 是连接选项。如果不使用/database,则前面需要加上/。所有连接选项都是键值对name=value,键值对之间通过&或;(分号)隔开
# 以下为三种建立无验证连接的方式
#client = MongoClient()
#client = MongoClient('localhost', 27017)
#client = MongoClient('mongodb://localhost:27017/')
#建立验证连接
使用用户 admin 使用密码 123456 连接到本地的 MongoDB 服务上。输出结果如下所示:

> mongodb://admin:[email protected]/
... 
使用用户名和密码连接登陆到指定数据库,格式如下:

mongodb://admin:[email protected]/test

获取数据库


# 以下是两种获取数据库的方式
db = client.pythondb
db = client['python-db']

获取集合


# 以下是两种获取集合的方式
collection = db.python_collection
collection = db['python-collection']

上述任何命令都没有在MongoDB服务器上实际执行任何操作。当第一个文档插入集合时才创建集合和数据库。

插入文档


#!/usr/bin/python3
#coding=utf-8

import datetime
from pymongo import MongoClient

client = MongoClient()
db = client.pythondb
posts = db.posts

post = {"author": "Maxsu",
         "text": "My first blog post!",
         "tags": ["mongodb", "python", "pymongo"],
         "date": datetime.datetime.utcnow()}

posts.insert(post)

# 批量插入,参数为list
posts.insert_many(new_posts)

查找文档


#!/usr/bin/python3
#coding=utf-8

import datetime
import pprint
from pymongo import MongoClient

client = MongoClient()
db = client.pythondb
posts = db.posts

# 查找单个文档
print(posts.find_one())

# 查找多个文档
for post in posts.find():
    print(post)

# 计数统计
print(posts.count())
print(posts.find({"author": "Maxsu"}).count())

到此这篇关于MongoDB安装使用并实现Python操作数据库 的文章就介绍到这了,更多相关MongoDB安装使用内容请搜索developpaer以前的文章或继续浏览下面的相关文章希望大家以后多多支持developpaer!