服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - MongoDB - MongoDB数据库基础操作总结

MongoDB数据库基础操作总结

2020-08-21 15:59huangyuxin_ MongoDB

这篇文章主要介绍了MongoDB数据库基础操作,结合实例形式总结分析了MongoDB数据库创建、删除、集合、文档等基本操作技巧,需要的朋友可以参考下

本文实例讲述了MongoDB数据库基础操作。分享给大家供大家参考,具体如下:

 

1.创建数据库

 

?
1
2
3
>use test
 
> db.test.insert({"name":1})
  • 插入之后才能查到test

 

2.查看数据库

 

?
1
>show dbs

 

3.删除数据库

 

?
1
2
3
> use test
 
> db.dropDatabase()

 

4.创建集合

 

 

4.1 集合概念

  • 集合就是一组文档,相当于多条记录。
?
1
> db.title.insert({"name":"hyx"})
  • 插入之后即创建集合

 

5.查看集合

 

?
1
> show collections

 

6.删除集合

 

?
1
2
3
>use test
 
>db.title.drop()

 

7.插入文档

 

 

7.1 文档概念

  • 多个键及其关联的值有序地放置在一起就是文档。
  • 文档类似于json数据
?
1
> db.file.insert({name:"huangyuxin",age:11})

 

8.查看文档

 

?
1
>db.files.find()

 

9.变量方式插入文档

 

?
1
2
3
4
5
6
7
8
> document=({by:"hyx"})
{ "by" : "hyx" }
> db.file.insert(document)
WriteResult({ "nInserted" : 1 })
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "huangyuxin", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" }
>

 

10.同时插入多条

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> var res = db.file.insertMany([{"b": 3}, {'c': 4}])
> res
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5c6e8bba0fc535200b893f2b"),
        ObjectId("5c6e8bba0fc535200b893f2c")
    ]
}
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "huangyuxin", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }
>

 

11.更新文档

 

