欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MongoDB 導(dǎo)出導(dǎo)入備份恢復(fù)數(shù)據(jù)詳解及實(shí)例

 更新時間:2016年10月21日 09:46:25   投稿:lqh  
這篇文章主要介紹了MongoDB 導(dǎo)出導(dǎo)入備份恢復(fù)數(shù)據(jù)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下

數(shù)據(jù)庫備份和數(shù)據(jù)恢復(fù)的重要性,我想大家都知道,這里就舉例說明如何操作數(shù)據(jù)備份,數(shù)據(jù)恢復(fù)的實(shí)例:

創(chuàng)建測試數(shù)據(jù)

創(chuàng)建db:testdb,collection:user,插入10條記錄

mongo
MongoDB shell version: 3.0.2
connecting to: test
> use testdb
switched to db testdb
> db.user.insert({id:1,name:"用戶1"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:2,name:"用戶2"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:3,name:"用戶3"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:4,name:"用戶4"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:5,name:"用戶5"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:6,name:"用戶6"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:7,name:"用戶7"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:8,name:"用戶8"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:9,name:"用戶9"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:10,name:"用戶10"});
WriteResult({ "nInserted" : 1 })
> 
> db.user.find();
{ "_id" : ObjectId("574d7aae8780832e6c4e27b4"), "id" : 1, "name" : "用戶1" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b5"), "id" : 2, "name" : "用戶2" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b6"), "id" : 3, "name" : "用戶3" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b7"), "id" : 4, "name" : "用戶4" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b8"), "id" : 5, "name" : "用戶5" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b9"), "id" : 6, "name" : "用戶6" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27ba"), "id" : 7, "name" : "用戶7" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27bb"), "id" : 8, "name" : "用戶8" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27bc"), "id" : 9, "name" : "用戶9" }
{ "_id" : ObjectId("574d7ab08780832e6c4e27bd"), "id" : 10, "name" : "用戶10" }

數(shù)據(jù)導(dǎo)出 mongoexport

參數(shù)說明:

-d     數(shù)據(jù)庫名
-c     collection名
-o     輸出的文件名
--type 輸出的格式,默認(rèn)為json
-f     輸出的字段,如果--type為csv,則需要加上 -f "字段名"

更多參數(shù)說明可參考 mongoexport –help

例子:導(dǎo)出user所有記錄到/tmp/user.json

mongoexport -d testdb -c user -o /tmp/user.json
2016-05-31T20:00:32.257+0800  connected to: localhost
2016-05-31T20:00:32.286+0800  exported 10 records

cat /tmp/user.json
{"_id":{"$oid":"574d7aae8780832e6c4e27b4"},"id":1,"name":"用戶1"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b5"},"id":2,"name":"用戶2"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b6"},"id":3,"name":"用戶3"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b7"},"id":4,"name":"用戶4"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b8"},"id":5,"name":"用戶5"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b9"},"id":6,"name":"用戶6"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27ba"},"id":7,"name":"用戶7"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27bb"},"id":8,"name":"用戶8"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27bc"},"id":9,"name":"用戶9"}
{"_id":{"$oid":"574d7ab08780832e6c4e27bd"},"id":10,"name":"用戶10"}

例子:導(dǎo)出user所有id到/tmp/user.csv

格式為csv但不指定字段會出錯

mongoexport -d testdb -c user --type csv -o /tmp/user.csv
2016-05-31T20:01:05.393+0800  Failed: CSV mode requires a field list

mongoexport -d testdb -c user --type csv -f "id" -o /tmp/user.csv
2016-05-31T20:01:46.510+0800  connected to: localhost
2016-05-31T20:01:46.534+0800  exported 10 records

cat /tmp/user.csv
id
1
2
3
4
5
6
7
8
9
10

數(shù)據(jù)導(dǎo)入 mongoimport

參數(shù)說明:

-d           數(shù)據(jù)庫名
-c           collection名
--type       導(dǎo)入的格式,默認(rèn)json
-f           導(dǎo)入的字段名
--headerline 如果導(dǎo)入的格式是csv,則可以使用第一行的標(biāo)題作為導(dǎo)入的字段
--file       要導(dǎo)入的文件

更多參數(shù)說明可參考 mongoimport –help

導(dǎo)入前先清空collection user

> db.user.drop();
true
> db.user.find();
> 

例子:把上例導(dǎo)出的user.json導(dǎo)入

mongoimport -d testdb -c user --file /tmp/user.json
2016-05-31T20:10:22.240+0800  connected to: localhost
2016-05-31T20:10:22.287+0800  imported 10 documents

例子:把上例導(dǎo)出的user.csv導(dǎo)入

mongoimport -d testdb -c user --type csv --headerline --file /tmp/user.csv
2016-05-31T20:11:28.975+0800  connected to: localhost
2016-05-31T20:11:29.003+0800  imported 10 documents

數(shù)據(jù)備份 mongodump

參數(shù)說明:

