本文整理自网络,侵删。
一、安装和配置
MongoDB 的官方下载站是 http://www.mongodb.org/downloads,可以去上面下载最新的安装程序
Windows 平台的安装
● 步骤一: 下载 MongoDB
点击上方官方下载地址, 并下载 Windows 版本
● 步骤二: 设置 MongoDB 程序存放目录
下载完成后, 解压到自定义文件夹,例: D:\mongodb\
● 步骤三: 设置数据文件存放目录
在 D:\mongodb\ 目录下创建 db 和 logs 文件夹 (和 bin 目录同级),
随后在 logs 文件夹内创建 mongodb.log 日志文件
● 步骤四: 启动 MongoDB 服务, 作为 Windows 服务
//进入 cmd 控制台
D:/mongodb/bin>mongod.exe --logpath D:/mongodb/logs/mongodb.log --logappend
--dbpath D:/mongodb/db
--directoryperdb
--serviceName MongoDB
--install
--directoryperdb 指定每个DB都会新建一个目录
安装完成后,就可以在 cmd 下用命令 net start MongoDB 和 net stop MongoDB 来启动和停止 MongoDB 了
● 步骤五: 客户端连接验证
打开 cmd 输入: D:/mongodb/bin>mongo.exe
D:/mongodb/bin>mongo.exe
MongoDB shell version: 2.4.8
connecting to: test
>
Linux 平台的安装
● 步骤一: 下载 MongoDB
点击上方官方下载地址, 并下载 Linux 版本
● 步骤二: 设置 MongoDB 程序存放目录
下载完成后, 解压到自定义文件夹,例: /usr/local/mongo
● 步骤三: 设置数据文件存放目录
创建 /data/db 和 /data/logs 文件夹, 随后在 logs 文件夹内创建 mongodb.log 日志文件
● 步骤四: 启动 MongoDB 服务, 作为 Linux 服务随机启动
vi /etc/rc.local //使用vi 编辑器打开配置文件,并在其中加入下面一行代码
/usr/local/mongo/bin/mongod --dbpath=/data/db/ --logpath=/data/logs/mongodb.log --logappend&
安装完成后, 可以使用 pkill mongod 来结束
二、数据逻辑结构
● MongoDB 的文档(document),相当于关系数据库中的一行记录。
● 多个文档组成一个集合(collection),相当于关系数据库的表。
● 多个集合(collection),逻辑上组织在一起,就是数据库(database)。
● 一个 MongoDB 实例支持多个数据库(database)。
● 默认端口: 27017
三、常用命令
选择数据库
显示当前数据库
删除当前数据库
显示当前数据库下的集合 Collections
show tables || show collections
显示当前 system.profile
show profile
显示当前数据库下的用户 Users
show users
添加用户
db.addUser(username, password)
删除用户
db.removeUser(username)
四、索引 ensureIndex()
//普通索引
db.persons.ensureIndex({name:1});
db.factories.insert({name: "xyz", metro: {city: "New York", state: "NY"}});
//文档式索引
db.factories.ensureIndex({metro : 1});
//嵌入式索引
db.factories.ensureIndex({"metro.city": 1});
//组合索引
db.things.ensureIndex({name: -1, qty: 1});
//唯一索引
db.user.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
/* 当一个记录被插入到唯一性索引文档时,缺失的字段会以null为默认值被插入文档 */
db.things.save({lastname: "Smith"});
//下面这个操作将会失败,因为 firstname 上有唯一性索引,值为 null
db.things.save({lastname: "Jones"});
//查看索引
db.persons.getIndexes();
//删除所有索引
db.collection.dropIndexes();
//删除单个索引
db.collection.dropIndex({x: 1, y: -1});
五、增删改查等
//定义文档
>doc = {
"_id" : 1,
"author" : "sam",
"title" : "i love you",
"text" : "this is a test",
"tags" : [ "love", "test" ],
"comments" : [
{ "author" : "jim", "comment" : "yes" },
{ "author" : "tom", "comment" : "no" }
]
}
//插入文档
> db.posts.insert(doc);
//查找文档
> db.posts.find({'comments.author':'jim'});
查询 Query
db.users.find({})
//查询出所有 “last_name” 属性值为 “Smith” 的文档记录
db.users.find({'last_name': 'Smith'})
查询选项
> db.user.find( {},{ age : 0 } );
//返回 tags=tennis 除了 comments 的所有列
> db.posts.find( { tags : 'tennis' }, { comments : 0 } );
//返回 userid=16 的 name 字段
> db.user.find( { userid : 16 },{ name : 1 } );
//返回结果:
{ "_id" : 16, "name" : "user16" }
//返回 x=john 的所有 z 字段
> db.things.find( { x : "john" }, { z : 1 } );
//注: _id字段始终都会被返回,哪怕没有明确指定
条件表达式
1) <, <=, >, >=
// 大于: field > value
> db.collection.find( { "field" : { $gt : value } } );
//小于:field < value
> db.collection.find( { "field" : { $lt : value } } );
//大于等于: field >= value
> db.collection.find( { "field" : { $gte : value } } );
//小于等于:field <= value
> db.collection.find( { "field" : { $lte : value } } );
//区间查询 5 < field <= 10
> db.collection.find( { "field" : { $gt : 5, $lte : 10 } } );
$all 操作类似 $in 操作,但是不同的是,$all操作要求数组里面的值全部被包含在返回的记录里面
switched to db test
> db.things.insert( { a : [ 1, 2, 3 ] } );
> db.things.find();
{ "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }
> db.things.find( { a : { $all : [ 2, 3 ] } } );
{ "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }
> db.things.find( { a : { $all : [ 1, 2, 3 ] } } );
{ "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }
> db.things.find( { a : { $all : [ 1 ] } } );
{ "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }
> db.things.find( { a : { $all : [ 1, 2, 3, 4 ] } } );
>
>
$exists 操作检查一个字段是否存在
//userid 字段存在
> db.user.find( { userid : { $exists : true } } ).limit(1);
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 }
//sex 字段不存在
> db.user.find( { sex : { $exists : true } } ).limit(1);
>
>
$mod 操作可以让我们简单的进行取模操作
> db.user.find( "this._id%10==1" ).limit(5);
//$mod操作
> db.user.find( { _id : { $mod : [ 10, 1 ] } } ).limit(5);
$ne 意思是 不等于 (not equal)
{ "_id" : 0, "name" : "user0", "userid" : 0, "age" : 20 }
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 }
> db.user.find( { _id : { $ne : 0 } } ).limit(2);
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 }
$in 操作类似于传统关系数据库中的IN
> db.user.find( { _id : { $in : [ 2, 3 ] } } ).limit(5);
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 }
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 }
$nin 跟 $in 操作相反
//扣掉 _id = 1/2/3/4 的记录
> db.user.find( { _id : { $nin : [ 1, 2, 3, 4 ] } } ).limit(5);
{ "_id" : 0, "name" : "user0", "userid" : 0, "age" : 20 }
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 }
{ "_id" : 6, "name" : "user6", "userid" : 6, "age" : 20 }
$or
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 }
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 }
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 }
>
$nor 跟 $or 相反
{ "_id" : 0, "name" : "user0", "userid" : 0, "age" : 20 }
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 }
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 }
{ "_id" : 6, "name" : "user6", "userid" : 6, "age" : 20 }
>
$size 操作将会查询数组长度等于输入参数的数组
{ "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }
> db.things.find( { a : { $size : 3 } } );
{ "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }
> db.things.find( { a : { $size : 2 } } );
>
> db.things.find( { a : { $size : 1 } } );
>
$where
//同上效果
> db.mycollection.find( function() { return this.a == 3 || this.b == 4; } );
$type 将会根据字段的 BSON 类型来检索数据
//返回 a 是字符串的记录
> db.things.find( { a : { $type : 2 } } );
//返回 a 是 int 类型的记录
> db.things.find( { a : { $type : 16 } } );
类型名称映射
● Double : 1
● String : 2
● Object : 3
● Array : 4
● Binary data : 5
● Object id :7
● Boolean :8
● Date :9
● Null : 10
● Regular expression : 11
● JavaScript code : 13
● Symbol : 14
● JavaScript code with scope : 15
● 32-bit integer : 16
● Timestamp : 17
● 64-bit integer : 18
● Min key : 255
● Max key : 127
相关阅读 >>
mongodb快速入门笔记(六)之mongodb的文档修改操作
windows下安装mongodb以及node.js连接mongodb实例
mongodb db.serverstatus()输出内容中文注释
更多相关阅读请进入《mongodb》频道 >>
数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。