?
1
2
3
4
5
6
7
8
9
> db.file.update({"name":"huangyuxin"},{$set:{"name":"hyx"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }
{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }
>
?
1
2
3
4
5
6
7
8
9
> db.file.save({"_id" : ObjectId("5c6e8b1c0fc535200b893f2a"),"name":"hyx"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }
{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }
>

 

12.删除文档

 

 

12.1删除指定文档

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> db.title.find()
{ "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" }
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }
{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }
> db.file.remove({"b":3})
WriteResult({ "nRemoved" : 1 })
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }
{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }
>

 

12.2删除全部文档

?
1
>db.file.deleteMany({})

 

12.3删除多个文档

?
1
>db.file.deleteMany({ status : 1 })
  • 删除当前库所有status 等于 1 的文档

 

13.条件表达式

 

 

13.1$gt 大于

  • 查询age 大于 0 的数据
?
1
2
3
> db.title.find({age:{$gt : 0}})
{ "_id" : ObjectId("5c6f7d633ea8783bbfb7fd5e"), "age" : 10 }
>

 

13.2 $lt 小于

 

13.3 $gte 大于等于 $lte 小于等于

  • 查询age 大于等于 0 的数据
?
1
> db.title.find({age:{$gte : 1}})

 

13.4 大于小于

?
1
2
3
4
> db.title.find({age:{$lt:13,$gt:10}})
{ "_id" : ObjectId("5c6f7ded3ea8783bbfb7fd5f"), "age" : 12 }
{ "_id" : ObjectId("5c6f7e833ea8783bbfb7fd60"), "age" : 12 }
>

 

13.5 $ne 不等于 $eq 等于

 

14. $type操作符

 

  • $type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。

MongoDB数据库基础操作总结

?
1
2
3
> db.title.find({"name" : {$type : 2}})
{ "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" }
>

 

15. limit()

 

  • 查询指定条数
?
1
2
3
4
> db.title.find().limit(2)
{ "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" }
{ "_id" : ObjectId("5c6f7d633ea8783bbfb7fd5e"), "age" : 10 }
>
  • 第一个 {} 放 where 条件,为空表示返回集合中所有文档。
  • 第二个 {} 指定那些列显示和不显示 (0表示不显示 1表示显示)。
?
1
2
3
> db.title.find({},{"name":1,_id:0}).limit(1)
{ "name" : "yx" }
>

 

16.skip() 

 

  • 跳过几条数据
  • 不要轻易使用Skip来做查询,否则数据量大了就会导致性能急剧下降,这是因为skip是一条一条的数过来的,多了自然就慢了。

 

17.sort()

 

  •  1 为升序排列,而 -1 是用于降序排列。
?
1
2
3
4
5
6
7
8
9
10
11
> db.title.find({},{'age':1,_id:0}).sort({age:1})
{ }
{ "age" : 10 }
{ "age" : 12 }
{ "age" : 12 }
> db.title.find({},{'age':1,_id:0}).sort({age:-1})
{ "age" : 12 }
{ "age" : 12 }
{ "age" : 10 }
{ }
>

 

18.索引

 

 

18.1 创建单个索引

  • 1 为指定按升序创建索引,降序索引指定为 -1
?
1
>db.title.createIndex({"age":1})

 

18.2 创建多个索引

?
1
>db.title.createIndex({"name":1,"age":-1})

 

18.3 查看索引

?
1
>db.col.getIndexes()

 

18.4 查看索引大小

?
1
>db.col.totalIndexSize()

 

18.5 删除所有集合索引

?
1
>db.col.dropIndexes()

 

18.6 删除指定索引

?
1
2
3
>> db.title.dropIndex({'age':1})
{ "nIndexesWas" : 2, "ok" : 1 }
>

希望本文所述对大家MongoDB数据库程序设计有所帮助。

原文链接:https://blog.csdn.net/huangyuxin_/article/details/87866808

延伸 · 阅读

精彩推荐
  • MongoDBMongodb如何开启用户访问控制详解

    Mongodb如何开启用户访问控制详解

    默认启动 MongoDB 服务时没有任何参数,可以对数据库任意操 作,而且可以远程访问数据库,所以推荐开发阶段可以不设置任何参数,但对于生产环境还是要...

    不争5402020-05-10
  • MongoDBMongo服务重启异常问题的处理方法

    Mongo服务重启异常问题的处理方法

    这篇文章主要给大家介绍了关于Mongo服务重启异常问题的处理方法,这个问题其实还是挺常见的,通过此文学习处理方法,以后遇到了就不会措手不及的,需要的...

    Leafage11842021-08-24
  • MongoDBMongodb数据库误删后的恢复方法(两种)

    Mongodb数据库误删后的恢复方法(两种)

    本文给大家分享两种方法来实现Mongodb数据库误删后的恢复,每种方法给大家介绍的都非常详细,需要的朋友参考下吧 ...

    fyg05249922020-05-18
  • MongoDBMongoDB分片测试

    MongoDB分片测试

    分片是mongoDB扩展的一种方式。分片分割一个collection并将不同的部分存储在不同的机器上,本文给大家介绍MongoDB分片测试,需要的朋友参考下吧 ...

    我思,故我在5532020-05-05
  • MongoDBMongoDB简单操作示例【连接、增删改查等】

    MongoDB简单操作示例【连接、增删改查等】

    这篇文章主要介绍了MongoDB简单操作,涉及命令行窗口下使用MongoDB进行简单的连接、增删改查等相关操作技巧,需要的朋友可以参考下 ...

    tinyphp2982020-05-23
  • MongoDBMongoDB 学习笔记

    MongoDB 学习笔记

    最近在学习MongoDB,小结一下,主要都是一些基础知识,需要的朋友可以参考下 ...

    服务器之家3412020-04-25
  • MongoDBMongoDB 简单入门教程(安装、基本概念、创建用户)

    MongoDB 简单入门教程(安装、基本概念、创建用户)

    这篇文章主要介绍了MongoDB 简单入门教程(安装、基本概念、创建用户)的相关资料,帮助大家更好的理解和学习使用MongoDB数据库,感兴趣的朋友可以了解下...

    AsiaYe6352021-05-10
  • MongoDB将MongoDB加入到Windows的本地服务项的方法

    将MongoDB加入到Windows的本地服务项的方法

    下面主要针对MongoDB在Windows下加入本地服务项做一些简单的分享。以方便刚接触MongoDB并在Windows环境下进行开发的同学 ...

    MongoDB教程网3012020-04-28