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



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({条件});

eg:db.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:查询第

 

时间: 2024-11-29 02:57:18

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

MongoDB.NET 2.2.4驱动版本对Mongodb3.3数据库中GridFS增删改查_MongoDB

本文实例为大家分享了针对Mongodb3.3数据库中GridFS增删改查,供大家参考,具体内容如下 Program.cs代码如下: internal class Program { private static void Main(string[] args) { GridFSHelper helper = new GridFSHelper("mongodb://localhost", "GridFSDemo", "Pictures"); #re

nodejs连接mongodb数据库实现增删改查_node.js

准备 1.通过npm命令安装mongodb 2.安装mongodb数据库,这里不详细介绍了,安装网址:http://www.jb51.net/article/82522.htm CRUD操作 在此之前应对MongoDB数据库有所了解,知道它的一些增删查改命令. 1.增加 var MongoClient = require("mongodb").MongoClient; var DB_URL = "mongodb://localhost:27017/chm"; fun

Android编程连接MongoDB及增删改查等基本操作示例

本文实例讲述了Android编程连接MongoDB及增删改查等基本操作.分享给大家供大家参考,具体如下: MongoDB简介 Mongodb,分布式文档存储数据库,由C++语言编写,旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++开发. Mongo安装参考 1)下载安装包文件,解压到某一文件夹下. 官方下载地址:htt

Mongodb c#增删改查

写在前面 最近项目需要,就研究了下mongodb,也是为了快速上手,就自己弄了一个简单的例子,这里记录一下. Mongodb 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象(document)三个层次组成.MongoDB对于关系型数据库里的表,但是集合中没有列.行和关系概念,这体现了模式自由的特点. 那么在c#如何使用呢?下面看个例子,你会发现上手非常简

PHP简单操作MongoDB的方法(安装及增删改查)_php技巧

本文实例讲述了PHP简单操作MongoDB的方法.分享给大家供大家参考,具体如下: php操作MongoDB的话首先从网上下载MongoDB的扩展包,https://github.com/mongodb/mongo-php-driver/downloads,选择对应的扩展包. 这是我下的,然后解压,VC6适合apache,VC9适合IIS,ts(thread safe)指PHP以模块形式运行的. 然后把其中的php_mongo.dll放在PHP中的ext文件夹中,然后在PHP.INI里面加入ex

Mongodb + PHP 进行增删改查(CRUD)操作教程

用户使用MongoDB开发应用程序使用的是逻辑结构: ● MongoDB的文档,相当于关系数据库中的一行记录 ● 多个文档组成一个集合,相当于关系数据库中的表 ● 多个集合逻辑上组织在一起就是数据库 在安装配置好MongoDB后,我们开始用PHP来实现对数据库的基本操作.所谓基本操作,自然就是我们常说的增删改查(又称CRUD)了. 一.PHP扩展配置 PHP默认是不带mongodb扩展的,在http://www.php.net/manual/en/mongo.installation.php可以

mongodb+php实现简单的增删改查_MongoDB

windows上安装mongodb的php扩展 下载地址https://s3.amazonaws.com/drivers.mongodb.org/php/index.html 找到对应的php版本的dll文件,下载php_mongo.dll,放到php安装目录下的ext目录中,修改php.ini,添加一项extension=php_mongo.dll,没有找到支持php7的dll 获取MongoClient对象,new出来 获取数据库对象db,通过MongoClient对象的数据库属性,$Mon

MySQL数据库学习笔记(十一)----DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)

[正文] 一.DAO模式简介 DAO即Data Access Object,数据访问接口.数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据库资源中间. DAO模式实际上是两个模式的组合,即Data Accessor (数据访问者)模式和 Active Domain Object(领域对象)模式.Data Accessor 模式实现了数据访问和业务逻辑的分离:Active Domain Object 模式实现了业务数据的对象化封装. 需要注意的是,DAO设计模式是Java EE中的设计模式

[Android] SQLite数据库之增删改查基础操作

    在编程中经常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,能够支持Windows/Linux/Unix等主流操作系统,同时能够跟很多程序语言如C#.PHP.Java等相结合.下面先回顾SQL的基本语句,再讲述Android的基本操作. 一. adb shell回顾SQL语句     首先,我感觉自己整个大学印象最深的几门课就包括<数据库>,所以想先回顾SQL增删改查的基本语句.而在Androi