search
HomeDatabaseMysql Tutorial非关系型数据库(Nosql)之mongodb:创建集合,备份与导入导出,

1 固定集合 固定集合值得是事先创建而且大小固定的集合 2 固定集合的特征:固定集合很像环形队列,如果空间不足,最早文档就会被删除,为新的文档腾出空间。一般来说,固定集合适用于任何想要自动淘汰过期属性的场景,没有太多的操作限制。 3 创建固定集合使

固定集合

固定集合值得是事先创建而且大小固定的集合

固定集合的特征:固定集合很像环形队列,如果空间不足,最早文档就会被删除,为新的文档腾出空间。一般来说,固定集合适用于任何想要自动淘汰过期属性的场景,没有太多的操作限制。

创建固定集合使用命令:

db.createCollection(“collectionName”,{capped:true,size:100000,max:100});

size:指定集合大小,单位为KB,max指定文档的数量

当指定文档数量上限时,必须同时指定大小。淘汰机制只有在容量还没有满时才会依据文档数量来工作。要是容量满了,淘汰机制依据容量来工作。

创建一个集合:

>db.createCollection("cap1",{capped:true,size:1000,max:100});

{ "ok" : 1 }

\

插入数据

> for(var i=1;i

...db.cap1.insert({name:"dongxue",age:i});

... }

WriteResult({ "nInserted" : 1 })

> db.cap1.find().count();

53   (大小之所以是53是因为大小超过了1000

\

固定集合的应用场景:聊天记录,日志信息

      淘汰机制:当满足size指定集合大小,不能再继续往固定集合中加数据。

      固定集合的容量优先

当文档达到100条时,再添加的时候会替换先前的

 

备份与导入导出。

MongoDB提供了备份和回复的功能,分别是MongoDB下载目录下的mongodump.exemongorestore.exe文件。

 

备份数据使用下面的命令:

mongodump –h dbhost –d dbname –o dbdirectory

-h:MonDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017,当然该目录需要提前创建,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。 

mongodump -h localhost:27017 -d toto -of:/beifeng

-h:用来指定要输出的数据库所在的ip地址和端口号

-d:  指定要备份的数据库

-o:  表示要备份到的文件目录

执行后的效果图:

\

\

另外启动一个mongodb的客户端,目的是将数据库中toto数据库删掉

C:\Users\to-to>mongo

MongoDB shell version: 2.6.4

connecting to: test

> use toto;

switched to db toto

> db.help();

DB methods:

        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.r

unCommand(...) ]

        db.auth(username, password)

        db.cloneDatabase(fromhost)

        db.commandHelp(name) returns the help for the command

        db.copyDatabase(fromdb, todb, fromhost)

        db.createCollection(name, { size : ..., capped : ..., max : ... } )

        db.createUser(userDocument)

        db.currentOp() displays currently executing operations in the db

        db.dropDatabase()

        db.eval(func, args) run code server-side

        db.fsyncLock() flush data to disk and lock server for backups

        db.fsyncUnlock() unlocks server following a db.fsyncLock()

        db.getCollection(cname) same as db['cname'] or db.cname

        db.getCollectionNames()

        db.getLastError() - just returns the err msg string

        db.getLastErrorObj() - return full status object

        db.getMongo() get the server connection object

        db.getMongo().setSlaveOk() allow queries on a replication slave server

        db.getName()

        db.getPrevError()

        db.getProfilingLevel() - deprecated

        db.getProfilingStatus() - returns if profiling is on and slow threshold

        db.getReplicationInfo()

        db.getSiblingDB(name) get the db at the same server as this one

        db.getWriteConcern() - returns the write concern used for any operations on this db, inherit

ed from server object if set

        db.hostInfo() get details about the server's host

        db.isMaster() check replica primary status

        db.killOp(opid) kills the current operation in the db

        db.listCommands() lists all the db commands

        db.loadServerScripts() loads all the scripts in db.system.js

        db.logout()

        db.printCollectionStats()

        db.printReplicationInfo()

        db.printShardingStatus()

        db.printSlaveReplicationInfo()

        db.dropUser(username)

        db.repairDatabase()

        db.resetError()

        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj

 : 1 }

        db.serverStatus()

        db.setProfilingLevel(level,) 0=off 1=slow 2=all

        db.setWriteConcern( ) - sets the write concern for writes to the db

        db.unsetWriteConcern( ) - unsets the write concern for writes to the db

        db.setVerboseShell(flag) display extra information in shell output

        db.shutdownServer()

        db.stats()

        db.version() current version of the server

> db.dropDatabase();

{ "dropped" : "toto", "ok" : 1 }

\

\

数据还原

mongorestore -h localhost:27017 -d toto -directoryperdb F:/beifeng/toto

-h:MongoDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017

