MongoDB学习笔记第一弹

环境:OSX 10.11.2

1、安装

安装教程:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

注:建议采用Homebrew来安装比较方便

2、启动

服务端启动:mongod 

客户端启动:mongo


⚠️:If you get this warning when you connect to mongo shell in Mac OX X:

** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

A simple way to fix this is setting the limit just before startingmongodwith this:

ulimit -n 1024 && mongod

Or this:

launchctl limit maxfiles 1024 1024

But if you are running mongo in a development environment this shouldn’t be a problem, you can just ignore it.

3、连接

通过shell连接MongoDB服务

你可以通过执行以下命令来连接MongoDB的服务。

注意:localhost为主机名,这个选项是必须的:

mongodb://localhost

当你执行以上命令时,你可以看到以下输出结果:

$./mongo

MongoDB shell version:3.0.6

connecting to:test

>mongodb://localhostmongodb://localhost

...

更多连接实例

连接本地数据库服务器,端口是默认的。

mongodb://localhost

使用用户名fred,密码foobar登录localhost的admin数据库。

mongodb://fred:foobar@localhost

使用用户名fred,密码foobar登录localhost的baz数据库。

mongodb://fred:foobar@localhost/baz

连接 replica pair, 服务器1为example1.com服务器2为example2。

mongodb://example1.com:27017,example2.com:27017

连接 replica set 三台服务器 (端口 27017, 27018, 和27019):

mongodb://localhost,localhost:27018,localhost:27019

连接 replica set 三台服务器, 写入操作应用在主服务器 并且分布查询到从服务器。

mongodb://host1,host2,host3/?slaveOk=true

直接连接第一个服务器,无论是replica set一部分或者主服务器或者从服务器。

mongodb://host1,host2,host3/?connect=direct;slaveOk=true

当你的连接服务器有优先级,还需要列出所有服务器,你可以使用上述连接方式。

安全模式连接到localhost:

mongodb://localhost/?safe=true

以安全模式连接到replica set,并且等待至少两个复制服务器成功写入,超时时间设置为2秒。

mongodb://host1,host2,host3/?safe=true;w=2;wtimeoutMS=2000

4、创建数据库

MongoDB 创建数据库的语法格式如下:

use DATABASE_NAME

>db.runoob.insert({"name":"菜鸟教程"})

WriteResult({"nInserted":1})

>show dbs

local 0.078G

执行删除命令:

>db.dropDatabase()

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

删除集合

集合删除语法格式如下:

db.collection.drop()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mogon简介 MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可...
    猪哥亮阅读 2,016评论 0 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • NoSql数据库优缺点 在优势方面主要体现在下面几点: 简单的扩展 快速的读写 低廉的成本 灵活的数据模型 在不足...
    dreamer_lk阅读 2,776评论 0 6
  • [TOC]未完更新中... 1. MongoDB安装及配置 官网下载地址 安装msc文件,现在3.4的版本,有了m...
    BigJeffWang阅读 2,291评论 0 49
  • 又一次考试结束,今晚注定要熬到深夜。不记得有多久了,没有在零点之前入睡。是不是其他的同行业和我一样呢? ...
    风雨同舟_f997阅读 94评论 0 0