MongoDB

OverView.png

基本知识;

document database

MongoDB RDBMS
database database
collection table
document row
_id primary key

_id:12‐byte hexademical value 4+3+2+3
时间戳 + 机器标识符 + 进程 + 随机数
documents may have different structures,while records/rows in RDBMS have the same schema

MongDB 命令行使用

1.mongod   => connecting to: mongodb://127.0.0.1:27017
2.mongo

命令行的常用操作

show databases                      查看所有数据库
db                                      查看当前数据库
use db_name                             切换数据库,若数据库不存在则创建,但必须插入数据后,才能算真正将此数据库映射到文件中
db.dropDatabase()                   删除当前数据库
show collections/tables             查看当前数据库下的collection  
db.getCollectionNames()             等价于show collections
db.createCollection('clc_name')     创建一个collection
db.clc_name.drop()                  删除collection_name

DML

db.person.insert({"_id": 1, "name": "john smith"})
使用ObjectId()生成一个id
db.person.insert({"_id": ObjectId(), "name": "john smith"})
id会自动创建
db.person.insert({"name": "john smith"}) 
插入多条数据,其实insert也可以实现
db.person.insertMany([{"name": "kevin small", "age": 35, "scores":[5, 6, 3]}, {"name": "mary lou", "age": 25, "scores":[5,8,2]} ])


插入数据的模式:
db.clc_name.insert(doc)
db.clc_name.insertMany([doc1,doc2,...])
doc格式
{k:v,k:v}


更新数据的格式  $set  添加属性
db.clc_name.update({条件},{$set:{ 更新的属性 }}, {multi:true},{upsert:true})
如果条件为空,则为更新所有的document

$unset 删除属性
db.person.update({}, {$unset: {"status": ""}},
{multi: true})


删除所有的docs
db.person.remove({})
删除指定条件的docs
db.person.remove( { "age": {$gt: 30} } )

DQL

db.person.find()
db.person.find({"name": "kevin small"}) 
db.person.find().pretty()
db.person.find().sort({age:‐1})   1正序 
db.person.find().skip(1).limit(1)
db.person.count()

db.person.distinct("age")  # 返回一个array  [1,2,3] 
db.person.distinct("age").length



查找运算符 $ 
Comparison operators  $lt, $gt, $lte, $gte, $ne, $in, $all
Logical operators     $or, $and, $not
Pattern matching      /pattern/

$exists 表示存在
db.person.find({name: {$not: /john/i,$exists:true}}) 

db.person.find({$or: [{cond1},{cond2}...}]})
db.person.find({$and: [{cond1},{cond2}...]})
db.person.find({age:{$gt: 25, $lt: 35}})

db.person.find({scores: {$all: [2, 5]}})
db.person.find({scores: {$in : [2, 5]}})

$elemMatch 表示元素对应
db.person.find({scores: {$elemMatch: {"midterm": "B", "score": {$gt: 90}}}}) 



关于投影
db.person.find({检查内容}, { 投影属性})  1:取  0:不取

查找子属性,必须要加上“”
db.person.find({"address.city": "LA"})


聚合函数的使用

格式
db.product(
{$match:{xx}}  <-- 对应的是where
{$group: {_id: {cat: "$category", st: "$store"}, total:{$sum:"$qty"}}} <- 对应group by,
{$match: {xx}},  <-- 对应having
{$limit: x},        
{$sort: {xx}},
{$project:{xx}}
)

$match ‐> $group ‐> $match ‐> $limit ‐> $sort

索引的使用

Useful for searching and sorting results

db.users.getIndexes() 获取当前collection的索引
db.users.createIndex({score: 1}) 创建score的索引 1表示正序
db.users.createIndex({ssn: 1}, {unique: true}) 唯一索引
db.users.dropIndex(index_name) 删除索引

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

推荐阅读更多精彩内容

  • 1. 介绍、安装、使用(简单写写,不做详细介绍) 1.1 介绍 Mongodb是属于NoSql的一种数据类型; M...
    Grace_ji阅读 1,576评论 0 0
  • 索引(index) 索引 index经常用于常用的查询,如果设计得好,在创建索引之后的查询会有提升效率的效果。但是...
    我看不见阅读 3,299评论 0 6
  • MongoDB 索引和MySQL索引有相同的特性,甚至于所有的索引都有共同的特性:通常能够极大的提高查询的效率 ...
    糖炒栗子_01c5阅读 627评论 0 0
  • 这里选取一千万条数据来说明问题,这些数据都是没有索引的,首先我们要做的是找出慢查询。1.开启慢查询分析器db.se...
    若尘0328阅读 2,013评论 1 2
  • 客户端https://robomongo.org/ 连接mongodb://[username:password@...
    加勒比兔Z阅读 346评论 0 0