About MongoDB
MongoDB is a high-performance, open-source, modeless document-based database that is a popular one in the current NoSQL database. It can be used in many scenarios to replace the traditional relational database or key/value storage methods.
The traditional relational database is usually composed of three hierarchical concepts of database, table, record, and MongoDB is made up of database, collection (collection), Three levels of document objects. MongoDB is for tables in relational databases, but there is no concept of columns, rows, and relationships in the collection, which embodies the nature of pattern freedom.
Features: high performance, easy to deploy, easy to use, easy to store data.
The main features are:
- For collection storage, easy to store data for object types.
- Mode of freedom.
- Supports dynamic queries.
- Supports full indexes, including internal objects.
- Support Queries.
- Supports replication and recovery.
- Use efficient binary data storage, including large objects such as video.
- Automatically process fragmentation to support scalability at the cloud level
- Drivers for Python,php,ruby,java,c,c#,javascript,perl and C + + languages are supported, and drivers for Erlang and. NET platforms are also available in the community.
- The file storage format is Bson (an extension of JSON).
- can be accessed over the network.
function :
- Collection-oriented storage: suitable for storing objects and data in JSON form.
- Dynamic query: MONGO supports rich query expressions. Query directives use a JSON-style tag to easily query objects and arrays embedded in the document.
- Full index support: Includes embedded objects and arrays in the document. The query optimizer of MONGO parses the query expression and generates an efficient query plan.
- Query monitoring: MONGO contains a monitoring tool to analyze the performance of database operations.
- Replication and automatic failover: The MONGO database supports data replication between servers, supporting master-slave mode and inter-server replication. The primary goal of replication is to provide redundancy and automatic failover.
- Efficient traditional storage: supports binary data and large objects (such as photos or pictures)
- Auto-sharding to support cloud-scale scalability: Automatic sharding supports a level of database clustering, adding additional machines dynamically.
applicable occasions :
- Website data: MONGO is ideal for real-time inserts, updates and queries, as well as the replication and high scalability required for real-time data storage on the site.
- Caching: Because of its high performance, MONGO is also suitable as a caching layer for the information infrastructure. After the system restarts, the persistent cache layer built by MONGO can avoid overloading the underlying data sources.
- Large, low-value data: Storing some data in a traditional relational database can be expensive, and many times programmers often choose traditional files for storage.
- Highly scalable scenario: The MONGO is ideal for databases made up of dozens of or hundreds of servers. Built-in support for the MapReduce engine is already included in the roadmap for MONGO.
- Storage for objects and JSON data: The MONGO Bson data format is ideal for storing and querying in a document format.
9.6.2 Download Installation and configuration
1. Download the MongoDB installation package
Official Latest Version: Https://www.mongodb.com/download-center
Other versions: Https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl,
Note: Although the latest version is 3.2.6, but the author tried 10 of the remaining MongoDB visualizer connection 3.2.6 version, the use of the problem, so I use the lower version of the 3.0.7. This also tells us an experience: in the selection of technology, do not pursue the latest, but should choose to meet the requirements and run very stable. Because of the latest technology, not only the relevant information is less, but also not through the actual validation of the project. The study cost is very high, the risk is big.
Of course, here, if you do not need to use the Visual client tool, there is no problem with the latest MongoDB version of the operation.
The author downloads the version 9-51 shown.
2. installing MongoDB under Windows
After the installation package is downloaded, MongoDB is installed in the directory C:\Program Files\mongodb by default.
View the C:\Program Files\mongodb\server\3.0\bin directory, as shown in 9-52.
The Mongod.exe is used to connect to the MONGO database server, which is the server side.
Mongo.exe is used to start the MongoDB shell, which is the client.
Other files:
Mongodump.exe: Logical Backup tool.
Mongorestore.exe: Logical Recovery Tool.
Mongoexport.exe: Data export tool.
Mongoimport.exe: Data import Tool.
(1) Configuring the MONGODB environment
In the new Data folder under Directory D:\WorkSpace\mongodb, the Data folder will be the root folder in which to store the information.
(2) Run cmd as Administrator. Perform the following 2 cmd commands, respectively:
CD \program Files\mongodb\server\3.0--dbpath D:\WorkSpace\mongodb\data
Note: Be sure to create a folder, otherwise it will not take effect.
(3) In the browser input: http://localhost:27017/, you can see the following prompt:
It looks like you is trying to access MongoDB over HTTP on the native driver port.
Indicates that the MONGODB database service has successfully started.
(4) To see the D:\WorkSpace\mongodb\data folder again, you will find many more files under the data directory.
(5) Set up user name
MongoDB default no user name password, is also not authorized, so we need to make our own command as follows:
> Useadminswitched toDB Admin>Db.createuser (... {... User: "DBA",... pwd: "DBA",... roles:[{role: "Useradminanydatabase", DB: "admin"}]... } ... ) Successfully addedUser: { "User": DBA", "Roles":[{"Role": "Useradminanydatabase", "db": "Admin"}]}
View by:> Db.auth ("DBA", "dba") = = =
1: Inserting new data
1 |
db.admin. insert ({ "uid" : 12, "uname" : "gechong" }) |
2: BULK INSERT
3. Update
> db.admin. Update ({"UID":},{"UID": "123", "addr": "Hunan"})
4. Removal
Db.person.remove ({"", ""})
5. Delete the collection you don't want
db.admin. Drop ()
> db.admin.find({
"uid"
: {$gt : 98}}) 查询uid 大于98的
> db.admin.find({
"uid"
:{$ne : 99}}).
count
() 查询uid不等于99的数据量
> db.admin.find({
"uid"
: 100,
"addr"
:
"shanghai"
}) 查询uid=100并且addr="shanghai"的文档
> db.person.find ({$
or
:[{
:100},{
Code class= "SQL string" "addr"
:
"Shanghai"
Code class= "SQL Plain")}).
count
() query uid=100 or addr= "Shanghia" document
> db.person.find({$
where
:
function
(){
return
this.uid==88}}) 使用where条件查询
Visualization Tools
Using MongoDB in Windows