MongoDB query expression

Source: Internet
Author: User
Tags logical operators mongodb mongodb query

One, comparison operators

operator operators that correspond to MySQL
$gt >
$gte >=
$in In
$lt <
$lte <=
$ne !=
$nin Not in
$all None of the corresponding items, the index group all cell matching have, use the same $in, $nin
Syntax format:
{column name: {$gt: Value}}
{column name: {$ne: Value}}
{column name: {$in: [Value1,value2,value3 ...]}}  
Note:

two, logical operators

operator operators that correspond to MySQL
$or Or
$and and
$not Not
$nor No corresponding item, meaning "All listed conditions are not valid, true"
Syntax format:
{$and: [{columns: {$gte: value1}},{columns: {$gle: Value2}}]}

{$nor: [{columns: value1},{columns: value2},{columns: Value3} ...]}

three, element operator

name Description
$exists True if a column exists
$mod Satisfies a certain remainder condition to be true
$type Data is true for a type
Whether a column exists {column: {$exists: 1}}  #1表示存在
5 times times columns: {column: {$mod: [5,0]}}  #[5,0] represents a remainder of 5, with 0
type  {column: {$type: 2}}

type values from 1 to 19, and-1 and 127 corresponding data types check the official documentation:
https://docs.mongodb.com/manual/reference/operator/query/type/index.html

Four, JS operator (1)

name Description
$where The JS expression is true is true
$regex Regular expression matches are true
$where and $regex is to convert all the Bson first to JSON object, and then for the JS engine query, memory consumption is more
suitable for a small number of data queries, generally do not recommend the

syntax format:
{$where: ' this. Column > Value1 && this. Column < value2 ... '}  

{column: {$regex:/pattern/<options>}}
for example: {
name: {$regex:  /acme.*corp/i, $nin: [' Acmeblahcorp ']}}
{name: {$regex:/acme.*corp/, $options: ' I ', $nin: [' Acmeblahcorp ']}
{name: {$regex: ' Acme.*corp ', $options: ' I ', $nin: [' Acmeblahcorp ']}}

Five, JS operator (2)

name Description
$inc Growth
$rename renaming columns
$setOnInsert Set the value of a field when Upsert
$set Set a new value for a field
$unset Deletes the specified column
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.