Basic usage skills of MongoDB and mongodb
This section describes some basic operations of MongoDB.
Create user-specified database login get more command insert document MongDB common query comparison
Create user
> Db. createUser ({user: "<your user name>", pwd: "<your password>", roles: [{role: "readWrite", db: "<the database you want to create>"}]})Specified database
# For example, if I have created an admin database, the switch mode is> use admin.
Note:Use the use command to specify the database used by Sanjin. If the database does not exist, a new database is created.
Login
> Db. auth ("<account>", "<password> ")
When the console output is1Indicates successful
Get more commands
> db.help()
More commands will be listed.
Insert document
MongoDB manages databases through collections. Similar to database tables, a Collection contains multiple documents, which are similar to the records of database tables. MongoDB does not need to create a set on display. You can add documents to the set directly. For example:
Db. test. insert ({_ id: "springboot", desc: "create distributed server", tag: ["IT", "Spring"],})Introduction Parameters
_ Id:
Fields starting with the line below MongoDB have special meanings, indicating the document's primary keyIf the document does not provide a primary key, the system automatically generates a primary key of the ObjectID type. Desc: Entry description tag: saved as an array. The MongDB field type can be string, number, boolean, date, or document type. Comment: This field is a JSON document. Comparison of common MongDB queries
| MongoDB format |
Code Format |
Instance |
| $ Gt |
> |
"Comment. good": {$ gt: 1000} indicates that comment. good is an element greater than 1000. |
| $ Gte |
> = |
"Comment. good": {$ gte: 1000} |
| $ Lt |
< |
"Comment. good": {$ lt: 1000} |
| $ Lte |
<= |
"Comment. good": {$ lte: 1000} |
| $ Eq |
= |
"Comment. good": {$ eq: 1000} |
| $ Ne |
! = |
"Comment. good": {$ ne: 1000} |
| $ In |
In |
"Tag": {$ in: ["IT", "template language"] Find All tags that contain IT and template languages} |
| $ Nin |
Not in |
Not in |