mongodb基础/mongodb入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013862108/article/details/87855970

学习网站:

  1. MongoDB官网: www.mongodb.org    //安装包的下载, 使用文档
  2. MongoDB国内官方网站: www.mongoing.com   //
  3. 中文文档地址: docs.mongoing.com
  4. MongoDB的 github:  github.com/mongodb
  5. MongoDB的jira:  jira.mongodb.org
  6. 两个google  groups:     mongodb-user  与  mongo-cn

数据库分类:

  1. Sql 数据库: 支持Sql 语言的数据库 。如: Mysql Oracle
  2. NoSql 数据库:不支持sql 语言的数据库。Redis, MongoDB…  Not Only Sql

mongodb 特点:

无数据结构限制

没有表结构的概念,每条记录可以有完全不同的结构

适合快捷开发和多变的业务需求

redis 的key-value;   hbase的单索引需要自己实现

完全的索引支持

单键索引;

多键索引;

数组索引;

全文索引;

地理位置索引;

客户端命令:

[root@liang mongodb]# ./bin/mongo 127.0.0.1:12345/test

> db.shutdownServer()

基本操作: 写入,查询;

查看有哪些数据库:

> show dbs

admin   0.000GB

config  0.000GB

local   0.000GB

插入一条记录

> use imooc

switched to db imooc

> db.imooc_collection.insert({x:1})

WriteResult({ "nInserted" : 1 })

//删除数据库; 先use ,再drop

> use imooc

switched to db imooc

> db.dropDatabase()

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

查找记录/ 查找所有记录

> db.imooc_collection.find()

{ "_id" : ObjectId("5c4ee829342ab373e60f1d63"), "x" : 1 }

//for 循环插入:

> for(i=3;i<100;i++)db.imooc_collection.insert({x:i})

WriteResult({ "nInserted" : 1 })

//使用 skip  , limit 查询

> db.imooc_collection.find().skip(3).limit(2).sort({x:1})

{ "_id" : ObjectId("5c4eec54342ab373e60f1dc8"), "x" : 4 }

{ "_id" : ObjectId("5c4eec54342ab373e60f1dc9"), "x" : 5 }

//指定条件 更新记录

> db.imooc_collection.update({x:1},{x:999})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

//更新部分字段 使用$set 操作符

> db.imooc_collection.insert({x:100, y:100, z:100})

> db.imooc_collection.update({z:100},{$set:{y:99}})

> db.imooc_collection.find({x:100})

{ "_id" : ObjectId("5c4efc02342ab373e60f1e28"), "x" : 100, "y" : 99, "z" : 100 }

//更新不存在的数据,不存在就插入;第三个参数true

> db.imooc_collection.update({y:100},{y:999}, true)

//update 默认只更新找到的第一条数据(即便有多条满足)//防止误操作

//想更新所有满足查询条件的数据,第四个参数要设置为true

> db.imooc_collection.update({y:100},{y:999}, true)

//删除

//删除指定条件的记录

> db.imooc_collection.remove({c:2})

//删除表/ 删除集合

> db.imooc_collection.drop()

//聚合函数  /  聚合查询

//统计记录数目:

> db.imooc_collection.find().count()

索引:

//查看集合的索引

> db.imooc_collection.getIndexes()

//创建索引

// 1 正向排序, -1 逆向排序

> db.imooc_collection.ensureIndex({x:1})

索引种类:

_id索引

单键索引:

  是最普通的索引:

  > db.imooc_collection.ensureIndex({x:1})

多键索引:

多键索引与单键索引创建形式相同,区别在于字段的值。

单键索引:值为一个单一的值,例如字符串,数字,或者日期

多键索引:值具有多个记录,例如数组。

复合索引

当查询条件不只有一个时,就需要建立复合索引

插入{x:1,y:2,z:3} 记录, 按照x与y的值查询, 创建索引

> db.imooc_collection.ensureIndex({x:1, y:1})

过期索引

全文索引

地理位置索引

猜你喜欢

转载自blog.csdn.net/u013862108/article/details/87855970