MongoDB database decluttering required .

Mondo Technology Updated on 2024-01-31

1. Database Operations

1. Create a new database (if the database does not exist, create a database, otherwise switch to the specified database.) )

Enter use db01

2. Delete the database.

First, use db01 to switch to the database you want to delete.

Enter dbdropdatabase: Deletes the current database.

Second, the collection operation].

1. Create a new collection: dbcreatecollection(collection name).

Enter dbcreatecollection('list01')

Input: show collections or show tables

2. The second way to create a collection: dbThe name of the collection. insert()

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

Enter dblist02.insert()

3. Delete collection: dbThe name of the collection. drop()

Enter dblist02.drop() deletes the specified collection.

Enter: show collections to view all collections.

4. Collection rename: dbOld collection name. renamecollection("new collection name").

Enter dblist01.renamecollection(“list02”)

Enter: show collections to view all collections.

3. Document Operation] Find documents.

1. Find the document: dbThe name of the collection. find

Unconditionally: dblist02.find() [is equal to viewing all documents in the current collection].

Conditional query: dblist02.find()

2. Query result formatting: dbThe name of the collection. find().pretty()

Formatting: dblist02.find().pretty()

3. Comparison operators (=, <, =, >, =, !=)

Equals (=) is the default equals judgment, with no operator.

Less than ( Find documents with IDs less than 2: db.list02.find(})

Less than or equal to (<=): LTE

Greater than ( >gt

Greater than or equal to (>=): gte

Not equal to (!= ):ne

Query documents with IDs other than 2,4.

4. Range query (in, nin).

Documentation in "[: $in:[ array].

db.list02.find(})

Not in the "[ document: $nin:[ array].

5. Logical operators (and, or).

and : Write multiple conditions in json.

or: Use $or, the value is an array, and each element in the array is JSON

db.list02.find(,]

6. Interval query (limit, skip).

Used to read a specified number of documents: dbThe name of the collection. find().limit (quantity).

db.list02.find().limit(1)

Used to skip a specified number of files: dbThe name of the collection. find().limit (quantity).

db.list02.find().skip(1)

7. Sort: dbThe name of the collection. find().sort()

Parameter 1 is in ascending order.

Parameter -1 is in descending order.

db.con1.find().sort()

8. Fuzzy query: equivalent to %, note no quotation marks.

db.con1.find()

db.con1.find()

Fourth, document operations] added.

Insert document: dbThe name of the collection. insert (document).

db.con.insert([, where both data are inserted.

Fifth, the document operation is deleted.

db.The name of the collection. remove

db.con1.remove()

Sixth, the document operation is updated.

Update Documentation: dbThe name of the collection. update(condition,).

db.con1.update(,}

Related Pages