-d:需要备份的数据库实例,例如test

-o:备份的数据存放位置,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放数据库实例的备份数据。

C:\Users\to-to> mongorestore -h localhost:27017 -d toto -directoryperdb F:/beifeng/toto

connected to: localhost:27017

2014-10-15T23:19:11.071+0800 F:/beifeng/toto\c3.bson

2014-10-15T23:19:11.071+0800    going into namespace [toto.c3]

2014-10-15T23:19:14.009+0800            Progress: 5740200/54000000      10%     (bytes)

2014-10-15T23:19:17.010+0800            Progress: 10125000/54000000     18%     (bytes)

2014-10-15T23:19:20.010+0800            Progress: 15660000/54000000     29%     (bytes)

2014-10-15T23:19:23.011+0800            Progress: 22528800/54000000     41%     (bytes)

2014-10-15T23:19:26.013+0800            Progress: 29586600/54000000     54%     (bytes)

2014-10-15T23:19:29.013+0800            Progress: 36752400/54000000     68%     (bytes)

2014-10-15T23:19:32.000+0800            Progress: 43372800/54000000     80%     (bytes)

2014-10-15T23:19:35.001+0800            Progress: 50284800/54000000     93%     (bytes)

1000000 objects found

2014-10-15T23:19:36.579+0800    Creating index: { key: { _id: 1 }, name: "_id_", ns: "toto.c3" }

2014-10-15T23:19:36.641+0800    Creating index: { unique: true, key: { age: 1 }, name: "age_1", ns:

"toto.c3" }

2014-10-15T23:19:41.440+0800 F:/beifeng/toto\cap1.bson

2014-10-15T23:19:41.440+0800    going into namespace [toto.cap1]

