In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Introduction to MongoDB
Summary: MongoDB is an open source database system based on distributed file storage, which is based on documents (document, that is, the concept of rows in a relational database) and is stored in the form of key= > value, similar to the format of JSON. Mongodb is a document-based non-relational database, which is open source, high-performance, highly available and extensible.
Comparison between MongoDB and Relational Database
The following figure shows a comparison between sql terms and mongodb terms:
Document
Document is the basic unit of data in MongoDB. It is very simple to understand that a group of data put together in an orderly manner is a document.
For example:
Single key value document {"userName": "Bertram"}
Multi-key value document {"_ id": ObjectId ("ff4a0ead0c5fc752b13f2af441b7da12"), "name": "Bertram", "countryName": "Japan"}
Set
Putting multiple documents together is a collection.
Database
Multiple collections are put together to form a database.
Shell partial command table
1) show dbs displays all databases
> show dbs / / the newly created database will not be displayed. Some data needs to be written before it can be displayed. Admin (empty) local 0.078GBtest 0.078GBservice 0.058GB >
2) db displays the current database
> db test
3) use switch or create database
> use local / / if the database does not exist, create the database, otherwise switch to the specified database. Basic operations of databases, collections and documents of switched to dblocal > dblocal > MongoDB
1.MongoDB creates database and deletes database
1) create the database: use DATABASE_NAME if the database does not exist, create the database, otherwise switch to the specified database 2) delete the database: db.dropDatabase ()
two。 Create and delete collections
1) create a collection: db.createCollection (name, options) Parameter description: name: name of the collection to be created options: optional parameter, specify options for memory size and index 2) Collection deletion: db.collection.drop () 3) if you want to view an existing collection You can use the show collections or show tables command: > show collections or show tables4) example: here is the use of createCollection () with several key parameters: create a collection (the entire collection space size 10240KB, the maximum number of documents is 17855200.) > db.createCollection ('replicationColletion', {' capped':true, 'size':10240,' max':17855200}) {"ok": 1} > in MongoDB, you do not need to create a collection. When you insert some documents, MongoDB automatically creates collections. > db.curriculum.insert ({"name": "English"}) > show collectionscurriculum
3. Insert document
1) insert document: MongoDB inserts a document into the collection using the insert () or save () method The syntax is as follows: db.serviceLECTION_NAME.insert (document) example: the following documents can be stored in the service collection of MongoDB's runoob database: > db.service.insert ({title: 'MongoDB learning', description: 'MongoDB is a Nosql database', by: '51CTO blog', url: 'https://docs.mongodb.com', tags: [' mongodb', 'database',' NoSQL'] Likes: 100}) in the above example, service is the collection name If the collection is not in the database, MongoDB automatically creates the set and inserts the document. 2) View the inserted document: > db.service.find () {"_ id": ObjectId ("5e143d32183a994d1b71c06a"), "title": "MongoDB Learning", "description": "MongoDB is a Nosql Database", "by": "51CTO blog", "url": "https://docs.mongodb.com"," tags ": [" mongodb "," database "," NoSQL "] "likes": 100}-3) We can also define the data as a variable As follows: > document= ({title: 'MongoDB Learning', description: 'MongoDB is a Nosql database', by: '51CTO blog', url: 'https://docs.mongodb.com', tags: [' mongodb', 'database',' NoSQL'], likes: 100}) The results displayed after execution are as follows: {"title": "MongoDB Learning", "description": "MongoDB is a Nosql database", "by": "51CTO blog", "url": "https://docs.mongodb.com"," tags ": [" mongodb "," database "," NoSQL "] "likes": 100} perform insert operation: > db.service.insert (document) WriteResult ({"nInserted": 1}) insert the document you can also use the db.service.save (document) command. If you do not specify the _ id field, the save () method is similar to the insert () method. If you specify the _ id field, the data for that _ id is updated.
4. Query document
-- MongoDB queries documents using the find () method. The find () method displays all documents in an unstructured manner. -- MongoDB Limit () method if you need to read a specified number of data records in MongoDB, you can use the Limit method of MongoDB, and the limit () method accepts a numeric parameter that specifies the number of records to read from MongoDB. -- count query the number of records-- the MongoDB Skip () method can not only use the limit () method to read a specified amount of data, but also use the skip () method to skip the specified amount of data. The skip method also accepts a numeric parameter as the number of skipped records. Example: > db.service.find () {"_ id": ObjectId ("5e143d32183a994d1b71c06a"), "title": "MongoDB Learning", "description": "MongoDB is a Nosql database", "by": "51CTO blog", "url": "https://docs.mongodb.com"," tags ": [" mongodb "," database "," NoSQL "]," likes ": 100 > db.service.find ({}) {"title": "0", "_ id": "1"}) .limit (1) {"_ id": ObjectId ("5e143d32183a994d1b71c06a"), "title": "MongoDB Learning"} > db.service.find (). Limit (2) {"_ id": ObjectId ("5e143edc183a994d1b71c06b"), "title": "Python tutorial", "description": "Python is a programming language", "by": "51CTO blog" "url": "https://www.python.org"," tags ": [" Python "," programme "," language "]," likes ": 120} Note: if the query find does not specify parameters in the limit () method, all data in the collection is displayed. > db.service.find () .count () / / query and count the number of result records > db.service.count () / / Statistics the following examples only show the second document data > db.service.find ({}, {"title": "1", _ id:0}) .limit (1) .skip (1) {"title": "Python tutorial"}
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.