Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB daily operation and maintenance-01 common command summary

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Summary of Common commands in MongoDB (1)

-add, delete, change, check

One: increase (insert)

Two: delete (delete)

Third: change (update)

Four: check (select)

-connect to the database

[mongo@cjc bin] $pwd

/ usr/local/mongodb/bin

[mongo@cjc bin] $. / mongo-- port 27017

-create new databases cjcdb and cjctest

MongoDB Enterprise > use cjcdb

-insert data before the database is created

MongoDB Enterprise > db.tab01.insert ({"time": "2018-11-25 18:20"})

MongoDB Enterprise > use cjctest

MongoDB Enterprise > db.tab01.insert ({"time": "2018-11-25 18:21"})

-check what databases are available

MongoDB Enterprise > show dbs

...

Cjcdb 0.000GB

Cjctest 0.000GB

-MongoDB deletes database cjctest

MongoDB Enterprise > use cjctest

Switched to db cjctest

-View the currently connected database

MongoDB Enterprise > db

Cjctest

-Delete the database (proceed with caution)

MongoDB Enterprise > db.dropDatabase ()

{"dropped": "cjctest", "ok": 1}

-MongoDB creates a collection

MongoDB Enterprise > use cjcdb

Switched to db cjcdb

MongoDB Enterprise > db.createCollection ("tab02")

{"ok": 1}

MongoDB Enterprise > db.createCollection ("tab03")

{"ok": 1}

In MongoDB, you don't need to create a collection. When you insert some documents, MongoDB automatically creates collections.

MongoDB Enterprise > db.tab05.insert ({"id": "123456"})

WriteResult ({"nInserted": 1})

MongoDB Enterprise > show collections

Tab01

Tab02

Tab03

Tab05

.

=

One: increase (insert)

=

Insert document

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

MongoDB Enterprise > use cjcdb

Switched to db cjcdb

MongoDB Enterprise > db

Cjcdb

MongoDB Enterprise >

Db.tab02.insert ({id:1, name:'a'})

Db.tab02.insert ({id:1, name:'ab'})

Db.tab02.insert ({id:1, name:'cae'})

Db.tab02.insert ({id:2, name:'b'})

Db.tab02.insert ({id:3, name:'c'})

Db.tab02.insert ({id:4, name:'d'})

Db.tab02.insert ({id:5, name:'e'})

Db.tab02.insert ({id:6, name:'f'})

Db.tab02.insert ({id:7, name:'g'})

Db.tab02.insert ({id:8, name:'h'})

Db.tab02.insert ({id:9, name:'i'})

Db.tab02.insert ({id:10, name:'j'})

Db.tab02.insert ({id:10, name:'h'})

Db.tab02.insert ({id:10, name:'i'})

Db.tab02.insert ({id:10,c100:'h',c200:'f',c300:' industry is good at diligence'})

Db.tab02.insert ({ccc:10,c10:'h',c20:'f',c30:' acts in thought'})

-We can also define the data as a variable, as follows:

