MongoDB common command summary

  • 2020-05-06 11:57:32
  • OfStack

Common MongoDB commands:

superuser related:

use admin
Add or change the user password
db.addUser(ixigua,'pwd')
View the list of users
db.system.users.find()
User authentication
db.auth(ixigua,'pwd')
Delete user
db.removeUser('mongodb')
View all users
show users
View all databases
show dbs
View all collection
show collections
Check the status of each collection db.printCollectionStats()
View master-slave copy status
db.printReplicationInfo()
Fix database
db.repairDatabase()
Set the record profiling, 0=off 1=slow 2=all
db.setProfilingLevel(1)
# view profiling
show profile
Copy the database
db.copyDatabase('mail_addr','mail_addr_tmp')
Delete collection
db.mail_addr.drop()
Delete the current database
db.dropDatabase()


client connection:
/usr/local/mongodb/bin/mongo 8.8.88/ixigualib -u ixigua -p 'pwd'


:
# stores nested objects
db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})
Store the array object
db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})
Modify according to the query condition, insert if none exists, allowing multiple records to be modified
db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
Delete the record yy=5
db.foo.remove({'yy':5})
Delete all records
db.foo.remove()


index:
# add index: 1(ascending),-1(descending)
db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
# index child object
db.user_addr.ensureIndex({'Al.Em': 1})
View index information
db.deliver_status.getIndexes()
db.deliver_status.getIndexKeys()
Drop index
by index name db.user_addr.dropIndex('Al.Em_1')


query:
# find all
db.foo.find()
Find a record
db.foo.findOne()
Retrieve 10 records
by condition db.foo.find({'msg':'Hello 1'}).limit(10)
# sort sorting
db.deliver_status.find({'From':'ixigua@sina.com'}).sort({'Dt',-1})
db.deliver_status.find().sort({'Ct':-1}).limit(1)
# count
operation db.user_addr.count()
# distinct
operation db.foo.distinct('msg')
# > Operating
db.foo.find({"timestamp": {"$gte" : 2}})
# child object lookup
db.foo.find({'address.city':'beijing'})


management:
View the size of collection data
db.deliver_status.dataSize()
View colleciont status
db.deliver_status.stats()
Query the size of all indexes
db.deliver_status.totalIndexSize()
View the database
currently in use db

The author mahout

:

Library operation
show dbs   view the list of databases in the system
Note: there must be something in the database to display
db = db.getSiblingDB(" < Database name > ")
or
use < Database name >  
Switch the current database
Note: if the database does not exist, create database
Either way, you can set the db value to the specified database, and then you can use db to manage the new current database.
 
Es324en.dropDatabase ()   delete the current database
Note: the deleted database is recreated by deleting the current database and then creating a collection using the handle without changing the current database.
 
db.copyDatabase(" < Current database name > ", " < Backup library name > ", [hostname])
Copy the database, creating an identical database
except for the name Note: the optional hostname parameter specifies the current library MongDB server hostname.
 
db   displays the current database

Collection operation
View the list of collections contained in the current database  
db.createCollection(" < A collection of > ", { < options > }) create the collection
Note: parameter 2 optional object
Property   indicates
capped     Boolean, if true, means that the collection is a capped collection and that it does not grow to a maximum size specified by the size attribute. Default: false
autoIndexID   Boolean, if true, indicates that a _id field is automatically created and indexed on each document added to the collection. This pair of capped sets should be false. The default true
size   byte unit size for capping collections. The oldest files are deleted to make room for the new
file max   maximum number of documents allowed in the capped collection. The oldest files are deleted to make room for the new

file

db. < A collection of > .drop()
or
coll = db.getCollection(" < A collection of > ")
Es423en.drop ()   delete the collection
 
db. < A collection of > .find()
or
coll = db.getCollection(" < A collection of > ")
Es446en.find (query)   view the document
in the collection Note: the optional query parameter specifies that the query document containing the fields and values matches the document in the collection and returns the match. Example: coll. find ({speed: "120 mph"})
 
db. < A collection of > .insert({})  
or
db. < A collection of > .save({})
Insert document data into the collection (it is created if the collection does not exist, as is save)
Treat like data as a collection
The data in the collection is specifically similar to the property
A collection is a container inside which N multi-data
can be placed This data can be filtered and sorted by
db. < A collection of > .remove({filter conditions}, false)
Remove document parameter 1: filter from the collection. Parameter 2: optional parameter, whether to delete a single line, default false.
 
db. < A collection of > .save({ "_id" : ObjectId("57e26b294a655f35e13d6f5d"), "name" : "hung", "age" : 18})
or
db. < A collection of > .update({filter conditions},   {$set:{"name":"jin","up":true}},
{upsert: true multi: true})  
update in the collection document Parameter 1: condition
for updating the document Parameter two: the update operator used when updating, and the update property
Common operator: $inc recurses the value of this field. $set sets the field value. $push pushes an entry into the array. $rename rename the field. Such as ・ ・
Parameter three: optional, two properties. multi (all document updates that match to, default false only updates the first) and upsert (if no match is found, create one, default false does not), Boolean.
*******************************************************************
db. < A collection of > .pretty ()   displays the query document
neatly db. < A collection of > .find ().sort ({attribute :1/-1})   sorts
by the specified attribute 1 is the positive order, -1: the reverse order
db. < A collection of > .find ().limit (n)   shows the previous n data
db. < A collection of > find().skip(n)   skips the previous n data
Conditional lookup
db. < A collection of > .find ({property :value})   property == value
db. < A collection of > .find ({property :{operator :value}})
Operator:
$lt         is less than
$lte     is less than or equal to
$gt       is greater than
$gte   is greater than or equal to
$ne     does not equal
Use two operators
for the same attribute db. < A collection of > .find({property: {operator 1:value, operator 2:value}})
Such as: db < A collection of > .find({ age: { $gt: 6, $lt: 16 } })
Qualification for different attributes (both conditions are true)
db. < A collection of > .find ({attribute 1: {operator :value}, attribute 2: {operator :value}})
Or conditions
db. < A collection of > .find({
$or: [
{property: value},
{property: value},
{property: value}
]
})
Use the function to filter the data
db. < A collection of > .find({
$where: function(){
return true/false
}
})


Related articles: