In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Document database MongoDB is like, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
1. Introduction to MongoDB 1.1.What is MongoDB
MongoDB is a cross-platform, document-oriented database, which is one of the most popular NoSQL database products. It is between the relational database and the non-relational database. It is the most functional product in the non-relational database and most similar to the relational database. It supports a very loose data structure, which is similar to JSON's BSON format, so it can store more complex data types. The official website address of MongoDB is: http://www.mongodb.org/
1.2 MongoDB featur
The most important feature of MongoDB is that the query language it supports is very powerful, and its syntax is somewhat similar to that of object-oriented query language. It can almost achieve most of the functions of single table query in relational database, and it also supports building indexes for data. It is a collection-oriented, schema-free document database. The specific features are summarized as follows:
(1) for collection storage, it is easy to store data of object type.
(2) Mode freedom.
(3) support dynamic query
(4) full indexing is supported, including internal objects
(5) support replication and failure recovery
(6) use efficient binary data storage, including large objects (such as video, etc.)
(7) automatically handle fragments to support scalability at the cloud computing level
(8) drivers that support Python,PHP,Ruby,Java,C,C#,Javascript,Perl and C++, and drivers for Erlang and .NET platforms are also available in the community.
(9) File storage format is BSON (an extension of JSON)
1.3 MongoDB architecture
The logical structure of MongoDB is a hierarchical structure. Mainly by: document (document), collection (collection), database (database) of these three parts. The logical structure is user-oriented, and users use the logical structure to develop applications using MongoDB.
(1) the document (document) of MongoDB is equivalent to a row of records in a relational database.
(2) multiple documents form a collection, which is equivalent to the tables of a relational database.
(3) multiple collections (collection), logically organized together, is the database (database).
(4) one MongoDB instance supports multiple databases (database). The hierarchical structure of documents (document), collections (collection), and databases (database) is shown below:
The following table shows a comparison of the concepts of logical structure between MongoDB and MySQL databases
1.4 data types
Basic data type
Null: used to represent fields that are null or do not exist, {"x": null}
Boolean: Boolean has two values true and false, {"x": true}
Value: shell defaults to 64 as a floating-point value. {"x": 3.14} or {"x": 3}. For integer values, you can use NumberInt (4-byte signed integer) or NumberLong (8-byte signed integer), {"x": NumberInt ("3")} {"x": NumberLong ("3")}
String: UTF-8 string can be represented as data of string type, {"x": "hehe"}
Date: date is stored as the number of milliseconds that have elapsed since the New Epoch dependency, no time zone, {"x": new Date ()}
Regular expressions: when querying, regular expressions are used as qualifying conditions, and the syntax is the same as JavaScript's regular expressions, {"x": / [abc] /}
Array: a data list or dataset can be represented as an array, {"x": ["a", "b", "c"]}
Embedded documents: documents can be nested within other documents, and nested documents are treated as values, {"x": {"y": 3}}
Object Id: object id is a 12-byte string that is the unique identifier of the document, {"x": objectId ()}
Binary data: binary data is an arbitrary byte string. It cannot be used directly in shell. If you want to save non-utf- characters to the database, binary data is the only way. Code: queries and documents can include any JavaScript code, {"x": function () {/... /}}
2.MongoDB installation and startup 2.1.1 window system MongoDB installation
Double-click "mongodb-win32-x86_64-2008 plusssll-3.2.10-signed.msi" in "Resources\ Micro Services related\ supporting Software\ mongodb" to install it. After the installation is complete, the software is installed in the C:\ Program Files\ MongoDB directory.
The service program we want to start is mongod.exe in the C:\ Program Files\ MongoDB\ Server\ 3.2\ bin directory. To facilitate our startup, I set C:\ Program Files\ MongoDB\ Server\ 3.2\ bin to the environment variable path.
Start
(1) first open the command prompt and create a directory for storing data
Md d:\ data
(2) start the service
Mongod-- dbpath=d:\ data
We can see from the startup information that the default port of mongoDB is 27017. If we want to change the default boot port, we can use-- port to specify the port and enter the following command at the command prompt to complete the login.
Mongo
Exit mongodb
Exit2.2 Common commands 2.2.1 Select and create databases
Select and create the syntax format of the database:
Use database name
If the database does not exist, automatically create the following statement to create the spit database
Use spitdb2.2.2 insert and query documents
Insert the syntax format of the document:
Db. Collection name .insert (data)
We can insert the following test data here:
Db.spit.insert ({content: "I heard that the tenth power course is very powerful", userid: "1011", nickname: "Xiaoya", visits:NumberInt (902)})
The syntax format of the query collection:
Db. Collection name. Find ()
If we want to query all the documents in the spit collection, we enter the following command
Db.spit.find ()
Here you will find that each document will have a field called _ id, which is equivalent to the primary key of the table in our original relational database. When you do not specify this field when you insert the document record, MongoDB will automatically create it with a type of ObjectID. If we specify this field when we insert a document record, its type can be ObjectID or any type supported by MongoDB.
Enter the following test statement:
Db.spit.insert ({_ id: "1", content: "I still haven't figured out why I went wrong", userid: "1012", nickname: "Xiaoming", visits:NumberInt (2020)}); db.spit.insert ({_ id: "2", content: "overtime until midnight", userid: "1013", nickname: "Caesar", visits:NumberInt (1023)}); db.spit.insert ({_ id: "3", content: "what if the mobile traffic exceeds?" , userid: "1013", nickname: "Caesar", visits:NumberInt (1223); db.spit.insert ({_ id: "4", content: "persistence is victory", userid: "1014", nickname: "Nono", visits:NumberInt (1223)})
What if I want to query according to certain conditions, such as I want to query a record with a userid of 1013? It's simple! Just add a parameter to find (), which is also in json format, as follows:
Db.spit.find ({userid:'1013'})
If you only need to return the first piece of data that meets the criteria, we can use the findOne command to do so
Db.spit.findOne ({userid:'1013'})
If you want to return a specified number of records, you can call limit after the find method to return the result, for example:
Db.spit.find () .limit (3) 2.2.3 modify and delete documents
Modify the syntax structure of the document:
Db. Collection name .update (condition, modified data)
If we want to modify the record with an id of 1 and the number of views is 1000, enter the following statement:
Db.spit.update ({_ id: "1"}, {visits:NumberInt (1000)})
After execution, we will find that all the fields in this document are missing except the visits field. To solve this problem, we need to use the modifier $set to implement it, with the command as follows:
Db.spit.update ({_ id: "2"}, {$set: {visits:NumberInt (2000)}})
This is OK.
Delete the syntax structure of the document:
Db. Collection name .remove (condition)
The following statement can delete all data, please use it with caution
Db.spit.remove ({})
If you delete the record of visits=1000, enter the following statement
Db.spit.remove ({visits:1000}) 2.2.4 count the number of entries
Statistical recording conditions use the count () method. The following statement counts the number of records in the spit collection
Db.spit.count ()
If you count according to conditions, for example, count the number of records with a userid of 1013
Db.spit.count ({userid: "1013"}) 2.2.5 Fuzzy query
The fuzzy query of MongoDB is realized by regular expression. The format is:
/ Fuzzy query string /
For example, I want to query all documents where the complaint contains "traffic". The code is as follows:
Db.spit.find ({content:/ traffic /})
If you want to query the complaint that starts with "overtime", the code is as follows:
Db.spit.find ({content:/ ^ overtime /}) 2.2.6 greater than less than not equal to
The = operator is also commonly used in the following format:
Db. The collection name. Find ({"field": {$gt: value}}) / / is greater than: field > valuedb. Collection name .find ({"field": {$lt: value}}) / / less than: field
< valuedb.集合名称.find({ "field" : { $gte: value }}) // 大于等于: field >= valuedb. Collection name .find ({"field": {$lte: value}}) / less than or equal to: field
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.