Database 数据库程序员我爱编程

MongoDB 基础

2015-11-06  本文已影响255人  KeKeMars

基础

数据

存储数据方式:BSON

Collection 集合 > Key 键 > Value 值 > Document/Object (文档/对象)

数据类型

string integer boolean double null array object timestamp ObjectIDs InternationalizedStrings(UTF-8字符串)

数据库

连接

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

切换

use myinfo

备份(mongodump)

mongodump -h dbhost -d dbname -o dbdirectory

恢复(mongorerstore)

mongorestore -h dbhost -d dbname --�drop dbdirectory

监控

MongoDB中提供了mongostat 和 mongotop 两个命令来监控MongoDB的运行情况。

查询分析

常用函数有:

explain() //提供了查询信息,使用索引及查询统计等
hint()//强迫MongoDB使用一个指定的索引。

管理工具 Rockmongo

增删改查

集合操作

数据库操作

数据操作

条件操作符
$lt $gt $lte $gte $ne $type $inc $not

函数

索引

db.collection.ensureIndex({KEY:1/-1})

全文检索

db.adminCommand({setParameter:true,textSearchEnabled:true})
mongod --setParameter textSearchEnabled=true

管道命令

原子操作

数据库引用

DBRefs
{ $ref : , $id : , $db : }

{ 
"_id":ObjectId("53402597d852426020000002"),
"address": {
"$ref": "address_home",
"$id": ObjectId("534009e4d852427820000002"),
"$db": "w3cschoolcc"},
"contact": "987654321",
"dob": "01-01-1991",
"name": "Tom Benzamin"
}
> var user = db.users.findOne({"name":"Tom Benzamin"})
> var dbRef = user.address
> db[dbRef.$ref].findOne({"_id":(dbRef.$id)})
{
"_id" : ObjectId("534009e4d852427820000002"),
"building" : "22 A, Indiana Apt",
"pincode" : 123456,
"city" : "Los Angeles",
"state" : "California"
}

参考

上一篇 下一篇

猜你喜欢

热点阅读