Python Road _day99_mongodb Database

Source: Internet
Author: User
Tags mongodb

First, installation and configuration

1. Download and install

#1, install path is D:\MongoDB, add D:\MongoDB\bin directory to environment variable#2. Create new directory and fileD:\mongodb\data\dbd:\mongodb\log\mongod.log#3. New configuration file Mongod.cfg, reference: https://docs.mongodb.com/manual/reference/configuration-options/systemLog:destination:file Path:"D:\MongoDB\log\mongod.log"logAppend:truestorage:journal:enabled:true DbPath:"D:\MongoDB\data\db"Net:bindip:0.0.0.0Port:27017SetParameter:enableLocalhostAuthBypass:false#4, Production system ServicesMongod--config"D:\MongoDB\mongod.cfg"--BIND_IP 0.0.0.0--Install#orMongod--bind_ip 0.0.0.0--port 27017--logpath D:\MongoDB\log\mongod.log--logappend--dbpath D:\MongoDB\data\db--serv Icename"MongoDB"--servicedisplayname"MongoDB"--Install#5. Start \ Closenet start mongodbnet stop MongoDB#6. LoginMONGO#Links: http://www.runoob.com/mongodb/mongodb-window-install.html

2. Account Management

#Account Management: https://docs.mongodb.com/master/tutorial/enable-authentication/#1. Create an account#Create Super UserUse admindb.createuser ({User:"Root", pwd:"123", roles: [{role:"Root"Db:"Admin" } ]  })#Create a normal userUse testdb.createuser ({User:"Egon", pwd:"123", roles: [{role:"ReadWrite"Db:"Test"}, {role:"Read"Db:"DB1" } ]  })#2. Restart the databaseMongod--Removemongod--config"C:\mongodb\mongod.cfg"--BIND_IP 0.0.0.0--install--Auth#or (corresponding to the production system service)Mongod--bind_ip 0.0.0.0--port 27017--logpath D:\MongoDB\log\mongod.log--logappend--dbpath D:\MongoDB\data\db--serv Icename"MongoDB"--servicedisplayname"MongoDB"--install--Auth

#3. Login: note Using double quotes instead of single quotesMONGO--port 27017-u"Root"-P"123"--authenticationdatabase"Admin" #You can also log in with Db.auth ("account", "password") after loginmongouse admin Db.auth ("Root","123")#Recommended Blog: https://www.cnblogs.com/zhoujinyi/p/4610050.html

Second, the basic operation

1. Database operation

A, Increase:

Use DB1

As above, DB1 is the name of the database that was created, when the above command is executed, if the DB1 database already exists, it will be switched to this database, or if the database does not exist, a new database will be created, and also switch to this database.

b, check:

Show dbs                                                             # View all existing data db                                                                   # View the database that is currently in use

Note that through show DBS you will not see the DB1 database you just created, mainly because there is no data in the database and you can only see it when there is data in it.

C. by deleting:

Use DB1                                                              # First step: Switch to the database you want to delete db.dropdatabase ()                                                    # Step Two: Delete the database in the current state

Tip: Methods for database-related operations can be viewed through the db.help () method.

2. Set operation

A collection is a set of documents. If a document in MongoDB is likened to a row of relational data, then a collection is the equivalent of a single table. Collections exist in the database, usually for ease of management, data of different formats and types should be inserted into different collections, but the collection does not have a fixed structure, which means that we can completely insert different formats and types of data into a single collection.

A, Increase:

When a document is inserted, the collection is created, as in the following example:

b, check:

Show Tables # or show collections.

C. by deleting:

Tip: Use the db. Collection name. Help () to see the methods associated with the collection operation.

3. Document Operation

(1) Increase

Add a piece of data: In addition to the ordered dictionary we will add in the previous instance directly in the Insert method, we can also use the following way, the global definition to insert the ordered dictionary data, and then directly assign this variable to the Insert method, the advantage is that the global data can be used multiple times.

Add multiple Data: The Insert method above can only insert one document data at a time, of course, it is possible to insert multiple documents at once, but you need to use the Insertmany method, the received document must be a list of the form, the following example:

(2) Check

The following example describes the query method for a fuzzy query, finds all the documents in the collection, or the first document, which uses Db.users.find (). Pretty () allows the results of the query to be displayed in a clearer structure, and no concrete instance results are shown here.

(3) Change

Document modification is mainly used in the Update method, its main parameters and usage are described as follows:

the update () method is used to update a document that already exists. The syntax format is as follows: Db.collection.update (<query>,   <update>, {upsert:<boolean>, Multi:<boolean>, Writeconcern:<document>}) Parameter description: Compare update db1.t1 set name='EGON', sex='Male'where name='Egon'  andAge=18; query: equivalent to the Where condition. Update:update objects and some updated operators (such as $, $inc ... And so on, equivalent to the Upsert after set: optional, the default is false, which means that if no record of the update is not updated or inserted, setting to True represents insert. Multi: optional, default = False, which indicates that only the first record found, set to True, represents all records found by the update. Writeconcern: Optional, throws an exception level. The update operation is inseparable: If two updates are sent at the same time, the server first executes first, and then executes the other, without destroying the document.

1, some content modification

(1) Upsert parameters

When the setting Upsert is not true, the result changes, and if there is no document object that meets the requirements, the content to be modified is inserted into the collection as a new document, as follows:

Multi Parameters:

However, when you set {"Multi": true}, the situation changes, changing all eligible documents to what you want to change, but for multiple object modifications, you must use the modifier, as in the following example:

2. New Document replacement

Mode 1:

Mode 2:

Python Road _day99_mongodb Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.