2.非关系型数据库(Nosql)之mongodb:mongodb显示所有数据库,使用数据库,集合创建(显示和隐式创建),集合查询,初步数据的增删改查,分页

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介:  1代开一个cmd窗口,当作mongodb客户端,执行以下命令: mongo localhost:27017 2查看所有的数据库: > show dbs; admin (empty) local 0.078GB 3 db命令,查看当前窗口在那个数据库下面(test数据库没有任何数据,在内存里面):


1代开一个cmd窗口,当作mongodb客户端,执行以下命令:

mongo localhost:27017

2查看所有的数据库:

> show dbs;

admin (empty)

local 0.078GB

3 db命令,查看当前窗口在那个数据库下面(test数据库没有任何数据,在内存里面):

4创建集合

A 显示创建

db.createCollection(“collectionName”);

> db.createCollection("c1")

{ "ok" : 1 }

B 隐式创建

> db.c2.insert({name:"zhangsan",age:31})

WriteResult({ "nInserted" : 1 });

显示集合

show collections;

5 CRUD

A添加

db.collectionName.insert({})

> db.c2.insert({name:"zhangsan",age:31})

WriteResult({ "nInserted" : 1 })

> show collections;

c1

c2

system.indexes

> var i = 1+2;

> i

3

> for(var i = 1 ; i <= 10000;i++){

... db.c1.insert({name:'toto',age:24})

... }

WriteResult({ "nInserted" : 1 })

清屏命令:cls

B删除

db.collectionName.remove();

> db.c4.insert({name:"zhangsan",age:20});

WriteResult({ "nInserted" : 1 })

> db.c4.insert({name:"lisi",age:24});

WriteResult({ "nInserted" : 1 })

db.c4.find();

> db.c4.remove({name:"zhangsan"});

WriteResult({ "nRemoved" : 1 })

> db.c4.find();

{ "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 }

C查询

A db.collectionName.find({条件});

egdb.c3.find();

再如:

> db.c4.find({name:"lisi"});

{ "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 }

B查找集合中的第一条数据

db.collectionName.findOne();

eg

> db.c4.find();

{ "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 }

{ "_id" : ObjectId("543e0d96e2c90313035e7ce4"), "name" : "wangwu", "age" : 20 }

{ "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 }

{ "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 }

> db.c4.findOne();

{

       "_id" : ObjectId("543e0b6ae2c90313035e7ce3"),

       "name" : "lisi",

       "age" : 24

}

注意:_id每个表都有,是mongodb自动维护的。

C查询集合中的文档,返回某些特定的键值

除了查询表达式以外,Mongodb还支持一些额外的参数选项,例如,我们可能仅仅只想返回某些特定的字段值:

> db.c4.find({name:"wangwu"},{name:1});

{ "_id" : ObjectId("543e0d96e2c90313035e7ce4"), "name" : "wangwu" }

D查询显示问题

> for(var i = 1; i<=30;i++){

... db.c4.insert({name:"tuzuoquan",age:i});

... }

WriteResult({ "nInserted" : 1 })

查询:

> db.c4.find();

{ "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 }

{ "_id" : ObjectId("543e0d96e2c90313035e7ce4"), "name" : "wangwu", "age" : 20 }

{ "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 }

{ "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 }

{ "_id" : ObjectId("543e104fe2c90313035e7ce7"), "name" : "tuzuoquan", "age" : 1 }

{ "_id" : ObjectId("543e104fe2c90313035e7ce8"), "name" : "tuzuoquan", "age" : 2 }

{ "_id" : ObjectId("543e104fe2c90313035e7ce9"), "name" : "tuzuoquan", "age" : 3 }

{ "_id" : ObjectId("543e104fe2c90313035e7cea"), "name" : "tuzuoquan", "age" : 4 }

{ "_id" : ObjectId("543e104fe2c90313035e7ceb"), "name" : "tuzuoquan", "age" : 5 }

{ "_id" : ObjectId("543e104fe2c90313035e7cec"), "name" : "tuzuoquan", "age" : 6 }

{ "_id" : ObjectId("543e104fe2c90313035e7ced"), "name" : "tuzuoquan", "age" : 7 }

{ "_id" : ObjectId("543e104fe2c90313035e7cee"), "name" : "tuzuoquan", "age" : 8 }

{ "_id" : ObjectId("543e104fe2c90313035e7cef"), "name" : "tuzuoquan", "age" : 9 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf0"), "name" : "tuzuoquan", "age" : 10 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf1"), "name" : "tuzuoquan", "age" : 11 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf2"), "name" : "tuzuoquan", "age" : 12 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf3"), "name" : "tuzuoquan", "age" : 13 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf4"), "name" : "tuzuoquan", "age" : 14 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf5"), "name" : "tuzuoquan", "age" : 15 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf6"), "name" : "tuzuoquan", "age" : 16 }

Type "it" for more

> it

{ "_id" : ObjectId("543e104fe2c90313035e7cf7"), "name" : "tuzuoquan", "age" : 17 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf8"), "name" : "tuzuoquan", "age" : 18 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf9"), "name" : "tuzuoquan", "age" : 19 }

{ "_id" : ObjectId("543e104fe2c90313035e7cfa"), "name" : "tuzuoquan", "age" : 20 }

{ "_id" : ObjectId("543e104fe2c90313035e7cfb"), "name" : "tuzuoquan", "age" : 21 }

{ "_id" : ObjectId("543e104fe2c90313035e7cfc"), "name" : "tuzuoquan", "age" : 22 }

{ "_id" : ObjectId("543e104fe2c90313035e7cfd"), "name" : "tuzuoquan", "age" : 23 }

{ "_id" : ObjectId("543e104fe2c90313035e7cfe"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e104fe2c90313035e7cff"), "name" : "tuzuoquan", "age" : 25 }

{ "_id" : ObjectId("543e104fe2c90313035e7d00"), "name" : "tuzuoquan", "age" : 26 }

{ "_id" : ObjectId("543e104fe2c90313035e7d01"), "name" : "tuzuoquan", "age" : 27 }

{ "_id" : ObjectId("543e104fe2c90313035e7d02"), "name" : "tuzuoquan", "age" : 28 }

{ "_id" : ObjectId("543e104fe2c90313035e7d03"), "name" : "tuzuoquan", "age" : 29 }

{ "_id" : ObjectId("543e104fe2c90313035e7d04"), "name" : "tuzuoquan", "age" : 30 }

> db.c4.count();

34

>总结:

Mongodb客户端默认一次显示20条记录

 

Eg:显示age大于20个数据:

> db.c4.find({age:{$gt:20}});

{ "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 }

{ "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 }

{ "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 }

{ "_id" : ObjectId("543e104fe2c90313035e7cfb"), "name" : "tuzuoquan", "age" : 21 }

{ "_id" : ObjectId("543e104fe2c90313035e7cfc"), "name" : "tuzuoquan", "age" : 22 }

{ "_id" : ObjectId("543e104fe2c90313035e7cfd"), "name" : "tuzuoquan", "age" : 23 }

{ "_id" : ObjectId("543e104fe2c90313035e7cfe"), "name" : "tuzuoquan", "age" : 24 }

{ "_id" : ObjectId("543e104fe2c90313035e7cff"), "name" : "tuzuoquan", "age" : 25 }

{ "_id" : ObjectId("543e104fe2c90313035e7d00"), "name" : "tuzuoquan", "age" : 26 }

{ "_id" : ObjectId("543e104fe2c90313035e7d01"), "name" : "tuzuoquan", "age" : 27 }

{ "_id" : ObjectId("543e104fe2c90313035e7d02"), "name" : "tuzuoquan", "age" : 28 }

{ "_id" : ObjectId("543e104fe2c90313035e7d03"), "name" : "tuzuoquan", "age" : 29 }

{ "_id" : ObjectId("543e104fe2c90313035e7d04"), "name" : "tuzuoquan", "age" : 30 }

 

Eg:显示age >= 25的数据:

db.c4.find({age:{$gte:25}});

> db.c4.find({age:{$gte:25}});

{ "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 }

{ "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 }

{ "_id" : ObjectId("543e104fe2c90313035e7cff"), "name" : "tuzuoquan", "age" : 25 }

{ "_id" : ObjectId("543e104fe2c90313035e7d00"), "name" : "tuzuoquan", "age" : 26 }

{ "_id" : ObjectId("543e104fe2c90313035e7d01"), "name" : "tuzuoquan", "age" : 27 }

{ "_id" : ObjectId("543e104fe2c90313035e7d02"), "name" : "tuzuoquan", "age" : 28 }

{ "_id" : ObjectId("543e104fe2c90313035e7d03"), "name" : "tuzuoquan", "age" : 29 }

{ "_id" : ObjectId("543e104fe2c90313035e7d04"), "name" : "tuzuoquan", "age" : 30 }

 

Eg:显示age <= 15的数据:

> db.c4.find({age:{$lte:15}});

{ "_id" : ObjectId("543e104fe2c90313035e7ce7"), "name" : "tuzuoquan", "age" : 1 }

{ "_id" : ObjectId("543e104fe2c90313035e7ce8"), "name" : "tuzuoquan", "age" : 2 }

{ "_id" : ObjectId("543e104fe2c90313035e7ce9"), "name" : "tuzuoquan", "age" : 3 }

{ "_id" : ObjectId("543e104fe2c90313035e7cea"), "name" : "tuzuoquan", "age" : 4 }

{ "_id" : ObjectId("543e104fe2c90313035e7ceb"), "name" : "tuzuoquan", "age" : 5 }

{ "_id" : ObjectId("543e104fe2c90313035e7cec"), "name" : "tuzuoquan", "age" : 6 }

{ "_id" : ObjectId("543e104fe2c90313035e7ced"), "name" : "tuzuoquan", "age" : 7 }

{ "_id" : ObjectId("543e104fe2c90313035e7cee"), "name" : "tuzuoquan", "age" : 8 }

{ "_id" : ObjectId("543e104fe2c90313035e7cef"), "name" : "tuzuoquan", "age" : 9 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf0"), "name" : "tuzuoquan", "age" : 10 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf1"), "name" : "tuzuoquan", "age" : 11 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf2"), "name" : "tuzuoquan", "age" : 12 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf3"), "name" : "tuzuoquan", "age" : 13 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf4"), "name" : "tuzuoquan", "age" : 14 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf5"), "name" : "tuzuoquan", "age" : 15 }

 

Eg:显示age不等于 2的数据:

> db.c4.find({age:{$ne:2}});

{ "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 }

{ "_id" : ObjectId("543e0d96e2c90313035e7ce4"), "name" : "wangwu", "age" : 20 }

{ "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 }

{ "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 }

{ "_id" : ObjectId("543e104fe2c90313035e7ce7"), "name" : "tuzuoquan", "age" : 1 }

{ "_id" : ObjectId("543e104fe2c90313035e7ce9"), "name" : "tuzuoquan", "age" : 3 }

{ "_id" : ObjectId("543e104fe2c90313035e7cea"), "name" : "tuzuoquan", "age" : 4 }

{ "_id" : ObjectId("543e104fe2c90313035e7ceb"), "name" : "tuzuoquan", "age" : 5 }

{ "_id" : ObjectId("543e104fe2c90313035e7cec"), "name" : "tuzuoquan", "age" : 6 }

{ "_id" : ObjectId("543e104fe2c90313035e7ced"), "name" : "tuzuoquan", "age" : 7 }

{ "_id" : ObjectId("543e104fe2c90313035e7cee"), "name" : "tuzuoquan", "age" : 8 }

{ "_id" : ObjectId("543e104fe2c90313035e7cef"), "name" : "tuzuoquan", "age" : 9 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf0"), "name" : "tuzuoquan", "age" : 10 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf1"), "name" : "tuzuoquan", "age" : 11 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf2"), "name" : "tuzuoquan", "age" : 12 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf3"), "name" : "tuzuoquan", "age" : 13 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf4"), "name" : "tuzuoquan", "age" : 14 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf5"), "name" : "tuzuoquan", "age" : 15 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf6"), "name" : "tuzuoquan", "age" : 16 }

{ "_id" : ObjectId("543e104fe2c90313035e7cf7"), "name" : "tuzuoquan", "age" : 17 }

Type "it" for more

 

> db.c4.find().count();

34

> db.c4.find({age:{$ne:20}}).count();

32

> db.c4.find({age:{$ne:2}}).count();

33

E :分页

db.c4.find().skip(0).limit(10);

> db.c4.find().skip(0).limit(10);

{ "_id" : ObjectId("543e0b6ae2c90313035e7ce3"), "name" : "lisi", "age" : 24 }

{ "_id" : ObjectId("543e0d96e2c90313035e7ce4"), "name" : "wangwu", "age" : 20 }

{ "_id" : ObjectId("543e0da0e2c90313035e7ce5"), "name" : "zhaoliu", "age" : 25 }

{ "_id" : ObjectId("543e0da8e2c90313035e7ce6"), "name" : "tianqi", "age" : 25 }

{ "_id" : ObjectId("543e104fe2c90313035e7ce7"), "name" : "tuzuoquan", "age" : 1 }

{ "_id" : ObjectId("543e104fe2c90313035e7ce8"), "name" : "tuzuoquan", "age" : 2 }

{ "_id" : ObjectId("543e104fe2c90313035e7ce9"), "name" : "tuzuoquan", "age" : 3 }

{ "_id" : ObjectId("543e104fe2c90313035e7cea"), "name" : "tuzuoquan", "age" : 4 }

{ "_id" : ObjectId("543e104fe2c90313035e7ceb"), "name" : "tuzuoquan", "age" : 5 }

{ "_id" : ObjectId("543e104fe2c90313035e7cec"), "name" : "tuzuoquan", "age" : 6 }

Eg:查询第

 

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
21天前
|
NoSQL Cloud Native atlas
探索云原生数据库:MongoDB Atlas 的实践与思考
【10月更文挑战第21天】本文探讨了MongoDB Atlas的核心特性、实践应用及对云原生数据库未来的思考。MongoDB Atlas作为MongoDB的云原生版本,提供全球分布式、完全托管、弹性伸缩和安全合规等优势,支持快速部署、数据全球化、自动化运维和灵活定价。文章还讨论了云原生数据库的未来趋势,如架构灵活性、智能化运维和混合云支持,并分享了实施MongoDB Atlas的最佳实践。
|
22天前
|
NoSQL Cloud Native atlas
探索云原生数据库:MongoDB Atlas 的实践与思考
【10月更文挑战第20天】本文探讨了MongoDB Atlas的核心特性、实践应用及对未来云原生数据库的思考。MongoDB Atlas作为云原生数据库服务,具备全球分布、完全托管、弹性伸缩和安全合规等优势,支持快速部署、数据全球化、自动化运维和灵活定价。文章还讨论了实施MongoDB Atlas的最佳实践和职业心得,展望了云原生数据库的发展趋势。
|
23天前
|
存储 NoSQL MongoDB
MongoDB 数据库引用
10月更文挑战第20天
14 1
|
30天前
|
存储 NoSQL MongoDB
基于阿里云数据库MongoDB版,微财数科“又快又稳”服务超7000万客户
选择MongoDB主要基于其灵活的数据模型、高性能、高可用性、可扩展性、安全性和强大的分析能力。
|
28天前
|
存储 NoSQL MongoDB
mongodb的数据库表怎么创建
在此过程中,理解并掌握这些基本操作,是深入探索MongoDB魅力,乃至构建高效数据解决方案的关键所在。通过实践,您将更加深刻地体会到这种随需应变的数据管理模式带来的便利与效率提升。
24 0
|
30天前
|
存储 NoSQL MongoDB
小川科技携手阿里云数据库MongoDB:数据赋能企业构建年轻娱乐生态
基于MongoDB灵活模式的特性,小川实现了功能的快速迭代和上线,而数据库侧无需任何更改
|
30天前
|
运维 NoSQL BI
简道云搭载阿里云MongoDB数据库,帮助数以万计企业重构业务系统
通过与MongoDB和阿里云团队的合作,让简道云少走了弯路,保障了线上服务的长期稳定运行,提高了吞吐效率,并相应降低了线上运行成本
|
1月前
|
存储 关系型数据库 MySQL
一个项目用5款数据库?MySQL、PostgreSQL、ClickHouse、MongoDB区别,适用场景
一个项目用5款数据库?MySQL、PostgreSQL、ClickHouse、MongoDB——特点、性能、扩展性、安全性、适用场景比较
|
2月前
|
存储 NoSQL 关系型数据库
非关系型数据库-MongoDB技术(二)
非关系型数据库-MongoDB技术(二)
|
2月前
|
NoSQL 关系型数据库 MongoDB
非关系型数据库-MongoDB技术(一)
非关系型数据库-MongoDB技术(一)