MongoDB 使用过程中经常需要根据字段的类型来查询数据, 而MongoDB中查询字段类型是通过$type操作符来实现.

$type使用法语:

db.集合名.find({$type:类型值});    //这里的类型值能使用Number也能使用alias

举个例子:

db.person.find({address:{$type:2}});         //查询address字段数据类型为字符串
db.person.find({address:{$type:"string"}}); //查询address字段数据类型为字符串

再举个有点特殊的查询,关于null 查询的例子:

db.person.find({address:null});             //注意,这样查询会将没有 address 列的数据一并查询出来
db.person.find({address:{$exists:true, $eq:null}}); //这样查询的是 address 列值为null 的数据
db.person.find({address:{$type:10}}); //这样查询的是 address 列值为null 的数据

$type 有效的类型值,如下:

Type Number Alias Notes
Double 1 “double”
String 2 “string”
Object 3 “object”
Array 4 “array”
Binary data 5 “binData”
Undefined 6 “undefined” Deprecated.
ObjectId 7 “objectId”
Boolean 8 “bool”
Date 9 “date”
Null 10 “null”
Regular Expression 11 “regex”
DBPointer 12 “dbPointer” Deprecated.
JavaScript 13 “javascript”
Symbol 14 “symbol” Deprecated.
JavaScript (with scope) 15 “javascriptWithScope”
32-bit integer 16 “int”
Timestamp 17 “timestamp”
64-bit integer 18 “long”
Decimal128 19 “decimal” New in version 3.4.
Min key -1 “minKey”
Max key 127 “maxKey”

官网参考:https://docs.mongodb.com/manual/reference/operator/query/type/index.html

MongoDB 数据类型查询 — $type使用的更多相关文章

  1. 二 MongoDB数据类型和$type操作符

    一.MongoDB中可以使用的类型如下表所示 二.$type操作符 举个例子:想获取指定集合中title为String类型的所有文档

  2. Symfony中Doctrine对应的Mongodb数据类型 data type

    1. hash 就是 json对象 2. collection 就是 数组 3. 若要知道如何使用referenceOne, referenceMany, embbedDocument等 主要查看: ...

  3. MongoDB数据库的数据类型和$type操作符

    前面的话 本文将详细介绍MongoDB数据库的数据类型和$type操作符 数据类型 MongoDB支持以下数据类型 类型 数字 备注 Double 1 双精度浮点数 - 此类型用于存储浮点值 Stri ...

  4. Mongodb 数据类型及Mongoose常用CURD

    前言 看完了Node.js实战,其中在数据存储部分提到了Redis.Mongodb,我自己也根据书中的介绍写了几个简单的demo,在demo的过程首先遇到的问题就是数据类型和常见的CURD写法. mo ...

  5. MongoDB数据类型查询与修改

    MongoDB数据类型和对应的代码如下: MongoDB可以根据字段类型进行文档查询: 可以看到,friend集合的文档中,age字段有32位int类型的,也有double类型的.如果需要把doubl ...

  6. 类型转换bin()、chr()、ord() 、int()、float()、str()、repr()、bytes()、tuple(s )、 list(s )   、unichr(x ) 、 ord(x )  、 hex(x )  、          type()数据类型查询

    1.bin() 将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer: 参数x:整数或者包含__index__()方法切返回值 ...

  7. mongodb数据类型

    随着web2.0的时代到来,关系型数据库在越来越多的场景下暴漏出许多问题,为了解决这类问题,NoSql数据库应用而生,今天就来说说当下比较主流的NoSql数据库mongodb.   1. 基本数据类型 ...

  8. MongoDB 数据类型

    MongoDB支持许多数据类型的列表下面给出: String : 这是最常用的数据类型来存储数据.在MongoDB中的字符串必须是有效的UTF-8. Integer : 这种类型是用来存储一个数值.整 ...

  9. MongoDB (七) MongoDB 数据类型

    MongoDB支持许多数据类型的列表下面给出: String : 这是最常用的数据类型来存储数据.在MongoDB中的字符串必须是有效的UTF-8. Integer : 这种类型是用来存储一个数值.整 ...

随机推荐

  1. flask介绍

    安装flask pip3 install flask 短小精悍.可扩展强 的一个Web框架. 牛逼点:上下文管理机制 依赖wsgi: werkzeug(相当于Django的wsgi):只要安装flas ...

  2. js重名的处理

    ### 关于重名的处理 > 在变量提升阶段,如果名字重复了,不会重新的进行声明,但是会重新的进行定义(后面赋的值会把前面赋的值给替换掉) ```javascript //=>变量提升:fn ...

  3. leetcode Ch1-Search

    一. Binary Search int binarySearch(vector<int> &array, int target) { , hi = array.size() - ...

  4. winfrom 实现窗体圆角

    在窗体中加入一下代码 #region 窗体圆角的实现 private void ComFrmBase_Resize(object sender, EventArgs e) { if (this.Win ...

  5. winform ComboBox控件反选

    winform ComboBox控件反选:int index = comboBox1.FindString(textBox2.Text); comboBox1.SelectedIndex = inde ...

  6. C#:安装Windows服务,动态指定服务名及描述(转载)

    来源:http://www.cnblogs.com/Fooo/p/3476675.html Installer.cs public Installer() { InitializeComponent( ...

  7. Java学习---Collection的学习

    Collection基本方法的使用 package com.huawei.ftl; import java.io.File; import java.io.FileInputStream; impor ...

  8. My personal website:http://47.94.240.229:8080/yjh/project/

    My personal website:  http://47.94.240.229:8080/yjh/project/

  9. August 05th 2017 Week 31st Saturday

    All endings are beginnings, we just don't know it at the time. 所有的结局都是新的开始,只是当时我们不知道而已. Several mont ...

  10. day13 多线程建立方法

    #01创建多线程    继承Thread类    覆盖run方法:run方法里面运行要执行的代码    创建对象    调用start方法,start方法会开启线程,然后调用run方法 获取线程名字: ...