MongoDB 中的查询和选择运算符

0 股票
0
0
0
0

介绍

MongoDB 中的查询运算符包括比较运算符、逻辑运算符、元素运算符、求值运算符、地理运算符、数组运算符、位运算符和注释运算符。.

MongoDB 比较运算符

$eq

$eq 运算符指定一个相等条件。此运算符匹配字段值等于指定值的文档。.

句法:

{ : { $eq: } }

例子:

db.books.find({ price: { $eq: 300 } })

在这个例子中,查询会查看 Books 集合,选择 Price 字段值为 300 的所有文档。.

$gt

$gt 运算符选择字段值大于指定值的文档。.

句法:

{ field: { $gt: value } }

例子:

db.books.find({ price: { $gt: 200 } })
$gte

$gte 运算符选择字段值大于或等于指定值的文档。.

句法:

{ field: { $gte: value } }

例子:

db.books.find({ price: { $gte: 250 } })
1TP4锡

$in 运算符选择字段值等于指定数组中任何值的文档。.

句法:

{ field: { $in: [, , …] } }

例子:

db.books.find({ price: { $in: [100, 200] } })
$lt

$lt 运算符选择字段值小于指定值的文档。.

句法:

{ field: { $lt: value } }

例子:

db.books.find({ price: { $lt: 20 } })
$lte

$lte 运算符选择字段值小于或等于指定值的文档。.

句法:

{ field: { $lte: value } }

例子:

db.books.find({ price: { $lte: 250 } })
$ne

$ne 运算符选择字段值不等于指定值的文档。.

句法:

{ : { $ne: } }

例子:

db.books.find({ price: { $ne: 500 } })
$nin

$nin 运算符选择字段值不在指定数组中或根本不存在的文档。.

句法:

{ field: { $nin: [, , …] } }

例子:

db.books.find({ price: { $nin: [50, 150, 200] } })

MongoDB 逻辑运算符

$and

$and 运算符的作用类似于对数组进行逻辑与运算。数组必须包含一个或多个表达式,该运算符会选择满足数组中所有表达式的文档。.

句法:

{ $and: [ { }, { }, ....] }

例子:

db.books.find({ $and: [ { price: { $ne: 500 } }, { price: { $exists: true } } ] })
$not

$not 运算符对指定的表达式执行逻辑非运算符,并选择与相应表达式不匹配的文档。.

句法:

{ field: { $not: { } } }

例子:

db.books.find({ price: { $not: { $gt: 200 } } })
$nor

$nor 运算符对一个或多个查询词的数组执行逻辑 NOR 运算符操作,选择不满足数组中任何查询词的文档。.

句法:

{ $nor: [ { }, { }, ..... ] }

例子:

db.books.find({ $nor: [ { price: 200 }, { sale: true } ] })
$or

<pعملگر $or به عنوان یک عملگر منطقی OR روی یک آرایه از دو یا چند عبارت عمل می‌کند و اسنادی را انتخاب می‌کند که حداقل یکی از عبارات را برآورده کنند.

句法:

{ $or: [ { }, { }, ... , { } ] }

例子:

db.books.find({ $or: [ { quantity: { $lt: 200 } }, { price: 500 } ] })

MongoDB 元素运算符

$exists

$exists 运算符用于选择当布尔值为真时包含指定字段的文档。它也用于选择当布尔值为空时包含该字段的文档。.

句法:

{ field: { $exists: } }

例子:

db.books.find({ qty: { $exists: true, $nin: [5, 15] } })
$型

$type 运算符选择字段值指定为 BSON 类型的文档。.

句法:

{ field: { $type: } }

例子:

db.books.find({ "bookid": { $type: 2 } })

MongoDB 评估运算符

$expr

$expr 运算符允许在查询语言中使用聚合表达式。.

句法:

{ $expr: { } }

例子:

db.store.find({ $expr: { $gt: [ "$product", "price" ] } })
$jsonSchema

此运算符选择与指定 JSON 模式匹配的文档。.

句法:

{ $jsonSchema: }
$mod

$mod 运算符选择字段值除以除数后余数为指定值的文档。.

句法:

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

例子:

db.books.find({ qty: { $mod: [ 200, 0 ] } })
$regex

此运算符提供正则表达式功能,用于在查询中匹配字符串模式。MongoDB 使用与 Perl 兼容的正则表达式。.

句法:

{ : /pattern/ }

例子:

db.books.find({ price: { $regex: /789$/ } })
$文本

$text 运算符搜索已建立文本索引的字段的内容。.

句法:

{ $text: { $search: , $language: , $caseSensitive: , $diacriticSensitive: } }

例子:

db.books.find({ $text: { $search: "Othelo" } })
$where

“where”运算符用于将包含 JavaScript 表达式或完整 JavaScript 函数的字符串传递给查询引擎。.

例子:

db.books.find({ $where: function() { return (hex_md5(this.name) == "9b53e667f30cd329dca1ec9e6a8") } })
发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

您可能也喜欢