-d 數(shù)據(jù)庫名
-c collection名
-o 備份的文件路徑

更多參數(shù)說明可參考 mongodump –help

例子:把testdb的user備份到/tmp

mongodump -d testdb -c user -o /tmp
2016-05-31T20:18:25.813+0800  writing testdb.user to /tmp/testdb/user.bson
2016-05-31T20:18:25.818+0800  writing testdb.user metadata to /tmp/testdb/user.metadata.json
2016-05-31T20:18:25.849+0800  done dumping testdb.user

數(shù)據(jù)恢復(fù) mongorestore

參數(shù)說明:

-d 數(shù)據(jù)庫名
-c collection名

更多參數(shù)說明可參考 mongorestore –help

導(dǎo)入前先清空collection user

> db.user.drop();
true
> db.user.find();
>

例子:把上例備份的數(shù)據(jù)恢復(fù)

mongorestore -d testdb -c user /tmp/testdb/user.bson 
2016-05-31T20:21:23.050+0800  checking for collection data in /tmp/testdb/user.bson
2016-05-31T20:21:23.084+0800  reading metadata file from /tmp/testdb/user.metadata.json
2016-05-31T20:21:23.088+0800  restoring testdb.user from file /tmp/testdb/user.bson
2016-05-31T20:21:23.153+0800  restoring indexes for collection testdb.user from metadata
2016-05-31T20:21:23.156+0800  finished restoring testdb.user
2016-05-31T20:21:23.156+0800  done

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • MongoDB執(zhí)行mongoexport時的異常及分析(數(shù)字類型的查詢)

    MongoDB執(zhí)行mongoexport時的異常及分析(數(shù)字類型的查詢)

    這篇文章主要給大家介紹了關(guān)于MongoDB執(zhí)行mongoexport時的異常及分析(數(shù)字類型的查詢)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • 優(yōu)化MongoDB配置來提高CPU使用率

    優(yōu)化MongoDB配置來提高CPU使用率

    MongoDB作為NoSQL數(shù)據(jù)庫的一種,已經(jīng)成為新一代數(shù)據(jù)存儲系統(tǒng)的趨勢,然而,MongoDB數(shù)據(jù)服務(wù)的性能問題一直是亟待解決的關(guān)鍵問題之一,在許多基于MongoDB的利用中,CPU利用率是一個重要的指標(biāo),它可以幫助我們對服務(wù)器的性能進(jìn)行實(shí)時監(jiān)控,以便及時發(fā)現(xiàn)和修復(fù)效力瓶頸
    2024-01-01
  • MongoDB憑什么躋身數(shù)據(jù)庫排行前五

    MongoDB憑什么躋身數(shù)據(jù)庫排行前五

    MongoDB以比去年同期超出65.96分的成績繼續(xù)雄踞榜單前五,這個增幅在全榜僅次于PostgreSQL的77.99,而其相對于4月份的6.10分的增長也是僅次于微軟SQL Server排名全榜第二
    2019-06-06
  • centos8安裝MongoDB的詳細(xì)過程

    centos8安裝MongoDB的詳細(xì)過程

    MongoDB由MongoDB Inc開發(fā),并根據(jù)SSPL(服務(wù)器端公共許可證)進(jìn)行分發(fā),是一個開源,跨平臺,面向文檔的數(shù)據(jù)庫管理系統(tǒng),本文重點(diǎn)給大家介紹centos8安裝MongoDB的詳細(xì)過程,感興趣的朋友一起看看吧
    2021-10-10
  • MongoDB學(xué)習(xí)之Text Search文本搜索功能

    MongoDB學(xué)習(xí)之Text Search文本搜索功能

    這篇文章主要給大家介紹了MongoDB之Text Search文本搜索功能的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • MongoDB集合中的文檔管理

    MongoDB集合中的文檔管理

    這篇文章介紹了MongoDB集合中文檔的管理方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • mongodb?linux下集群搭建過程

    mongodb?linux下集群搭建過程

    這篇文章主要介紹了mongodb?linux下集群搭建過程,本例中,為每個集群(shard?config)三個mongo實(shí)例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • springboot整合mongodb?changestream的示例代碼

    springboot整合mongodb?changestream的示例代碼

    Chang?Stream(變更記錄流)?是指collection(數(shù)據(jù)庫集合)的變更事件流,應(yīng)用程序通過db.collection.watch()這樣的命令可以獲得被監(jiān)聽對象的實(shí)時變更,本文給大家介紹springboot整合mongodb?changestream的示例代碼,感興趣的朋友一起看看吧
    2022-02-02
  • java操作mongoDB查詢的實(shí)例詳解

    java操作mongoDB查詢的實(shí)例詳解

    這篇文章主要介紹了java操作mongo查詢的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • springboot整合mongodb

    springboot整合mongodb

    這篇文章主要介紹了springboot如何整合mongodb,mongodb的安裝和使用,感興趣的同學(xué)可以參考閱讀本文
    2023-03-03

最新評論