MongoDB study notes (five)-addition, deletion, modification, and check

This article will introduce the related operations of adding, deleting, modifying and checking the MongoDB database. For details, please refer to the official manual: https://docs.mongodb.com/manual/

This page also provides MongoDB Web Shell , which can be used to simulate the database environment online to facilitate learning.


increase

db.collection.insert()

-Insert one or more documents into the collection

When we insert a document into the collection, if the _id attribute is not specified for the document, the database will automatically add _id to the document, which is used as the unique identifier of the document.

  • Insert one
db.c1.insert({name:"tom", age:"18", gender:"男"})
  • Insert multiple
db.c1.insert([
    {name:"tom", age:"18", gender:"男"},
    {name:"jenney", age:"16", gender:"女"},
    {name:"klay", age:"20", gender:"男"}
])
  • _id can be specified by yourself, the database will not be automatically added, and you must specify its own uniqueness
db.c1.insert({_id:"hello", name:"tom", age:"18", gender:"男"})
  • ObjectId() can be used to generate id, generated by timestamp and machine code
ObjectId()

db.collection.insertOne()

-Insert a document object

Both insertOne() and insertMany() can be regarded as the split of insert(), which is a new function added after version 3.2, in order to standardize the operation.

db.c1.insertOne({name:"tom", age:"18", gender:"男"})

db.collection.insertMany()

-Insert multiple document objects

db.c1.insertMany([
    {name:"tom", age:"18", gender:"男"},
    {name:"jenney", age:"16", gender:"女"},
    {name:"klay", age:"20", gender:"男"}
])

check

db.collection.find()

-find() is used to query all documents in the collection that meet the conditions , and the return is an array

  • Query all documents in the collection
db.c1.find() 
db.c1.find({}) 
  • find() can receive an object as a condition parameter, and the query attribute is a document with the specified value {field:value}
db.c1.find({age:"18"}) 
  • The collection of query results
db.c1.find({_id:"hello", name:"wyc"})
  • The first file of the query result
db.c1.find({age:"18"})[1]
  • The name of the first file in the query result
db.c1.find({age:"18"})[1].name
  • The number of all query results
db.collection.find({age:"18"}).count()

db.collection.findOne()

-Used to query the first document that meets the conditions in the collection, and returns a document object

db.c1.findOne({age:"18"})

change

db.collection.update()

  • update(query condition, new object) will replace the old object with the new object, only one will be modified by default
db.c1.update({name:"tom"},{age:28})
  • If you need to modify rather than replace the specified attribute, you need to use the "modification operator" to complete the modification

    $set can be used to modify the specified attributes in the document
    $unset can be used to delete the specified attributes of the document
    ...

db.c1.update(
    {"_id":"123"},
    {$set:{
        gender:"男"
        address:"北京"
        }}
)
  • update() will only modify one by default. To modify multiple, add another parameter {multi: true}
db.c1.update(
    {"_id":"123"},
    {$set:{
        gender:"男"
        address:"北京"
        },
    {multi:true}
)

db.collection.updateMany()

 -Modify multiple eligible documents at the same time

db.collection.updateOne()

 -Modify a document that meets the conditions


delete

db.collection.remove()

-remove can delete documents based on conditions. The method of passing conditions is the same as find(). Delete all documents that meet the conditions

  • Delete the specified document
db.c1.remove({age:28})
  • If the second parameter of remove() is passed a true, only one will be deleted
db.c1.remove({age:28}, true)
  • Report an error if you don’t pass the conference
db.c1.remove()
  • Only pass an empty parameter {}, which is equivalent to emptying the collection (delete one by one, poor performance), show still exists after deletion
db.c1.remove({})
show collections

db.collection.deleteOne()

-Delete a document

db.c1.deleteOne({age:28})

db.collection.deleteMany()

-Delete all documents

db.c1.deleteMany({age:28})

db.collection.drop()

-Delete the entire collection, show does not exist after deletion

db.c1.drop()
show collections

db.dropDatabase() 

-Delete the entire database

use testdb
db.dropDatabase() 

isDel field

-Add a specified field such as isDel to represent whether the document is used, so there is no need to delete the data completely, which is convenient for maintenance

Guess you like

Origin blog.csdn.net/qq_14997473/article/details/89681081