This article mainly introduces the usage of PHP database operation mongodb. It analyzes the functions, installation, basic commands, usage and related precautions of MongoDB in detail in the form of examples. Friends in need can refer to it
The details are as follows:
In traditional databases, we have to write a large number of SQL statements to operate database data, and when storing irregular data, the processing of different fields when creating tables in traditional relational databases is also difficult. It seems a bit weak, mongo came into being, and the wide application of ajax technology and the wide acceptance of json format also make mongo closer to developers.
Introduction to mongo and application scenarios
MongoDB is a document-oriented non-relational database (NoSQL) that is stored in json format. Mongo DB implements object-oriented thinking (OO thinking) very well. In Mongo DB, every record is a Document object. The biggest advantage of Mongo DB is that all data persistence operations do not require developers to manually write SQL statements, and CRUD operations can be easily implemented by directly calling methods.
mongo can be used in the following scenarios:
Storing large-size, low-value data
json and object type data
Website cache data
Comment and sub-comment categories have obvious affiliation data
Multi-server data, its built-in MapReduce can easily realize global traversal.
Installing and using mongodb
We can download the latest version from the official website https://www.mongodb.org/ Stable version, mongo has been officially compiled and can be used after decompression. Its commands are all in the bin directory.
Configure the mongo.conf file first before use
1 2 3 4 5 |
|
Both the database and the data table can be created directly, that is, there is no need to switch, use it directly, use It is created immediately. You can also write js scripts directly in mongo and run it directly. If the _id field is not specified in mongo, mongo will automatically add one.
Various commands of mongo
The commands of mongo are its essence. These very complex commands are gathered together, making mongo’s query become more complex. Be gorgeous and efficient. Each table in mongo is called a collection. The use of commands is similar to MySQL. Switch to the database to directly operate each collection. Its command consists of method (func()), query body (written in {}) and operator (starting with $).
Basic commands
1 2 3 4 5 6 7 |
|
##Query body
##1 2 3 4 5 6 7 8 |
|
1 2 3 4 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 |
|
1 2 3 4 |
|
MapReduce is a very powerful traversal operation tool built into mongo. To use it, you need to implement Its map and reduce functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
More and more detailed commands can be found in the mongo Chinese community http://docs.mongoing.com/manual -zh/ found.
Mongo’s users, data import and export and cluster
User managementMongoDB is not enabled by default Turn on authorization. You can add the --auth or --keyFile option when starting the server to enable authorization. If using a configuration file, use security.authorization or security.keyFile settings.
MongoDB provides its own roles, each of which provides a clear role for a common use case. For example, roles such as read, readWrite, dbAdmin, and root. We manage users by creating users, creating roles, and assigning/recycling different roles to users.
When adding a role, you must first add an administrator role in the admin database, and then use the administrator role to add different roles in each database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
We use the tools that come with mongo to import and export, in the mongo/bin directory Next, it is best to export in csv format to facilitate data exchange.
./mongoexport -d dataname -c tablename -f key1,key2 -q 'query' -o ainname --csv//Export data, the default is json format
./mongoimport -d dataname - c tablename --type json --file ./path //Import data, the default is json format
mongo database cluster
1. Add the option --replSet replname;## when opening mongod
#2. Connect to a mongod process on the mongo client, enter the admin database, and then declare the mongoconf variable:1 2 |
|
We first add the mongo extension to php (see the method: http://www.jb51.net /article/96829.htm). Then, we can use the mongo class function library in the script.
不同于其他的类库只有一个核心类,mongo有四个类,分别是:
Mongo类,基础类,拥有连接、关闭连接、对全局数据库的操作方法。
mongoDB类,邮Mongo类通过selectDB()方法得到,拥有表级的操作方法。
MongoCollection类,一般由Mongo->dbname->collection或直接用MongoDB类和数据库名实例化得到,拥有对数据的基本操作。
MongoCursor类,由MongoCollection通过find()方法得到,拥有普通的游标遍历操作。
以下是一个典型的mongo操作:
1 2 3 4 5 6 |
|
The above is the detailed content of Usage of PHP database operation mongodb. For more information, please follow other related articles on the PHP Chinese website!