开发者社区> 涂作权> 正文

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

简介:  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:查询第

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
JavaScript创建对象(四)——组合使用构造函数和原型模式
在JavaScript创建对象(三)——原型模式中,我们阐述了原型模式存在的两个问题:一是没办法通过构造函数初始化对象属性,二是共享引用类型的数据导致数据错乱。
873 0
如何创建云数据库MongoDB版实例?
本文将为大家介绍云数据库MongoDB实例的创建和开通流程。 登录控制台https://mongodb.console.aliyun.com/replicate/cn-beijing/instances,点击进入MongoDB的管理控制台。
948 0
SpringBoot实现与MongDB非关系型数据库的数据连接【项目实战经验分享】
记录一下SringBoot+Spring Data Mongodb,供大家借鉴和参考,同时也为了回顾一下知识,废话不多说直接上干货。
1521 0
非关系型数据库redis、memcached对比
- redis数据库的优点: (1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1) (2) 支持丰富数据类型,支持string,list,set,sorted set,hash (3) 支...
1393 0
Redis非关系型数据库
Redis非关系型数据库1.简介 Redis是一个基于内存的Key-Value非关系型数据库,由C语言进行编写。 Redis一般作为分布式缓存框架、分布式下的SESSION分离、分布式锁的实现等等。 Redis速度快的原因:基于内存、单线程、多路复用。
1360 0
+关注
涂作权
java,架构,编程语言相关专家
1234
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
OceanBase 入门到实战教程
立即下载
阿里云图数据库GDB,加速开启“图智”未来.ppt
立即下载
实时数仓Hologres技术实战一本通2.0版(下)
立即下载