Get the App
SLTechnology News&Howtos  ›  Database  › 

Mongodb simple operation

Shulou Source: shulou.com Published: 2022-06-01 12:04:47 10月29日 Update

1. After performing mongod.exe server startup, you can follow the parameters to describe the database and logs that are started.

Eg:

Mongod.exe-bind_ip 127.0.0.1-logpath "F:\ data\ dbConf\ mongodb.log"-- logappend-- dbpath "F:\ data\ db"-- port 19901-- serviceName "mongodb1"-- serviceDisplayName "mongodbAll"-- install

2. Perform mongo.exe client connection

View all databases: show dbs

Syntax format for creating a database: use DATABASE_NAME

Delete the current database: db.dropDatabase ()

Switch to database: use DATABASE_NAME

Delete collection: db.collection.drop ()

MongoDB uses the insert () or save () methods to insert documents into the collection:

Db.COLLECTION_NAME.insert (document)

MongoDB uses the update () and save () methods to update documents in the collection:

The update () method is used to update existing documents. The syntax format is as follows:

Db.collection.update (

, / / query: the query condition of update, which is similar to that after where in the sql update query.

, / / update: the object of update and some updated operators (such as $, $inc...), etc., can also be understood as the following set in the sql update query

{

Upsert: / / upsert: optional, this parameter means that if there is no record of update, whether to insert objNew,true is insert, default is false, do not insert.

Multi: / / multi: optional. Mongodb defaults to false. Only the first record found is updated. If this parameter is true, all records found by condition will be updated.

WriteConcern: / / writeConcern: optional, the level at which the exception is thrown.

}

)

The save () method replaces an existing document with an incoming document. The syntax format is as follows:

Db.collection.save (

{

WriteConcern:

}

)

Db.col.update ({'title':'MongoDB tutorial'}, {$set: {'title':'MongoDB'}})

MongoDB deletes a document

Parameter description:

Document: document data.

WriteConcern: optional, the level at which the exception is thrown.

The basic syntax format of the remove () method is as follows:

Db.collection.remove (

)

Remove all data: db.col.remove ({})

The syntax format of MongoDB query data is as follows:

Db.COLLECTION_NAME.find ()

Read data in a readable manner

Db.col.find () .pretty ()

Similar statements in the example RDBMS of the action format

Equals {:} db.col.find ({"by": "Rookie tutorial"}) .pretty () where by = "Rookie tutorial"

Less than {: {$lt:} db.col.find ({"likes": {$lt:50}}) .pretty () where likes

< 50 小于或等于 {:{$lte:}} db.col.find({"likes":{$lte:50}}).pretty() where likes 50 大于或等于 {:{$gte:}} db.col.find({"likes":{$gte:50}}).pretty() where likes >

= 50

Not equal to {: {$ne:} db.col.find ({"likes": {$ne:50}}) .pretty () where likes! = 50

The conditional operators in MongoDB are:

(>) greater than-$gt

(=) greater than or equal to-$gte

(100)

I wrote down the above while typing the website myself.

Tags: Data document format database method syntax update parameter query tutorial operator condition level rookie tutorial statement rookie multiple client client object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Huawei MySQL Shulou Technology OPPO Reno