mongoDB 使用手冊

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

1、基本操作
db.AddUser(username,password) 添加用戶
db.auth(usrename,password) 設置數據庫連接驗證
db.cloneDataBase(fromhost) 從目標服務器克隆一個數據庫
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 復制數據庫fromdb---源數據庫名稱,todb---目標數據庫名稱,fromhost---源數據庫服務器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 創建一個數據集,相當于一個表
db.currentOp() 取消當前庫的當前操作
db.dropDataBase() 刪除當前數據庫
db.eval_r(func,args) run code server-side
db.getCollection(cname) 取得一個數據集合,同用法:db['cname'] or
db.getCollenctionNames() 取得所有數據集合的名稱列表
db.getLastError() 返回最后一個錯誤的提示消息
db.getLastErrorObj() 返回最后一個錯誤的對象
db.getMongo() 取得當前服務器的連接對象get the server
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回當操作數據庫的名稱
db.getPrevError() 返回上一個錯誤對象
db.getProfilingLevel()
db.getReplicationInfo() 獲得重復的數據
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(殺死)在當前庫的當前操作
db.printCollectionStats() 返回當前庫的數據集狀態
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回當前數據庫是否為共享數據庫
db.removeUser(username) 刪除用戶
db.repairDatabase() 修復當前數據庫
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 關閉當前服務程序
db.version() 返回當前程序的版本信息

?

2、數據集(表)操作
db.test.find({id:10}) 返回test數據集ID=10的數據集
db.test.find({id:10}).count() 返回test數據集ID=10的數據總數
db.test.find({id:10}).limit(2) 返回test數據集ID=10的數據集從第二條開始的數據集
db.test.find({id:10}).skip(8) 返回test數據集ID=10的數據集從0到第八條的數據集
db.test.find({id:10}).limit(2).skip(8) 返回test數據集ID=1=的數據集從第二條到第八條的數據
db.test.find({id:10}).sort() 返回test數據集ID=10的排序數據集
db.test.findOne([query]) 返回符合條件的一條數據
db.test.getDB() 返回此數據集所屬的數據庫名稱
db.test.getIndexes() 返回些數據集的索引信息
db.test.group({key:...,initial:...,reduce:...[,cond:...]})
db.test.mapReduce(mayFunction,reduceFunction,<optional params>)
db.test.remove(query) 在數據集中刪除一條數據
db.test.renameCollection(newName) 重命名些數據集名稱
db.test.save(obj) 往數據集中插入一條數據
db.test.stats() 返回此數據集的狀態
db.test.storageSize() 返回此數據集的存儲大小
db.test.totalIndexSize() 返回此數據集的索引文件大小
db.test.totalSize() 返回些數據集的總大小
db.test.update(query,object[,upsert_bool]) 在此數據集中更新一條數據
db.test.validate() 驗證此數據集
db.test.getShardVersion() 返回數據集共享版本號

?

3、MongoDB語法與現有關系型數據庫SQL語法比較
MongoDB語法 MySql語法
db.test.find({'name':'foobar'}) <==> select * from test where name='foobar'
db.test.find() <==> select * from test
db.test.find({'ID':10}).count() <==> select count(*) from test where ID=10
db.test.find().skip(10).limit(20) <==> select * from test limit 10,20
db.test.find({'ID':{$in:[25,35,45]}}) <==> select * from test where ID in (25,35,45)
db.test.find().sort({'ID':-1}) <==> select * from test order by ID desc
db.test.distinct('name',{'ID':{$lt:20}}) <==> select distinct(name) from test where ID<20
db.test.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}}) <==> select name,sum(marks) from test group by name
db.test.find('this.ID<20',{name:1}) <==> select name from test where ID<20
db.test.insert({'name':'foobar','age':25})<==>insert into test ('name','age') values('foobar',25)
db.test.remove({}) <==> delete * from test
db.test.remove({'age':20}) <==> delete test where age=20
db.test.remove({'age':{$lt:20}}) <==> elete test where age<20
db.test.remove({'age':{$lte:20}}) <==> delete test where age<=20
db.test.remove({'age':{$gt:20}}) <==> delete test where age>20
db.test.remove({'age':{$gte:20}}) <==> delete test where age>=20
db.test.remove({'age':{$ne:20}}) <==> delete test where age!=20
db.test.update({'name':'foobar'},{$set:{'age':36}}) <==> update test set age=36 where name='foobar'
db.test.update({'name':'foobar'},{$inc:{'age':3}}) <==> update test set age=age+3 where
name='foobar'

