mongodb Database introduction CURD simple operation example

  • 2020-12-16 06:11:28
  • OfStack

An example of CURD is presented in this paper. To share for your reference, the details are as follows:

View all databases


show dbs;
show databases; # Some versions may not work 

Using a database


use  The database name 

View the collection (the collection is mysql's table)


show tables/collections

See the help


db.help()

Create the library

MongoDB's library is created implicitly, so you can use1 libraries that don't exist,

You can then create the library by creating colletion under the library.

Delete database


db.dropDatabase();

Create a collection


db.createCollection(name, options)

Example: > db.createCollection('student');

Delete the collection


db.collectionName.drop();

Insert document (add data)


db.COLLECTION_NAME.insert(document)

Example:


db.student.insert({name:'zhang san',age:10});

Delete the document


db.COLLECTION_NAME.remove( Query expression, options )

Example:


use  The database name 

0

Parameter 2:

justOne: (optional) If set to true or 1, only 1 document is deleted, equivalent to limit 1.

Update the document


use  The database name 

1

Example:


use  The database name 

2

Description:

If you do not use $set, the original whole data will be changed to the new data,

With $set, only the columns to be modified are modified.

$unset removes a column $rename renames a column $inc increases the value of a column

Query the document


db.collection.find(query, projection)

Example:


use  The database name 

4

I hope this article has been helpful to the MongoDB database programming.


Related articles: