【Basic operation of mongoDB database】

Basic operation of mongoDB database

Connect to the mongoDb server
and enter the mongoDb installation directory

cd /data/mongodb/bin
insert image description here

Use ./mongo to connect to your local server & open a command window:
insert image description here

1. Connect/create database

After using ./mongo to connect to the server, switch to the specified database:
use + database name
will be created if it does not exist
insert image description here
. Because it is an empty database, it is also empty when show dbs:
connect to the yapi database:
insert image description here

2. Check the database

Check which show dbs or show databases the current database is

insert image description here

3. Delete the database

db.dropDatabase()
deletes the current database

Basic Operations of Tables

In mongoDB, a table is a collection.
If you want to operate on a table, you need to connect to the corresponding database first, and you can add, delete, modify, and query the table after passing the authentication.
If you operate the table without authentication, an error will be reported:
insert image description here

Authenticate via the auth command:

  • Format: db.auth("username", "password")
  • The example is as follows
    insert image description here
    Returning 1 means the authentication is successful, and then the data table can be operated

1. Create a table

Can useinsert records to create a table with records

insert image description here
insert image description here
can also be usedThe createCollection keyword creates an empty table
insert image description here

2. Delete table

In mongoDB, the table is said to be a collection
. Usedb.collection name.drop()delete table
insert image description here

3. Query table

usedb.collection name.find()Query all records of the collection
insert image description here

4. Update table

Insert a record
db.collection name.insert(document)
or
db.collection name.save(document)

in

  • save(): If the _id primary key exists, update the data, and if it does not exist, insert the data. This method is deprecated in the new version, you can use db.collection.insertOne() or db.collection.replaceOne() instead.
  • insert(): If the primary key of the inserted data already exists,
    an org.springframework.dao.DuplicateKeyException will be thrown, prompting that the primary key is duplicated, and the current data will not be saved

example

insert image description here

update update record
The syntax is as follows

db.collectio_name.update(
<query>,
<update>,
{
    
    
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
})

Parameter Description

  • query : query condition of update, similar to where in sql update query.
  • update : the object of update and some update operators (such as , ,inc...), etc., can also be understood as the set behind the sql update query
  • upsert: optional, this parameter means, if there is no update record, whether to insert objNew, true means insert, default is false, no insert.
  • multi : optional, mongodb defaults to false, and only updates the first record found. If this parameter is true, all multiple records found according to the conditions are updated.
  • writeConcern : optional, the level of exception thrown.

Example
Update name=cuicj2 in the testCreateTable collection to 3
testCreateTable.update({name:'cuicj2'},{$set:{name:'cuicj3'}});
is equivalent to update testCreateTable set name='cuicj3' where name ='cuicj2'
insert image description here
When there are multiple records in the default collection, only the first matching record will be updated, because the default value of the parameter multi is false (only the first record will be updated), and this field value needs to be updated when all matching records need to be updated Set to true
that is

testCreateTable.update({name:‘cuicj2’},{$set:{name:‘cuicj3’}},false,true);
insert image description here

delete records in the table
The MongoDB remove() function is used to remove data from a collection. It is a good habit to execute the find() command before executing the remove() function to determine whether the execution conditions are correct. The syntax is as follows:

db.collection_name.remove(
<query>,
<justOne>)
}

Parameter Description:

  • query : (optional) The criteria for the deleted documents.
  • justOne : (optional) if set to true or 1, only one document will be deleted, if this parameter is not set, or use the default value
  • false, deletes all documents matching the criteria.
    For example:

db.testCreateTable.remove({'name':'cuicj3'})
example:
insert image description here

If you only want to delete the first found record, you can set justOne to 1, as shown below:

db.collection_name.remove (delete condition, 1)
example
insert image description here

If you want to delete all data, you can use the following method:

db.testCreateTable.remove({})
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42808782/article/details/131189858