命令行
# mongo --help
MongoDB shell version v4.2.0
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.168.0.5/foo foo database on 192.168.0.5 machine
192.168.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
mongodb://192.168.0.5:9999/foo connection string URI can also be used
Options:
--ipv6 enable IPv6 support (disabled by
default)
--host arg server to connect to
--port arg port to connect to
-h [ --help ] show this usage information
--version show version information
--verbose increase verbosity
--shell run the shell after executing files
--nodb don't connect to mongod on startup - no
'db address' arg expected
--norc will not run the ".mongorc.js" file on
start up
--quiet be less chatty
--eval arg evaluate javascript
--disableJavaScriptJIT disable the Javascript Just In Time
compiler
--enableJavaScriptJIT enable the Javascript Just In Time
compiler
--disableJavaScriptProtection allow automatic JavaScript function
marshalling
--retryWrites automatically retry write operations
upon transient network errors
--disableImplicitSessions do not automatically create and use
implicit sessions
--jsHeapLimitMB arg set the js scope's heap size limit
Authentication Options:
-u [ --username ] arg username for authentication
-p [ --password ] arg password for authentication
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication
FLE AWS Options:
--awsAccessKeyId arg AWS Access Key for FLE Amazon KMS
--awsSecretAccessKey arg AWS Secret Key for FLE Amazon KMS
--awsSessionToken arg Optional AWS Session Token ID
--keyVaultNamespace arg database.collection to store encrypted
FLE parameters
--kmsURL arg Test parameter to override the URL for
KMS
TLS Options:
--tls use TLS for all connections
--tlsCertificateKeyFile arg PEM certificate/key file for TLS
--tlsCertificateKeyFilePassword arg Password for key in PEM file for TLS
--tlsCAFile arg Certificate Authority file for TLS
--tlsCRLFile arg Certificate Revocation List file for TLS
--tlsAllowInvalidHostnames Allow connections to servers with
non-matching hostnames
--tlsAllowInvalidCertificates Allow connections to servers with
invalid certificates
--tlsFIPSMode Activate FIPS 140-2 mode at startup
--tlsDisabledProtocols arg Comma separated list of TLS protocols to
disable [TLS1_0,TLS1_1,TLS1_2]
file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
插入
db.collection.insert({a:"a"})
db.collection.insertOne([{a:"a", b:"b"}, {c:"c", d:"d"}, {e:"e", f:"f", g: "g"} ])
db.collection.insertMany()
查询
To select all documents in the collection, pass an empty document as the query filter document to thedb.collection.find()
db.inventory.find()
db.inventory.find( {} )
-- Specify Equality Condition
db.inventory.find( { status: "D" } )
-- Specify Conditions Using Query Operators
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
-- Specify AND Conditions
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
-- Specify OR Conditions
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
-- Specify AND as well as OR Conditions
db.inventory.find( { status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ] } )
更新
db.collection.insertMany([{1:"a"}, {2:"b"}, {3:"c"}])
Update a Single Document
db.collection.update({1:"a"}, {1:"x"}) # 前边是条件,后边是更新的内容
db.inventory.updateOne( { item: "paper" }, { $set: { "size.uom": "cm", status: "P" }, $currentDate: { lastModified: true } } ) # uses the $currentDate operator to update the value of the lastModified field to the current date. IflastModified field does not exist, $currentDate will create the field. See $currentDate for details.
db.collection.updateMany()
db.collection.findAndModify()
db.collection.findOneAndUpdate()
db.collection.findOneAndReplace()
-- Upsert Option
If updateOne(), updateMany(), or replaceOne() includes upsert : true and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.
db.collection.save()
db.collection.bulkWrite()
删除
Delete All Documents
db.collection.deleteMany({})
Delete only one Document that Matches a Condition
db.collection.deleteOne()