天天看点

【MongoDB】mongodb 简单入门一、安装二、数据库三、文档四、索引

mongodb 简单入门

  • 一、安装
  • 二、数据库
  • 三、文档
    • 1、基本查询
    • 2、范围查询
    • 3、复杂查询
    • 4、抽出字段
    • 5、文档方法
    • 6、文档更新
    • 7、更多相关的函数
  • 四、索引

一、安装

参考菜鸟教程:点击跳转

二、数据库

  1. 新建数据库

    use [数据库名]

    (建立成功会直接切换到这个数据库)
  2. 当前库信息

    db.stats()

  3. 新建集合

    db.createCollection("[集合名]")

    例如:

    db.createCollection("user")

  4. 查看当前数据库中所有集合

    show collections

  5. 删除表

    db.[数据库名].drop()

    例如:

    db.user.drop()

  6. 删除数据库

    db.dropDatabase("[数据库名]")

    例如:

    db.dropDatabase("test")

三、文档

这里默认创建集合

db.createCollection("user")

=>

user

1、基本查询

  1. 表插入数据:
    db.user.insert(
    	{
    	  id: 1,
    	  name: "zhangsan",
    	  age: 10
        }
    );
               
  2. 查看所有数据

    db.user.find()

  3. 查找指定条件数据

    db.user.find({id:2, name:"zs"})

    查找 id=2, name=“zs” 的这条数据
  4. 循环插入
    for(var i=5; i<10; i++){
    	db.user.insert({id:i, name:"t"+i, age:20+i})
    }
               
  5. 文档数据条数

    db.user.count()

  6. 删除数据
    //删除 id = 1 的数据
    db.user.remove({id:1})
    
    // 删除所有数据
    db.user.remove({})
               

2、范围查询

大于等于 $gte

大于 $gt

小于等于 $lte

小于 $lt

等于 $eq

不等于 $nq

正则 /k/、/^k/

  1. 大于小于查询
// id >= 6
db.user.find({id:{$gte: 6}})

// id > 6
db.user.find({id:{$gt: 6}})

// 正则,查找 name 是测试开头的记录
db.user.find({name:/^测试/})

// 去重某一字段 [ 25, 26, 27, 28, 29, 29 ] => [ 25, 26, 27, 28, 29 ]
db.user.distinct("age")
           

3、复杂查询

或 $or

范围 $in

存在 $exists

  1. $exists
    // 查询 age 字段不存在的记录 { "_id" : ObjectId("61c97a62c6a78592b954dd62"), "id" : 11, "name" : "测试测试测试" }
    db.user.find({age: {$exists: false}})
    
    // 查询 age 字段存在的记录 { "_id" : ObjectId("61c978d2c6a78592b954dd61"), "id" : 9, "name" : "t9", "age" : 29 }
    db.user.find({age: {$exists: true}})
               
  2. $in
    // age 范围在 26 ~ 28 之间的数据
    db.user.find({ age: {$in: [26, 28]} })
               
  3. $or
    // id=5 或 name 已测字开头的
    db.user.find({ $or: [{id:5},{name:/^测/}] })
               

4、抽出字段

相当于关系数据库的

select *

语法:

db.user.find({}, {filed:1, filed:true})

,true / 1 都指代抽出,false / 0 不抽出
// 抽出 name
	db.user.find({ $or: [{id:5},{name:/^u/}] }, {name: true, _id: false})
	// 抽出之前:{ "_id" : ObjectId("61c978d2c6a78592b954dd5d"), "id" : 5, "name" : "t5", "age" : 25 }
	// 抽出之后:{ "name" : "t5" }
	
	// 指定不抽出字段(指定字段不抽出,其他都抽出)
	db.user.find({ $or: [{id:5},{name:/^u/}] }, {name: 0, _id: 0})
	// 原先:{ "_id" : ObjectId("61c978d2c6a78592b954dd5d"), "id" : 5, "name" : "t5", "age" : 25 }
	// 输出:{ "id" : 5, "age" : 25 }
           

5、文档方法

排序:sort()

分页:limit()

跳过:skip()

数据集合:

> db.user.find()
{ "_id" : ObjectId("61c978d2c6a78592b954dd5d"), "id" : 5, "name" : "t5", "age" : 25 }
{ "_id" : ObjectId("61c978d2c6a78592b954dd5e"), "id" : 6, "name" : "t6", "age" : 26 }
{ "_id" : ObjectId("61c978d2c6a78592b954dd5f"), "id" : 7, "name" : "t7", "age" : 27 }
{ "_id" : ObjectId("61c978d2c6a78592b954dd60"), "id" : 8, "name" : "t8", "age" : 28 }
{ "_id" : ObjectId("61c978d2c6a78592b954dd61"), "id" : 9, "name" : "t9", "age" : 29 }
{ "_id" : ObjectId("61c97a62c6a78592b954dd62"), "id" : 11, "name" : "测试测试测试" }
{ "_id" : ObjectId("61c97ab8c6a78592b954dd63"), "id" : 11, "name" : "测试测试测试", "age" : 29 }
           
  1. sort()
    // 按照 id 倒序
    db.user.find().sort({id: -1})
    
    // 按照 id 正序
    db.user.find().sort({id: 1})
               
  2. limit()
    // 截取前两位
    db.user.find().limit(2)
    
    // 输出:{ "_id" : ObjectId("61c97ab8c6a78592b954dd63"), "id" : 11, "name" : "测试测试测试", "age" : 29 }
    // { "_id" : ObjectId("61c97a62c6a78592b954dd62"), "id" : 11, "name" : "测试测试测试"
               
  3. skip()
    // 跳过前两位,再截取两位
    db.user.find().sort({id: -1}).skip(2).limit(2)
    
    // 输出:{ "_id" : ObjectId("61c978d2c6a78592b954dd61"), "id" : 9, "name" : "t9", "age" : 29 }
    // { "_id" : ObjectId("61c978d2c6a78592b954dd60"), "id" : 8, "name" : "t8", "age" : 28 }
               

6、文档更新

  1. update

update([filter], [update], [options])

参数1:过滤参数

参数2:更新数据

参数3:更多选项,可选全部更新等操作

// 更新 id=11 所有数据中的第一条数据, 更新为 id=12
db.user.update( {id: 11}, { $set: {id:12} })

// 更新 id=11 所有数据中 age exists 的第一条数据, 更新为 id=12
db.user.update( {id: 11, age: {$exists:true}}, { $set: {id:12} })

// 更新 id=11 所有数据, 更新为 id=12 (multi: true 更新所有数据)
db.user.update( {id: 11}, { $set: {id:12} }, {multi: true})
           
  1. upsert
更新、如没有这条数据就插入一条

db.user.update( {id: 11}, { $set: {id:12} }, {upsert: true})

7、更多相关的函数

点击跳转

四、索引

db.collection.createIndex(keys, options)
  1. 单列索引

    db.user.createIndex({"id":1})

    1 为指定按升序创建索引,如果你想按降序来创建索引指定为 -1 即可
  2. 复合索引

    db.user.createIndex({"id":1,"age":-1})

    ( id: 升序;age: 降序 )
  3. options 创建索引选项

    options 可选参数点击跳转

    例如:通过在创建索引时加 background:true 的选项,让创建工作在后台执行

    db.values.createIndex({open: 1, close: 1}, {background: true})

  4. 获取文档索引信息

    db.user.getIndexes()

    【MongoDB】mongodb 简单入门一、安装二、数据库三、文档四、索引