MongoDB - mongo shell入门

命令行

# 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()

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
最新发布
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值