Some commands used by MongoDb

# Show all libraries
show dbs
# Show all tables
show tables
show collections

# Delete a table, the following means to delete the user table
db.user.drop()

# Delete the database, you need to switch to the deleted database first
use testdb
#Switch to testdb db.dropDatabase()


#
数据的 Insert # Syntax: db.collection_name.insert(document)
# as follows: the effect of the following two is the same
db.user.insert({id:1, username:'woodie', age: 20})
db. user.save({id:1, username:'woodie', age: 20})


# Data Update
# Syntax
db.collection.update(
    <query>,
    <update>,
    [
        upsert: <boolean>,
        multi: <boolean>,
        writeConcern: <document>
    ]
)
Parameter description:
query: update query conditions, similar After where in the sql update query.
update: the object of update and some update operators (such as inc...), etc., can also be understood as the
upsert after set in the sql update query : optional. This parameter means that if there is no update record, whether Insert objNew, true is insert, the default is
false, not insert.
multi: Optional, mongodb defaults to false, only the first record found is updated, if this parameter is true,
all the multiple records found according to the conditions will be updated.
writeConcern: Optional, the level of the exception thrown.

eg:
db.house.update({hid:1}, {$set:{title:'abcd'}}) #If
written as below, other fields will be deleted
db.house.update({hid:1}, {title:'abcd'}) #If
you update a field that does not exist, the field will be added


        #Delete data
#Syntax
db.collection.remove(
    <query>,
    { justOne:<boolean>,         writeConcern: <document>     } )




#Parameter description query: (optional) the condition of the deleted document.
justOne: (Optional) If set to true or 1, only one document will be deleted. If this parameter is not set or the default value of false is used,
all documents matching the conditions will be deleted .
writeConcern: (optional) the level of the exception thrown

#eg:
db.house.remove((title:'abcd'))
# If no parameters are passed in, it means to delete all data
db.house.remove(())

官方推荐描述的方法
# Delete the first matched data
db.house.delectOne({title:'abcd'})
# Delete all the matched data
db.house.delectmany({title:'abcd'})


#
查数据# Add .pretty() at the end to beautify the query data
db.user.find() #Query all data 
db.user.find((),(id:1,username:1)) # Query only the id and username fields 
db.user.find().count()
#Query the number of data  db.user.find((id:1))
#Query the data with id 1  db.user.find((age: {$lte:21}})
#Query data less than or equal to 21  db.user.find({age:{$lte:21}, id:{$gte:2}}) #and query, age is less than or equal to 21 and id is greater than or equal to 2 
db.user.find({$or:[{id:1},{id:2}]}) #Query id=1 or id=2 


#Paging query: Skip() skip a few, limit() query the number of  db.user.find().limit(2).skip(1) #Skip 1 data, query 2 data 
db.user. find().sort({id:-1}) #Sorting according to age reverse order, -1 means reverse order, 1 means positive order

# View index
db.house.getIndexes()


#Create index db.house.createIndex({'name':1}) #Delete
index, delete according to the name field in the index information
db.house.dropIndex("name_1")

# Create a joint index, the following code means to create a positive index for name and a reverse index for id
db.house.createIndex({'name':1,'id': -1})

#插入1000条数据 
for(var i=1;i<1000;i++)db.user.insert({id:100+i,username:'name_'+i,age:10+i})

# View the execution plan
db.user.find({id:181}).explain()
#Query methods, common ones are COLLSCAN/full table scan, IXSCAN/index scan, FETCH/retrieve documents based on index, SHARD_MERGE/combined points Piece result, IDHACK/query for _id

Guess you like

Origin blog.csdn.net/qq_26896085/article/details/104926710