?

4、MongoDB主從復制介紹
MongoDB的主從復制其實很簡單,就是在運行 主的服務器 上開啟mongod進程 時,加入參數--master即可,在運行從的服務 器上開啟mongod進程時,加入--slave 和 --source 指定主即可,這樣,在主數據 庫更新時,數據被復制到從數據庫 中

(這里日志 文件 和訪問 數據時授權用戶暫時不考慮 )
下面我在單臺服務器上開啟2 deamon來模擬2臺服務器進行主從復制:
$ mkdir m_master m_slave
$mongodb/bin/mongod??--port??28018 --dbpath ~/m_master??--master??&
$mongodb/bin/mongod??--port??28019 --dbpath ~/m_slave??--slave??--source???localhost:28018??&
這樣主從服務器都已經啟動了,可以利用 netstat -an -t 查看28018、28019端口 是否開放

登錄主服務器:
$ mongodb/bin/mongo --port 28018
MongoDB shell version: 1.2.4-
url: test
connecting to: 127.0.0.1:28018/test
type "help" for help
> show dbs
admin
local
test
> use test
switched to db test
> show collections
這里主上的test數據什么表都沒有,為空,查看從服 務器同樣也是這樣
$ mongodb/bin/mongo --port 28019
MongoDB shell version: 1.2.4-
url: test
connecting to: 127.0.0.1:28019/test
type "help" for help
> show dbs
admin
local
test
> use test
switched to db test
> show collections
那么現在我們來驗證主從數據是否會像想象的那樣同步 呢?


我們在主上新建表user
> db??
test
>db.createCollection("user");
> show collections???????????
system.indexes
user
>
表 user已經存在了,而且test庫中還多了一個system.indexes用來存放索引的表

到從服務器上查看test庫:
> db??
test
> show collections???????????
system.indexes
User
> db.user.find();
>
從 服務器的test庫中user表已經存在,同時我還查了一下user表為空
現在我們再來測試一下,向主服務器test庫的user表中插入一條數據
> show collections???????????
system.indexes
user
> db.user.insert({uid:1,name:"Falcon.C",age:25});
> db.user.find();???????????????????????????????
{ "_id" : ObjectId("4b8226a997521a578b7aea38"), "uid" : 1, "name" : "Falcon.C", "age" : 25 }
>
這 時我們查看從服務器的test庫user表時會多出一條記錄來:
> db.user.find();
{ "_id" : ObjectId("4b8226a997521a578b7aea38"), "uid" : 1, "name" : "Falcon.C", "age" : 25 }
>
MongoDB 還有 Replica Pairs 和 Master - Master
參考地址:
http://www.mongodb.org/display/DOCS/Master+Slave


MongoDB一般情況下都可以支持主主復制,但是在大部分情況下官方不推薦使用
運行 的master - master的準備工作是:
新建存放數據 庫文件 的路徑
$mkdir mongodata/mm_28050 mongodata/mm_28051
運行mongodb數據庫 ,一個端口 為:28050,一個為:28051
$ mongodb/bin/mongod --port 28050 --dbpath ~/mongodata/mm_28050 --master --slave --source localhost:28051 > /dev/null &
$ mongodb/bin/mongod --port 28051 --dbpath ~mongodata/mm_28051 --master --slave --source localhost:28050 > /dev/null &
可以通過ps -ef|grep mongod 或 netstat -an -t來檢查是否運行功能


測試master - master模式 :
$ mongodb/bin/mongo --port 28050
MongoDB shell version: 1.2.4-
url: test
connecting to: 127.0.0.1:28050/test
type "help" for help
> show dbs
admin
local
> db
test
> db.user.insert({_id:1,username:"Falcon.C",age:25,sex:"M"});
> db.user.find();
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
> db.user.find();??//在28051端口插入數據后,再來查詢,看數據是否同步
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
{ "_id" : 2, "username" : "NetOne", "age" : 24, "sex" : "F" }
>
$ mongodb/bin/mongo --port 28051
MongoDB shell version: 1.2.4-
url: test
connecting to: 127.0.0.1:28051/test
type "help" for help
> db
test
> show collections?????????端口28050已經新建了一個user表并插入了一條數據,這里多出2表
system.indexes
user
> db.user.find();????????//查詢表user發現數據已經同步
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
> db.user.insert({_id:2,username:"NetOne",age:24,sex:"F"});在此插入數據看數據是否雙向同步
> db.user.find();??
{ "_id" : 1, "username" : "Falcon.C", "age" : 25, "sex" : "M" }
{ "_id" : 2, "username" : "NetOne", "age" : 24, "sex" : "F" }
>
通 過以上開啟兩終端分別連接到28050、28051端口,分別插入測試數據發現,一切正常,正如我們所想的那樣實現數據的雙向同步

