MongoDB日常运维操作命令集锦(6)

说明:将pagedb数据库中page集合的数据导出到pages.csv文件,其中各选项含义:
-f 指定cvs列名为_id,title,url,spiderName,pubDate
-q 指定查询条件
其他各个选项的使用,可以查看帮助:

[root@centos6-vm01 ~]# mongoexport --help
Usage:
mongoexport <options>

Export data from MongoDB in CSV or JSON format.

See for more information.

general options:
--help print usage
--version print the tool version and exit

verbosity options:
-v, --verbose more detailed log output (include multiple times for more verbosity, e.g. -vvvvv)
--quiet hide all log output

connection options:
-h, --host= mongodb host to connect to (setname/host1,host2 for replica sets)
--port= server port (can also use --host hostname:port)

authentication options:
-u, --username= username for authentication
-p, --password= password for authentication
--authenticationDatabase= database that holds the user's credentials
--authenticationMechanism= authentication mechanism to use

namespace options:
-d, --db= database to use
-c, --collection= collection to use

output options:
-f, --fields= comma separated list of field names (required for exporting CSV) e.g. -f "name,age"
--fieldFile= file with field names - 1 per line
--type= the output format, either json or csv (defaults to 'json')
-o, --out= output file; if not specified, stdout is used
--jsonArray output to a JSON array rather than one object per line
--pretty output JSON formatted to be human-readable

querying options:
-q, --query= query filter, as a JSON string, e.g., '{x:{$gt:1}}'
-k, --slaveOk allow secondary reads if available (default true)
--forceTableScan force a table scan (do not use $snapshot)
--skip= number of documents to skip
--limit= limit the number of documents to export
--sort= sort order, as a JSON string, e.g. '{x:1}'
注意:如果上面的选项-q指定一个查询条件,需要使用单引号括起来,如下所示:

[root@centos6-vm01 ~]# mongoexport -d page -c Article -q '{"spiderName": "mafengwoSpider"}' -f _id,title,content,images,publishDate,spiderName,url --jsonArray > mafengwoArticle.txt
2018-01-03T08:12:41.234+0800 connected to: localhost
2018-01-03T08:12:41.234+0800 exported 0 records

[root@centos6-vm01 ~]# ll mafengwoArticle.txt
-rw-r--r--. 1 root root 3 Jan 3 00:12 mafengwoArticle.txt
否则,就会出现下面的错误:

1
ERROR: too many positional options
六、远程连接管理

1)基于mongo实现远程连接
[root@centos6-vm01 ~]# mongo 192.168.10.220:27017/pagedb
或者
[root@centos6-vm01 ~]# mongo 192.168.10.220:27017/pagedb -ukevin -p123456kevin

通过mongo实现连接,可以非常灵活的选择参数选项,参看命令帮助,如下所示:

[root@centos6-vm01 ~]# mongo --help
MongoDB shell version: 3.0.6
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
Options:
--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
--port arg port to connect to
--host arg server to connect to
--eval arg evaluate javascript
-h [ --help ] show this usage information
--version show version information
--verbose increase verbosity
--ipv6 enable IPv6 support (disabled by default)

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

file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
2)基于MongoDB支持的javascript实现远程连接
当你已经连接到一个远程的MongoDB数据库服务器(例如,通过mongo连接到192.168.0.184),现在想要在这个会话中连接另一个远程的数据库服务器(192.168.0.197),可以执行如下命令:

var x = new Mongo('192.168.10.220:27017')
var ydb = x.getDB('pagedb');
use ydb
switched to db ydb
db
ydb
ydb.page.findOne()
{
"_id" : ObjectId("4eded6a5bf3bfa0014000003"),
"content" : "MongoDB是一个NoSQL非数据库系统,即一个数据库可以包含多个集合(Collection),每个集合对应于关系数据库中的表...",
"pubdate" : "2018-08-20",
"title" : "MongoDB日常运维操作命令集锦",
"url" : "https://www.linuxidc.com/Linux/2018-08/153631.htm"
}
上述通过MongoDB提供的JavaScript脚本,实现对另一个远程数据库服务器进行连接,操作指定数据库pagedb的page集合。
如果启用了安全认证模式,可以在获取数据库连接实例时,指定认证账号,例如:

var x = new Mongo('192.168.0.197:27017')
var ydb = x.getDB('pagedb', 'shirdrn', '(jkfFS$343$_\=\,.F@3');
use ydb
switched to db ydb

更多MongoDB相关教程见以下内容

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/f4892ae8b524347eef5d3289f45192bc.html