MongoDB Enterprise > document= ({title: 'blog.itpub'

... Description: 'chenoracleblog-chenjuchao'

... By: 'mongo test'

... Url: 'http://blog.itpub.net/29785807/'

.)

MongoDB Enterprise > db.tab02.insert (document)

=

Two: delete (delete)

=

-delete

The MongoDB remove () function is used to remove data from the collection.

You can use the update () function to update MongoDB data. It is a good habit to execute the find () command before executing the remove () function to determine whether the condition is correct.

MongoDB Enterprise > db

Cjcdb

-delete the id=10 data in the tab02 table

MongoDB Enterprise > db.tab02.find ({id:10})

{"_ id": ObjectId ("5bfacc790063f23910737472"), "id": 10, "name": "j"}

{"_ id": ObjectId ("5bfacc790063f23910737473"), "id": 10, "name": "h"}

{"_ id": ObjectId ("5bfacc790063f23910737474"), "id": 10, "name": "I"}

{"_ id": ObjectId ("5bfacc790063f23910737475"), "id": 10, "c100": "h", "c200": "f", "c300": "diligence"}

MongoDB Enterprise >

MongoDB Enterprise > db.tab02.remove ({id:10})

WriteResult ({"nRemoved": 4})

-the remove () method is out of date and is now officially recommended to use the deleteOne () and deleteMany () methods.

Delete the first document where id equals 1:

MongoDB Enterprise > db.tab02.find ({id:1})

{"_ id": ObjectId ("5bfacc790063f23910737467"), "id": 1, "name": "a"}

{"_ id": ObjectId ("5bfacc790063f23910737468"), "id": 1, "name": "ab"}

{"_ id": ObjectId ("5bfacc790063f23910737469"), "id": 1, "name": "cae"}

MongoDB Enterprise > db.tab02.deleteOne ({id:1})

{"acknowledged": true, "deletedCount": 1}

MongoDB Enterprise > db.tab02.find ({id:1})

{"_ id": ObjectId ("5bfacc790063f23910737468"), "id": 1, "name": "ab"}

{"_ id": ObjectId ("5bfacc790063f23910737469"), "id": 1, "name": "cae"}

-Delete all documents with id equal to 1:

MongoDB Enterprise > db.tab02.deleteMany ({id:1})

{"acknowledged": true, "deletedCount": 2}

MongoDB Enterprise > db.tab02.find ({id:1})

MongoDB Enterprise >

-for example, delete all documents under the collection:

-truncate table.

MongoDB Enterprise > db.tab02.find ()

{"_ id": ObjectId ("5bfacc790063f2391073746a"), "id": 2, "name": "b"}

{"_ id": ObjectId ("5bfacc790063f2391073746b"), "id": 3, "name": "c"}

{"_ id": ObjectId ("5bfacc790063f2391073746c"), "id": 4, "name": "d"}

{"_ id": ObjectId ("5bfacc790063f2391073746d"), "id": 5, "name": "e"}

{"_ id": ObjectId ("5bfacc790063f2391073746e"), "id": 6, "name": "f"}

{"_ id": ObjectId ("5bfacc790063f2391073746f"), "id": 7, "name": "g"}

{"_ id": ObjectId ("5bfacc790063f23910737470"), "id": 8, "name": "h"}

{"_ id": ObjectId ("5bfacc790063f23910737471"), "id": 9, "name": "I"}

{"_ id": ObjectId ("5bfacc7a0063f23910737476"), "ccc": 10, "c10": "h", "c20": "f", "c30": "thinking is done"}

{"_ id": ObjectId ("5bfaccb10063f23910737477"), "title": "blog.itpub", "description": "chenoracleblog", "by": "mongo test", "url": "http://blog.itpub.net/29785807/"}"

MongoDB Enterprise > db.tab02.deleteMany ({})

{"acknowledged": true, "deletedCount": 10}

MongoDB Enterprise > db.tab02.find ()

MongoDB Enterprise >

-Delete the collection

-drop table.

MongoDB Enterprise > show collections

Tab01

Tab02

Tab03

MongoDB Enterprise > db.tab02.drop ()

True

MongoDB Enterprise > show collections

Tab01

Tab03

=

Third: change (update)

=

-MongoDB updates the document

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

MongoDB Enterprise > db.tab05.find ({'id':1})

{"_ id": ObjectId ("5bfafab30063f23910737478"), "id": 1, "name": "a"}

{"_ id": ObjectId ("5bfafab30063f23910737479"), "id": 1, "name": "aa"}

{"_ id": ObjectId ("5bfafab30063f2391073747a"), "id": 1, "name": "aaa"}

-change the first piece of data that meets the criteria

MongoDB Enterprise > db.tab05.update ({'id':1}, {$set: {name:'chenjuchao'}})

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

MongoDB Enterprise > db.tab05.find ({'id':1})

{"_ id": ObjectId ("5bfafab30063f23910737478"), "id": 1, "name": "chenjuchao"}

{"_ id": ObjectId ("5bfafab30063f23910737479"), "id": 1, "name": "aa"}

{"_ id": ObjectId ("5bfafab30063f2391073747a"), "id": 1, "name": "aaa"}

-multi:true changes all data that meets the criteria

MongoDB Enterprise > db.tab05.update ({'id':1}, {$set: {name:'chenjuchao'}}, {multi:true})

WriteResult ({"nMatched": 3, "nUpserted": 0, "nModified": 2})

MongoDB Enterprise > db.tab05.find ({'id':1})

{"_ id": ObjectId ("5bfafab30063f23910737478"), "id": 1, "name": "chenjuchao"}

{"_ id": ObjectId ("5bfafab30063f23910737479"), "id": 1, "name": "chenjuchao"}

{"_ id": ObjectId ("5bfafab30063f2391073747a"), "id": 1, "name": "chenjuchao"}

=

Four: check (select)

=

1 (! =) is not equal to-$ne

2 (in) contains-$in

3 (not in)-$nin does not contain

4 (>) is greater than-$gt

5 (> =) greater than or equal to-$gte

6 (db.tab01.find ()

-add pretty () to make it easier to read

Db.tab01.find () .pretty ()

=

Equal to

=

MongoDB Enterprise > db.tab01.find ({age:20})

{"_ id": ObjectId ("5bfccbc636850458dff055b0"), "name": "a", "age": 20}

MongoDB Enterprise > db.tab01.find ({age:'20'})

MongoDB Enterprise >

MongoDB Enterprise > db.tab01.find ({age:'10'})

{"_ id": ObjectId ("5bfcc7b036850458dff055a5"), "name": "a", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a6"), "name": "ab", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a7"), "name": "bac", "age": "10"}

MongoDB Enterprise > db.tab01.find ({age:10})

MongoDB Enterprise >

=

1 (! =) is not equal to-$ne

=

MongoDB Enterprise > db.tab01.find ({name: {$ne:'a'}})

{"_ id": ObjectId ("5bfcc7b036850458dff055a6"), "name": "ab", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a7"), "name": "bac", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a8"), "name": "d", "age": "12"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a9"), "name": "e", "age": "13"}

{"_ id": ObjectId ("5bfcc7b036850458dff055aa"), "name": "aaa", "age": "14"}

{"_ id": ObjectId ("5bfcc7b036850458dff055ab"), "name": "f", "age": "15"}

{"_ id": ObjectId ("5bfcc7b036850458dff055ac"), "name": "g", "age": "16"}

{"_ id": ObjectId ("5bfcf37936850458dff055b1"), "name": "nba", "age": 21}

{"_ id": ObjectId ("5bfcf37936850458dff055b2"), "name": "cba", "age": 21}

=

2 (in) contains-$in

=

MongoDB Enterprise > db.tab01.find ({name: {$in: ['cba','nba']}})

{"_ id": ObjectId ("5bfcf37936850458dff055b1"), "name": "nba", "age": 21}

{"_ id": ObjectId ("5bfcf37936850458dff055b2"), "name": "cba", "age": 21}

=

3 (not in)-$nin does not contain

=

MongoDB Enterprise > db.tab01.find ({age: {$nin: ['10, nin, 12, 13, 14, 15, 21]})

{"_ id": ObjectId ("5bfcc7b036850458dff055ac"), "name": "g", "age": "16"}

{"_ id": ObjectId ("5bfcc7b036850458dff055ad"), "name": "a", "age": "17"}

{"_ id": ObjectId ("5bfcc7b136850458dff055ae"), "name": "a", "age": "18"}

{"_ id": ObjectId ("5bfccbc536850458dff055af"), "name": "a", "age": 19}

{"_ id": ObjectId ("5bfccbc636850458dff055b0"), "name": "a", "age": 20}

=

4 (>) is greater than-$gt

=

MongoDB Enterprise > db.tab01.find ({age: {$gt:'17'}})

{"_ id": ObjectId ("5bfcc7b136850458dff055ae"), "name": "a", "age": "18"}

=

5 (> =) greater than or equal to-$gte

=

MongoDB Enterprise > db.tab01.find ({age: {$gte:'17'}})

{"_ id": ObjectId ("5bfcc7b036850458dff055ad"), "name": "a", "age": "17"}

{"_ id": ObjectId ("5bfcc7b136850458dff055ae"), "name": "a", "age": "18"}

=

6 (db.tab01.find ({age: {$lt:'12'}})

{"_ id": ObjectId ("5bfcc7b036850458dff055a5"), "name": "a", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a6"), "name": "ab", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a7"), "name": "bac", "age": "10"}

=

7 (db.tab01.find ({age: {$lte:'12'}})

{"_ id": ObjectId ("5bfcc7b036850458dff055a5"), "name": "a", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a6"), "name": "ab", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a7"), "name": "bac", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a8"), "name": "d", "age": "12"}

=

8 Fuzzy query

=

-query documents for which name contains "a":

MongoDB Enterprise > db.tab01.find ({name:/a/})

{"_ id": ObjectId ("5bfcc7b036850458dff055a5"), "name": "a", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a6"), "name": "ab", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a7"), "name": "bac", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055aa"), "name": "aaa", "age": "14"}

{"_ id": ObjectId ("5bfcc7b036850458dff055ad"), "name": "a", "age": "17"}

{"_ id": ObjectId ("5bfcc7b136850458dff055ae"), "name": "a", "age": "18"}

{"_ id": ObjectId ("5bfccbc536850458dff055af"), "name": "a", "age": 19}

{"_ id": ObjectId ("5bfccbc636850458dff055b0"), "name": "a", "age": 20}

-query documents whose name fields begin with "a":

MongoDB Enterprise > db.tab01.find ({name:/ ^ a /})

{"_ id": ObjectId ("5bfcc7b036850458dff055a5"), "name": "a", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a6"), "name": "ab", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055aa"), "name": "aaa", "age": "14"}

{"_ id": ObjectId ("5bfcc7b036850458dff055ad"), "name": "a", "age": "17"}

{"_ id": ObjectId ("5bfcc7b136850458dff055ae"), "name": "a", "age": "18"}

{"_ id": ObjectId ("5bfccbc536850458dff055af"), "name": "a", "age": 19}

{"_ id": ObjectId ("5bfccbc636850458dff055b0"), "name": "a", "age": 20}

-query documents whose name field ends with "a":

-db.tab01.insert ({name:'nba',age:21})

-db.tab01.insert ({name:'cba',age:21})

MongoDB Enterprise > db.tab01.find ({name:/a$/})

{"_ id": ObjectId ("5bfcc7b036850458dff055a5"), "name": "a", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055aa"), "name": "aaa", "age": "14"}

{"_ id": ObjectId ("5bfcc7b036850458dff055ad"), "name": "a", "age": "17"}

{"_ id": ObjectId ("5bfcc7b136850458dff055ae"), "name": "a", "age": "18"}

{"_ id": ObjectId ("5bfccbc536850458dff055af"), "name": "a", "age": 19}

{"_ id": ObjectId ("5bfccbc636850458dff055b0"), "name": "a", "age": 20}

{"_ id": ObjectId ("5bfcf37936850458dff055b1"), "name": "nba", "age": 21}

{"_ id": ObjectId ("5bfcf37936850458dff055b2"), "name": "cba", "age": 21}

=

9 AND

=

The find () method of MongoDB can pass in multiple keys (key), each key separated by a comma, which is the AND condition of a regular SQL.

The syntax format is as follows:

MongoDB Enterprise > db.tab01.find ({name:'a', age:20})

{"_ id": ObjectId ("5bfccbc636850458dff055b0"), "name": "a", "age": 20}

MongoDB Enterprise > db.tab01.find ({name:'a',age: {$gt:18}})

{"_ id": ObjectId ("5bfccbc536850458dff055af"), "name": "a", "age": 19}

{"_ id": ObjectId ("5bfccbc636850458dff055b0"), "name": "a", "age": 20}

=

10 OR

=

The MongoDB OR conditional statement uses the keyword $or:

MongoDB Enterprise > db.tab01.find ({$or: [{name:'nba'}, {age:'12'}]})

{"_ id": ObjectId ("5bfcc7b036850458dff055a8"), "name": "d", "age": "12"}

{"_ id": ObjectId ("5bfcf37936850458dff055b1"), "name": "nba", "age": 21}

-AND and OR are used together

MongoDB Enterprise > db.tab01.find ({name:'a',$or: [{age:'18'}, {age:20}]})

{"_ id": ObjectId ("5bfcc7b136850458dff055ae"), "name": "a", "age": "18"}

{"_ id": ObjectId ("5bfccbc636850458dff055b0"), "name": "a", "age": 20}

=

11 Limit

=

MongoDB Limit () method

If you need to read a specified number of data records in MongoDB, you can use MongoDB's Limit method, and the limit () method accepts a numeric parameter that specifies the number of records to read from MongoDB.

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

MongoDB Enterprise > db.tab01.find ({age:'10'}) .limit (1)

{"_ id": ObjectId ("5bfcc7b036850458dff055a5"), "name": "a", "age": "10"}

MongoDB Enterprise > db.tab01.find () limit (2)

{"_ id": ObjectId ("5bfcc7b036850458dff055a5"), "name": "a", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a6"), "name": "ab", "age": "10"}

=

12 Skip

=

MongoDB Skip () method

In addition to using the limit () method to read a specified amount of data, we can also use the skip () method to skip the specified amount of data, and the skip method also accepts a numeric parameter as the number of skipped records.

Note: the default parameter of the skip () method is 0.

The format of the skip () method script syntax is as follows:

> db.COLLECTION_NAME.find () .limit (NUMBER) .skip (NUMBER)

Example

The following example displays only the second document data

MongoDB Enterprise > db.tab01.find ({age:'10'}) .limit (1) .skip (2)

{"_ id": ObjectId ("5bfcc7b036850458dff055a7"), "name": "bac", "age": "10"}

=

13 MongoDB sort Sort

=

When skip (), limilt (), and sort () are executed together, the order of execution is sort (), then skip (), and finally the displayed limit ().

MongoDB sort () method

In MongoDB, you use the sort () method to sort the data, and the sort () method can specify the sorted fields by parameters and use 1 and-1 to specify how to sort, where 1 is an ascending sort and-1 is used for descending sort.

Grammar

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

> db.COLLECTION_NAME.find () .sort ({KEY:1})

Example

-Ascending order

MongoDB Enterprise > db.tab01.find () .sort ({age:1})

{"_ id": ObjectId ("5bfccbc536850458dff055af"), "name": "a", "age": 19}

{"_ id": ObjectId ("5bfccbc636850458dff055b0"), "name": "a", "age": 20}

{"_ id": ObjectId ("5bfcf37936850458dff055b1"), "name": "nba", "age": 21}

{"_ id": ObjectId ("5bfcf37936850458dff055b2"), "name": "cba", "age": 21}

{"_ id": ObjectId ("5bfcc7b036850458dff055a5"), "name": "a", "age": "10"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a8"), "name": "d", "age": "12"}

{"_ id": ObjectId ("5bfcc7b036850458dff055a9"), "name": "e", "age": "13"}

{"_ id": ObjectId ("5bfcc7b036850458dff055aa"), "name": "aaa", "age": "14"}

.

-descending order

MongoDB Enterprise > db.tab01.find () .sort ({age:-1})

{"_ id": ObjectId ("5bfcc7b136850458dff055ae"), "name": "a", "age": "18"}

{"_ id": ObjectId ("5bfcc7b036850458dff055ad"), "name": "a", "age": "17"}

{"_ id": ObjectId ("5bfcc7b036850458dff055ac"), "name": "g", "age": "16"}

.

-take the maximum age

MongoDB Enterprise > db.tab01.find () .sort ({age:-1}) .limit (1)

{"_ id": ObjectId ("5bfcc7b136850458dff055ae"), "name": "a", "age": "18"}

-take the second largest value of age

MongoDB Enterprise > db.tab01.find () .sort ({age:-1}) .limit (1) .skip (1)

{"_ id": ObjectId ("5bfcc7b036850458dff055ad"), "name": "a", "age": "17"}

-take the top three values of age

MongoDB Enterprise > db.tab01.find () .sort ({age:-1}) .limit (1) .skip (2)

{"_ id": ObjectId ("5bfcc7b036850458dff055ac"), "name": "g", "age": "16"}

= =

14 number of count query records

= =

Use the count () method to query the number of records in the table, for example, the following command queries the number of records in the table users:

MongoDB Enterprise > db.tab01.find (). Count ()

fourteen

Welcome to follow my Wechat official account "IT Little Chen" and learn and grow together!

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report