轉載于:https://my.oschina.net/usenrong/blog/733258

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542718.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542718.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542718.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

android搜索框功能實現_巧用 Trie 樹,實現搜索引擎關鍵詞提示功能

來源 | 碼海責編 | Carol封圖 | CSDN 付費下載于視覺中國我們幾乎每天都在用搜索引擎搜索信息&#xff0c;相信大家肯定有注意過這樣一個細節:當輸入某個字符的時候&#xff0c;搜索引框底下會出現多個推薦詞&#xff0c;如下&#xff0c;輸入「python」后&#xff0c;底下會出…

Python | 從用戶輸入數據,保存到文件,讀取并打印

Here, we have to input the data from the user, to read the data from user, we use input() method, and then the input data we have to store in the file by using write() method and then by using read() method we can get the data. 在這里&#xff0c;我們必須從…

python語句print type 1234的輸出結果是_Python語句 print(type(1J))的輸出結果是

【填空題】遍歷輸出文件所有行。 fopen("d:\\r2.txt","r") while True: str print(str,end) if not str: break f.close()【單選題】執行下列 Python語句將產生的結果是( ) i1 if (i): print(True) else: print( False)【單選題】Python語句 print(type(1/…

qt5.9.0調試如何查看變量的值_深入了解 Java 調試

Bug(俗稱"八阿哥") 是軟件開發繞不過的一道坎&#xff0c;因此調試便成了每位程序員一項必備的核心技能。調試不僅有助于理解程序的運行流程&#xff0c;還能改進代碼質量&#xff0c;最終提高開發者解決問題的能力以及交付軟件的品質。本文旨在討論 Java 調試關鍵技…

python字符串轉浮點數_Python | 打印不同的值(整數,浮點數,字符串,布爾值)...

python字符串轉浮點數In the given example, we are printing different values like integer, float, string and Boolean using print() method in python. 在給定的示例中&#xff0c;我們使用python中的print()方法打印不同的值&#xff0c;例如整數&#xff0c;浮點數&…

(6) 如何用Apache POI操作Excel文件-----POI-3.10的一個和注解(comment)相關的另外一個bug...

如果POI-3.10往一個工作表&#xff08;sheet&#xff09;里面插入數據的話&#xff0c;需要注意了&#xff0c;其有一個不太被容易發現的bug。 被插入的工作表&#xff08;sheet&#xff09;里面的單元格沒有包含任何的注解&#xff08;comment&#xff09;的時候&#xff0c;插…

mysql下拉刷新加載數據_下拉刷新、加載數據功能

paging nick加載更多getData();varm0,n2;//m:button點擊次數 n:一次加載幾條數據$(.page-btn-nick).click(getData);functiongetData(){$.ajax(paging.html).then(function(response){//測試url寫本頁面varobj{developer:[{name:nick},{name:ljy},{name:xzl},{name:jeson},{nam…

mcq 隊列_人工智能邏輯才能問答(MCQ)

mcq 隊列1) Why do we want to implement the concept of Logic in an AI system? So that the agent can have decision making capabilitySo that the agent can think and act humanlySo that the agent can apply the logic for finding the solution to any particular p…

第三周作業!

1、列出當前系統上所有已經登錄的用戶的用戶名&#xff0c;注意&#xff1a;同一個用戶登錄多次&#xff0c;則只顯示一次即可。答&#xff1a;本題思路&#xff1a;先用who命令列出當前登陸的用戶信息&#xff0c;然后使用cut命令對字段進行分割&#xff0c;選出我們需要的字段…

python導入模塊以及類_python模塊的導入以及模塊簡介

標簽&#xff1a; 一、模塊的定義及類型 1、定義 模塊就是用一堆的代碼實現了一些功能的代碼的集合&#xff0c;通常一個或者多個函數寫在一個.py文件里&#xff0c;而如果有些功能實現起來很復雜&#xff0c;那么就需要創建n個.py文件&#xff0c;這n個.py文件的集合就是模塊 …

mysql 指定數字排序_Mysql數據排序

排序數據普通字段排序按照單一字段排序按照多個字段排序手動指定排序順序單個字段手動排序多個字段手動排序普通字段排序按照單一字段排序排序采用order by子句&#xff0c;order by后面跟上排序字段&#xff0c;排序字段可以放多個&#xff0c;多個采用逗號間隔&#xff0c;or…

《黃帝內經 —— 央視60集紀錄片》

下載地址&#xff1a; http://pan.baidu.com/s/1dFI8hxf 目錄 第一部 醫史篇第1集&#xff1a;神奇的秘笈&#xff08;《黃帝內經》是部什么書&#xff09;第2集&#xff1a;赫赫始祖&#xff08;上&#xff09;&#xff08;黃帝、炎帝&#xff09;第3集&#xff1a;赫赫始祖&a…

mnist手寫數字數據集_mnist手寫數據集(1. 加載與可視化)

》》歡迎 點贊&#xff0c;留言&#xff0c;收藏加關注《《1. 模型構建的步驟&#xff1a;在構建AI模型時&#xff0c;一般有以下主要步驟&#xff1a;準備數據、數據預處理、劃分數據集、配置模型、訓練模型、評估優化、模型應用&#xff0c;如下圖所示&#xff1a;【注意】由…

python凱撒密碼實現_密碼:凱撒密碼及其Python實現

python凱撒密碼實現Before we start let’s some basic terminology... 在開始之前&#xff0c;讓我們先介紹一些基本術語... The art and science to achieve security by encoding messages to make them unreadable are known as Cryptography. That’s what the whole art…

qtextedit 默認文案_QT-純代碼控件-QSplitter(分裂器)

版權聲明&#xff1a;本文為博主原創文章&#xff0c;遵循CC 4.0 by-sa版權協議&#xff0c;轉載請附上原文出處鏈接和本聲明。本文鏈接&#xff1a;https://blog.csdn.net/qq_41488943/article/details/96431379使用Qplitter實現頁面的三布局分布1.新建一個無ui界面的工程&…

TYVJ P1030 乳草的入侵 Label:跳馬問題

背景 USACO OCT09 6TH描述 Farmer John一直努力讓他的草地充滿鮮美多汁的而又健康的牧草。可惜天不從人愿&#xff0c;他在植物大戰人類中敗下陣來。邪惡的乳草已經在他的農場的西北部份佔領了一片立足之地。草地像往常一樣&#xff0c;被分割成一個高度為Y(1 < y < 100)…

kotlin中既繼承又實現_Kotlin程序| 解決繼承中的主要沖突的示例

kotlin中既繼承又實現繼承中的主要沖突 (Overriding Conflicts in Inheritance) It may appear, we inherit more than one implementation of the same method. 看來&#xff0c;我們繼承了同一方法的多個實現。 Need to implement all the methods which we have inherited f…

python雷達圖詳解_Python簡單雷達圖繪制

import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams[font.family] SimHei matplotlib.rcParams[font.sans-serif] [SimHei] lables np.array([綜合,KDA,發育,推進,生存,輸出]) nAttr 6 date np.array([7, 5, 6, 9, 8, 7]) angles…

瀏覽器兼容問題 透明度 position:fixed bootstrap

瀏覽器兼容問題&#xff1a;主要是ie8以下&#xff1a; 用bootstrap框架結合jq寫頁面&#xff0c;因為bootstrap有好多media和html5所以要在引入樣式后引入兩個js <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --><!-- WARNI…

s查找mysql服務_MySQL菜鳥實錄(一):MySQL服務安裝實戰

CentOS 7基本信息系統版本&#xff1a; CentOS 7.3 64bit系統配置&#xff1a; 4vCPUs | 8GB磁盤空間&#xff1a;[rootecs-ce5a-0001 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/vda1 40G 17G 22G 44% /devtmpfs 3.9G 0 3.9G 0% /devtmpfs 3.9G 0 3.9G 0% /dev…