2014-10-15T23:19:41.440+0800    Created collection toto.cap1 with options: { "create" : "cap1", "cap

ped" : true, "size" : 4096, "max" : 100 }

53 objects found

2014-10-15T23:19:41.440+0800    Creating index: { key: { _id: 1 }, name: "_id_", ns: "toto.cap1" }

2014-10-15T23:19:41.440+0800 F:/beifeng/toto\cap2.bson

2014-10-15T23:19:41.440+0800    going into namespace [toto.cap2]

2014-10-15T23:19:41.440+0800    Created collection toto.cap2 with options: { "create" : "cap2", "cap

ped" : true, "size" : 4096, "max" : 100 }

file F:/beifeng/toto\cap2.bson empty, skipping

2014-10-15T23:19:41.456+0800    Creating index: { key: { _id: 1 }, name: "_id_", ns: "toto.cap2" }

 

C:\Users\to-to>

\

 

 

导入导出:

用到的应用mongoexport,mongoimport

mongoexport –h dhost –d dbname –c collectionName –o output

参数说明:

-h 数据库地址

-d 指明使用的库

-c 指明要导出的集合

-o 指明要导出的文件名

dname:表示要导出的数据库

collectionName:表示导出哪个集合

output:表示导出到的位置。

C:\Users\to-to>mongoexport -h localhost:27017 -d toto -c c3 -o f:/beifen/c3.txt

connected to: localhost:27017

exported 1000000 records

 

C:\Users\to-to>

\

\

\

同样可以数据导出到doc

数据导入:

mongoimport -h localhost:27017 -d toto -c ccc f:/beifen/c3.txt

\

C:\Users\to-to>mongo

MongoDB shell version: 2.6.4

connecting to: test

> use toto

switched to db toto

> show tables;

c3

cap1

cap2

ccc

system.indexes

> db.ccc.find();

{ "_id" : ObjectId("543e7473256769913d467e75"), "name" : "zhangsan", "age" : 1 }

{ "_id" : ObjectId("543e7473256769913d467e76"), "name" : "zhangsan", "age" : 2 }

{ "_id" : ObjectId("543e7473256769913d467e77"), "name" : "zhangsan", "age" : 3 }

{ "_id" : ObjectId("543e7473256769913d467e78"), "name" : "zhangsan", "age" : 4 }

{ "_id" : ObjectId("543e7473256769913d467e79"), "name" : "zhangsan", "age" : 5 }

{ "_id" : ObjectId("543e7473256769913d467e7a"), "name" : "zhangsan", "age" : 6 }

{ "_id" : ObjectId("543e7473256769913d467e7b"), "name" : "zhangsan", "age" : 7 }

{ "_id" : ObjectId("543e7473256769913d467e7c"), "name" : "zhangsan", "age" : 8 }

{ "_id" : ObjectId("543e7473256769913d467e7d"), "name" : "zhangsan", "age" : 9 }

{ "_id" : ObjectId("543e7473256769913d467e7e"), "name" : "zhangsan", "age" : 10 }

{ "_id" : ObjectId("543e7473256769913d467e7f"), "name" : "zhangsan", "age" : 11 }

{ "_id" : ObjectId("543e7473256769913d467e80"), "name" : "zhangsan", "age" : 12 }

{ "_id" : ObjectId("543e7473256769913d467e81"), "name" : "zhangsan", "age" : 13 }

{ "_id" : ObjectId("543e7473256769913d467e82"), "name" : "zhangsan", "age" : 14 }

{ "_id" : ObjectId("543e7473256769913d467e83"), "name" : "zhangsan", "age" : 15 }

{ "_id" : ObjectId("543e7473256769913d467e84"), "name" : "zhangsan", "age" : 16 }

{ "_id" : ObjectId("543e7473256769913d467e85"), "name" : "zhangsan", "age" : 17 }

{ "_id" : ObjectId("543e7473256769913d467e86"), "name" : "zhangsan", "age" : 18 }

{ "_id" : ObjectId("543e7473256769913d467e87"), "name" : "zhangsan", "age" : 19 }

{ "_id" : ObjectId("543e7473256769913d467e88"), "name" : "zhangsan", "age" : 20 }

Type "it" for more

\

上面自动隐式创建了一个ccc集合。

 

9 mongodb安全认证

每个mongodb实例中的数据库都可以有许多用户,如果开启了安全性检查,只有数据库认证用户才能执行读或者写操作。在认证的上下文中,MongoDB会将普通的数据作为admin

数据库处理。Admin数据库中的用户被视为超级用户(即:管理员)

在认证之后,管理员可以读写所有数据库,执行特定的管理员命令,执行listDatabaseshutdown.

在开启安全检查之前,一定要至少一个管理员账号。

  1. 最少得保证有一个管理员账号(admin 数据库当中的用户都是管理员)

    use admin

    db.addUser(“username”,”password”);

2.有了管理员账号,就可以为其它的数据库分配用户。

    2.1 首先要跳转到被分配的数据库

3.需要重新启动mongodb服务,开启安全检查

4.接下来的客户端连接mongodb,需要登录才能执行相应的操作。

C:\Users\to-to>mongo localhost:27017/admin

MongoDB shell version: 2.6.4

connecting to: localhost:27017/admin

> db

admin


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
深入理解MySQL索引优化器工作原理深入理解MySQL索引优化器工作原理Nov 09, 2022 pm 02:05 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

sybase是什么数据库sybase是什么数据库Sep 22, 2021 am 11:39 AM

sybase是基于客户/服务器体系结构的数据库,是一个开放的、高性能的、可编程的数据库,可使用事件驱动的触发器、多线索化等来提高性能。

visual foxpro数据库文件是什么visual foxpro数据库文件是什么Jul 23, 2021 pm 04:53 PM

visual foxpro数据库文件是管理数据库对象的系统文件。在VFP中,用户数据是存放在“.DBF”表文件中;VFP的数据库文件(“.DBC”)中不存放用户数据,它只起将属于某一数据库的 数据库表与视图、连接、存储过程等关联起来的作用。

数据库系统的构成包括哪些数据库系统的构成包括哪些Jul 15, 2022 am 11:58 AM

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

microsoft sql server是什么软件microsoft sql server是什么软件Feb 28, 2023 pm 03:00 PM

microsoft sql server是Microsoft公司推出的关系型数据库管理系统,是一个全面的数据库平台,使用集成的商业智能(BI)工具提供了企业级的数据管理,具有使用方便可伸缩性好与相关软件集成程度高等优点。SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使用户可以构建和管理用于业务的高可用和高性能的数据应用程序。

access数据库的结构层次是什么access数据库的结构层次是什么Aug 26, 2022 pm 04:45 PM

结构层次是“数据库→数据表→记录→字段”;字段构成记录,记录构成数据表,数据表构成了数据库。数据库是一个完整的数据的记录的整体,一个数据库包含0到N个表,一个表包含0到N个字段,记录是表中的行。

go语言可以写数据库么go语言可以写数据库么Jan 06, 2023 am 10:35 AM

go语言可以写数据库。Go语言和其他语言不同的地方是,Go官方没有提供数据库驱动,而是编写了开发数据库驱动的标准接口,开发者可以根据定义的接口来开发相应的数据库驱动;这样做的好处在于,只要是按照标准接口开发的代码,以后迁移数据库时,不需要做任何修改,极大方便了后期的架构调整。

mysql查询慢的因素除了索引,还有什么?mysql查询慢的因素除了索引,还有什么?Jul 19, 2022 pm 08:22 PM

mysql查询为什么会慢,关于这个问题,在实际开发经常会遇到,而面试中,也是个高频题。遇到这种问题,我们一般也会想到是因为索引。那除开索引之外,还有哪些因素会导致数据库查询变慢呢?

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft