MongoDB concept understanding, MongoDB shell connection to Mongodb service, mongodbshell
MongoDB concept understanding
MongoDB shell connection to Mongodb Service
Standard URI connection Syntax:
Mongodb: // [username: password @] host1 [: port1] [, host2 [: port2],... [, hostN [: portN] [/[database] [? Options] mongodb: // This is a fixed format and must be specified. Username: password @ option. If this option is set, after connecting to the database server, the driver will attempt to log on to the database. host1 must specify at least one host. host1 is the only URI to be filled in. It specifies the address of the server to be connected. To connect to a replica set, specify multiple host addresses. PortX is an optional port. If this parameter is not specified, the default value is 27017/database. If username: password @ is specified, connect to and verify that you log on to the specified database. If this parameter is not specified, the test database is enabled by default .? Options is the connection option. If you do not use the/database, you must add /. All connection options are key-value pairs name = value. Key-value pairs are separated by & or; (semicolon ).
The "show dbs" command displays a list of all data.
Run the "db" command to display the current database object or set.
Run the "use" command to connect to a specified database or create a database:
use DATABASE_NAME
The newly created database is not in the Database List (show dbs). to display it, we need to insert some data into the database.
The syntax format for deleting a MongoDB database is as follows:
db.dropDatabase()
Delete collection
Set deletion syntax format: db. collection. drop ()
MongoDB uses the insert () or save () method to insert a document into the collection. The syntax is as follows:
db.COLLECTION_NAME.insert(document)
The data structure of the document is basically the same as that of JSON.
All data stored in the set is in BSON format.
BSON is a type of json Binary storage format, referred to as Binary JSON.
View the inserted document:
Db. col. find () # If you want to display data in read-friendly format, the format is as follows: db. col. find (). pretty ()
MongoDB update document
MongoDB uses the update () and save () Methods to update documents in the collection. Next, let's take a closer look at the application of the two functions and their differences.
Update () method
The update () method is used to update an existing document. The syntax format is as follows:
db.collection.update(
,
, { upsert:
, multi:
, writeConcern:
} )
Parameter description:
Query: the query condition of the update statement, similar to the where clause after the SQL update statement. Update: update object and some updated operators (such as $, $ inc ...) can also be understood as the upsert after the set in the SQL update query: optional, this parameter means that if there is no update record, whether to insert objNew, true is insert, the default value is false. Multi: Optional. The default value of mongodb is false. Only the first record found is updated. If this parameter is set to true, all multiple records are updated according to the condition. WriteConcern: (optional) exception level.
Save () method
The save () method replaces an existing document by passing in the document. The syntax format is as follows:
db.collection.save(
, { writeConcern:
})
Parameter description:
Document: document data. WriteConcern: (optional) exception level.
The deleteOne () and deleteMany () methods are recommended.
For example, to delete all documents under a collection:
db.COLLECTION_NAME.deleteMany({})
Delete all documents whose status is equal to:
db.COLLECTION_NAME.deleteMany({ status : "A" })
Delete a document whose status is D:
db.COLLECTION_NAME.deleteOne( { status: "D" } )