IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> mongoDB查询文档使用 -> 正文阅读

[大数据]mongoDB查询文档使用

db.collection.find(query_condition, projection_field):

query_condition:查询的条件,可以设置单个条件、多个条件(and、or)、对于field可以进行类似shell编程中的关系运算符,但是并不完全相同。例如:在shell中不小于即为:$ge ,而在mongoDB中需要使用$gte。

projection_field:设定返回的field;

???自动生成的ID是必须要进行设定说明,如果不设定是默认为1则返回,设定为0,则不返回;?

? ?其他filed,如果都不设置即默认为1进行全部返回,如果任何field设置为1,其他字段则默认为0,不返回。


关于查询条件使用的案例

  1. 关于关系运算符:
    关于运算符的说明
    关系运算符说明
    $eq等于
    $ne不等于
    $gt大于
    $gte不小于
    $lt小于
    $lte不大于
  2. 依赖于上面的顺序,现在举例说明

全量的文档:

> db.localfindb.localfidb.localfdb.localdb.local.find()db.local.find()
{ "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 }
{ "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" }
{ "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" }
{ "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" }
{ "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }
>?
提取年龄=90的
>db.local.find({"age":{$eq:90}})
{ "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" }
>?
> db.local.find({"age":90})
{ "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" }
>?

提取年龄不等于90的

> db.local.find({"age":{$ne:90}})db.local.find({"age":{$ne:90}})
{ "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 }
{ "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" }
{ "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" }
{ "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }
>?


提取年龄大于90的

> db.local.find({"age":{$gt:90}})db.local.find({"age":{$gt:90}})
{ "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 }
{ "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" }
{ "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }
>?

提取年龄不小于90的

> db.local.find({"age":{$gte:90}})db.local.find({"age":{$gte:90}})
{ "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 }
{ "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" }
{ "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" }
{ "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }
>?
>?

提取年龄小于90的

> db.local.find({"age":{$lt:90}})db.local.find({"age":{$lt:90}})
{ "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" }
>?

提取年龄不大于90的

> db.local.find({"age":{$lte:90}})db.local.find({"age":{$lte:90}})
{ "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" }
{ "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" }
>?


关于and条件的使用

全量文档如下:

> db.localfindb.localfidb.localfdb.localdb.local.find()db.local.find()
{ "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 }
{ "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" }
{ "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" }
{ "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" }
{ "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }

> db.local.find({$and:[{"age":100,"sex":"femal"}]})
{ "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
>?
>db.local.find({"age":100,"sex":"femal"})
{ "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }


关于or条件的使用

全量文档如下:

> db.localfindb.localfidb.localfdb.localdb.local.find()db.local.find()
{ "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 }
{ "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" }
{ "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" }
{ "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" }
{ "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }

> db.local.find({$or:[{"age":70},{"sex":"man"}]})db.local.find({$or:[{"age":70},{"sex":"man"}]})
{ "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" }
{ "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" }
>?
>?


关于and条件和or条件的联合使用

> db.local.find({"age":100,$or:[{"sex":"man"},{"sex":"femal"}]})
{ "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" }
{ "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
{ "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }


关于查询字段的使用样例:

  1. 查询全部的文档全部的field:

    > db.local.find()
    { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 }
    { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" }
    { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" }
    { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" }
    { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }

    > db.local.find({},{})db.local.find({},{})
    { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "name" : "zhengzw", "age" : 100 }
    { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "name" : "zhengzw", "age" : 100, "sex" : "man" }
    { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f3deea30f42c635791d7"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f3deea30f42c635791d8"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f40dea30f42c635791d9"), "name" : "zhengzw", "age" : 90, "sex" : "femal" }
    { "_id" : ObjectId("6139f41bea30f42c635791da"), "name" : "zhengzw", "age" : 70, "sex" : "femal" }
    { "_id" : ObjectId("6139f42bea30f42c635791db"), "name" : "zhengzw", "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f446ea30f42c635791dc"), "name" : "zhengzw", "age" : 110, "sex" : "femal" }
    >?

  2. 查询全部文档中的两个field(显示ID)

    > db.local.find({},{"age":1,"sex":1} ?)
    { "_id" : ObjectId("6139d43dbae7eecd0acf51c6"), "age" : 100 }
    { "_id" : ObjectId("6139d4e7bae7eecd0acf51c7"), "age" : 100, "sex" : "man" }
    { "_id" : ObjectId("6139f3b5ea30f42c635791d6"), "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f3deea30f42c635791d7"), "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f3deea30f42c635791d8"), "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f40dea30f42c635791d9"), "age" : 90, "sex" : "femal" }
    { "_id" : ObjectId("6139f41bea30f42c635791da"), "age" : 70, "sex" : "femal" }
    { "_id" : ObjectId("6139f42bea30f42c635791db"), "age" : 100, "sex" : "femal" }
    { "_id" : ObjectId("6139f446ea30f42c635791dc"), "age" : 110, "sex" : "femal" }

  3. 查询全部文档中的两个field(不显示ID)

    >db.local.find({},{"_id":0,"age":1,"sex":1})
    { "age" : 100 }
    { "age" : 100, "sex" : "man" }
    { "age" : 100, "sex" : "femal" }
    { "age" : 100, "sex" : "femal" }
    { "age" : 100, "sex" : "femal" }
    { "age" : 90, "sex" : "femal" }
    { "age" : 70, "sex" : "femal" }
    { "age" : 100, "sex" : "femal" }
    { "age" : 110, "sex" : "femal" }
    >

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-09-11 18:53:24  更:2021-09-11 18:54:16 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 19:41:18-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码