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.mzph.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…