首页 > 数据库 >MongoDB学习笔记三(查询)

MongoDB学习笔记三(查询)

时间:2023-02-17 15:35:18浏览次数:37  
标签:sku description MongoDB db 笔记 查询 line qty id

一、比较查询运算符

 $eq 相等

 ①等于指定值

 等价于:

 查询结果:

 ②嵌入式文档中的字段等于一个值

 查询结果:

 ③数组元素等于一个值

执行结果:

 ④等于数组值

 

 执行结果:

 

$gt大于

 执行结果:

 $gte大于等于

 执行结果:

 $lt、$lte同理。

$in 在...中

 使用$in运算符匹配值

 执行结果:

 使用$in运算符匹配数组中的值

 执行结果:

 $ne不等于,用法等同于$in

$nin 不在...中,用法等同于$in

 

二、逻辑查询运算符

$and

语法:{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

$and 对一个或多个表达式(、等)AND的数组 执行逻辑运算,并选择满足所有表达式的文档。

示例:

也可隐式的如下操作:

 

 

$not

语法:{ field: { $not: { <operator-expression> } } }

$nor

语法:{ $nor: [ { <expression1> }, { <expression2> }, ...  { <expressionN> } ] }

$nor对一个或多个表达式的数组执行逻辑运算。

 

 

$or

语法:{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

$or对一个或多个表达式的数组执行逻辑运算。

 

三、元素查询运算符

$exists

语法:{ field: { $exists: <boolean> } }

<boolean>是真的,$exists匹配包含该字段的文档,包括字段值为 的文档 null。如果<boolean>为 false,则查询仅返回不包含该字段的文档

{ a: 5, b: 5, c: null }

{ a: 3, b: null, c: 8 }

{ a: null, b: 3, c: 9 }

{ a: 1, b: 2, c: 3 }

{ a: 2, c: 5 }

{ a: 3, b: 2 }

{ a: 4 }

{ b: 2, c: 4 }

{ b: 2 }

{ c: 6 }

db.records.find( { a: { $exists: true } } )

执行结果:

{ a: 5, b: 5, c: null }

{ a: 3, b: null, c: 8 }

{ a: null, b: 3, c: 9 }

{ a: 1, b: 2, c: 3 }

{ a: 2, c: 5 }

{ a: 3, b: 2 }

{ a: 4 }

db.records.find( { a: { $exists: flase } } )

执行结果:

{ a: 2, c: 5 }

{ a: 4 }

{ c: 6 }

 

四、计算查询

$expr

语法:{ $expr: { <expression> } }

可以构建查询表达式,可以使用let变量比较字段。

示例1:

{ "_id" : 1, "category" : "food", "budget": 400, "spent": 450 }

{ "_id" : 2, "category" : "drinks", "budget": 100, "spent": 150 }

{ "_id" : 3, "category" : "clothes", "budget": 100, "spent": 50 }

{ "_id" : 4, "category" : "misc", "budget": 500, "spent": 300 }

{ "_id" : 5, "category" : "travel", "budget": 200, "spent": 650 }

查找金额spent超过的文件budget:

db.monthlyBudget.find( { $expr: { $gt: [ "$spent" , "$budget" ] } } )

返回结果:

{ "_id" : 1, "category" : "food", "budget" : 400, "spent" : 450 }

{ "_id" : 2, "category" : "drinks", "budget" : 100, "spent" : 150 }

{ "_id" : 5, "category" : "travel", "budget" : 200, "spent" : 650 }

示例2:

db.supplies.insertMany([

   { "_id" : 1, "item" : "binder", "qty" : NumberInt("100"), "price" : NumberDecimal("12") },

   { "_id" : 2, "item" : "notebook", "qty" : NumberInt("200"), "price" : NumberDecimal("8") },

   { "_id" : 3, "item" : "pencil", "qty" : NumberInt("50"), "price" : NumberDecimal("6") },

   { "_id" : 4, "item" : "eraser", "qty" : NumberInt("150"), "price" : NumberDecimal("3") },

   { "_id" : 5, "item" : "legal pad", "qty" : NumberInt("42"), "price" : NumberDecimal("10") }

]) 

要求:假设对于下个月即将进行的促销活动,您想要打折的价格是:

如果qty大于或等于 100,则折扣价为 的 0.5 price。

如果qty小于 100,则折扣价为 的 0.75 price。

在应用折扣之前,您想知道 supplies集合中哪些商品的折扣价低于5。 

let discountedPrice = {

   $cond: {

      if: { $gte: ["$qty", 100] },

      then: { $multiply: ["$price", NumberDecimal("0.50")] },

      else: { $multiply: ["$price", NumberDecimal("0.75")] }

   }

};

db.supplies.find( { $expr: { $lt:[ discountedPrice,  NumberDecimal("5") ] } });

执行结果:

{ "_id" : 2, "item" : "notebook", "qty": 200 , "price": NumberDecimal("8") }

{ "_id" : 3, "item" : "pencil", "qty": 50 , "price": NumberDecimal("6") }

{ "_id" : 4, "item" : "eraser", "qty": 150 , "price": NumberDecimal("3") }

$mod 取余

语法:{ field: { $mod: [ divisor, remainder ] } }

示例:

db.inventory.insertMany( [

   { "_id" : 1, "item" : "abc123", "qty" : 0 },

   { "_id" : 2, "item" : "xyz123", "qty" : 5 },

   { "_id" : 3, "item" : "ijk123", "qty" : 12 }

] )

db.inventory.find({qty:{$mod:{4,0}}})

执行结果:

   { "_id" : 1, "item" : "abc123", "qty" : 0 },

   { "_id" : 3, "item" : "ijk123", "qty" : 12 }

注:语法中的division如果是小数,如4.4, 4.5, 4.9,取余时直接除数直接按照4进行取余

即:

db.inventory.find({qty:{$mod:{4.99,0}}})

执行结果:

   { "_id" : 1, "item" : "abc123", "qty" : 0 },

   { "_id" : 3, "item" : "ijk123", "qty" : 12 }

$regex  正则表达式

语法:

{ <field>: { $regex: /pattern/, $options: '<options>' } }

{ <field>: { $regex: 'pattern', $options: '<options>' } }

{ <field>: { $regex: /pattern/<options> } }

options:

i 不区分大小匹配

m 对于包含锚点(即^开始、 $结束)的模式,在每行的开头或结尾匹配具有多行值的字符串。如果没有此选项,这些锚点将在字符串的开头或结尾匹配

x 忽略所有空白字符,包括未转义的井号/井号 ( #) 字符和下一个换行符

s 允许点字符(即.)匹配所有字符,包括换行符

示例:

db.products.insertMany( [

   { _id: 100, sku: "abc123", description: "Single line description." },

   { _id: 101, sku: "abc789", description: "First line\nSecond line" },

   { _id: 102, sku: "xyz456", description: "Many spaces before     line" },

   { _id: 103, sku: "xyz789", description: "Multiple\nline description" },

   { _id: 104, sku: "Abc789", description: "SKU starts with A" }

] )

^ 以...开头

$ 以...结尾

①进行like匹配

db.products.find( { sku: { $regex: /789$/ } } )

类似于sql like语句,如 where sku like '%789'

执行结果:

[

   { _id: 101, sku: 'abc789', description: 'First line\nSecond line' },

   { _id: 103, sku: 'xyz789', description: 'Multiple\nline description' },

   { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }

]

②执行不区分大小写的正则表达式匹配

db.products.find( { sku: { $regex: /^ABC/i } } )

执行结果:

[

   { _id: 100, sku: 'abc123', description: 'Single line description.' },

   { _id: 101, sku: 'abc789', description: 'First line\nSecond line' },

   { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }

]

③以指定模式开始的多行匹配

db.products.find( { description: { $regex: /^S/, $options: 'm' } } )

执行结果:

[

   { _id: 100, sku: 'abc123', description: 'Single line description.' },

   { _id: 101, sku: 'abc789', description: 'First line\nSecond line' },

   { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }

]

如果没有m选项

db.products.find( { description: { $regex: /^S/} } )

执行结果:

[

   { _id: 100, sku: 'abc123', description: 'Single line description.' },

   { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }

]

db.products.find( { description: { $regex: /S/ } } )

执行结果:

[

   { _id: 100, sku: 'abc123', description: 'Single line description.' },

   { _id: 101, sku: 'abc789', description: 'First line\nSecond line' },

   { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }

]

④使用.点字符匹配新行

以下示例使用s允许点字符(即.)匹配所有字符(包括换行符)的选项以及 i执行不区分大小写的匹配的选项:

db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } )

执行结果: 

[

   { _id: 102, sku: 'xyz456', description: 'Many spaces before     line' },

   { _id: 103, sku: 'xyz789', description: 'Multiple\nline description' }

]

如果没有s选项,执行结果:

[

   { _id: 102, sku: 'xyz456', description: 'Many spaces before     line' }

]

⑤忽略模式中的空白

以下示例使用x忽略空格和注释的选项,在匹配模式中以#和 结尾:\n

var pattern = "abc #category code\n123 #item number"

db.products.find( { sku: { $regex: pattern, $options: "x" } } )

执行结果:

[

   { _id: 100, sku: 'abc123', description: 'Single line description.' }

]

⑥使用正则表达式匹配字符串中的大小写

db.products.find( { sku: { $regex: "(?i)a(?-i)bc" } } )

执行结果:

[

   { _id: 100, sku: 'abc123', description: 'Single line description.' },

   { _id: 101, sku: 'abc789', description: 'First line\nSecond line' },

   { _id: 104, sku: 'Abc789', description: 'SKU starts with A' }

]

标签:sku,description,MongoDB,db,笔记,查询,line,qty,id
From: https://www.cnblogs.com/plzh/p/17130137.html

相关文章

  • 【IMX6ULL学习笔记】九、Linux内核移植
    一、在Linux中添加自己的开发板1、添加开发板默认配置文件将arch/arm/configs目录下的imx_v7_mfg_defconfig重新复制一份,命名为imx_kodo_emmc_defconfig,命令如......
  • 组合计数课程笔记(二):组合计数
    组合计数问题是组合数学中重要的最古典的分支。有人将组合计数问题归为\(12\)个集合映射问题。但是其中有\(2\)个是平凡的,所以我们只研究\(10\)个。十二重计数法在......
  • 【IMX6ULL学习笔记】四、 U-BOOT启动流程
    一、链接脚本u-boot.lds详解要分析uboot的启动流程,首先要找到“入口”,找到第一行程序在哪里。程序的链接是由链接脚本来决定的,所以通过链接脚本可以找到程序的入口。......
  • 【IMX6ULL学习笔记】五、U-BOOT移植与解析
    一、移植自定义开发板流程1、添加mx6ull_kodo_emmc_defconfig配置文件(.config)在/configs目录下,复制mx6ull_14x14_evk_emmc_defconfig文件,重命名为mx6ull_kodo_emm......
  • 基于sysbench-mongodb-lua的mongodb的性能测试
    1.环境准备installsysbenchyuminstallsysbenchinstallmongoroverdriver···yuminstalllibmongoc-devlibbson-devluarocksluarocksinstallmongorover......
  • mysql:分组查询每组最新的一条数据
    我们经常遇到类似这样的需求,查询最近N秒、N分钟、N小时的数据及N天的数据,相关的方法和函数很多,本人最近用的MySQL数据库,也就用MySQL为例,大概介绍几种比较通用的方法。一、......
  • 组合数学课程笔记(一):框架构建
    组合数学的严格定义是非常困难的,其设计的内容广泛,分类困难,体系性较弱。不过,我们可以把组合数学按照问题、工具、对象三种方法进行分类,例如图论,就是按照研究对象分出的内容......
  • JAVA 学习笔记(五)
      子类通过方法的重写机制可以隐藏继承父类的方法,把父类的状态和行为改变为子类自己的状态和行为.假如父类中有一个方法myMethod(),一旦子类重写了超类的方法myMethod......
  • 多项式相关算法学习笔记(持续更新)
    第一次用博客园写学习笔记,写的不好请见谅~欢迎各位OIer在评论区说一下自己对这篇博客的建议!有关多项式的基本概念对于求和式\(\suma_nx^n\),如果是有限项相加,称为多项......
  • dp学习笔记
    目录斜率优化dpH.仓库建设思路代码J.土地购买思路:代码斜率优化dpH.仓库建设思路很容易想暴力,因为只能往后送物资,从后往前计算dp[i]为在i这里建造仓库且i~n都有地可去......