MongoDB - Операторы сравнительных запросов
MongoDB использует различные операторы сравнительного запроса для сравнения значений документов. Следующая таблица содержит операторы сравнительного запроса:
Операторы | Описание |
---|---|
$ экв | Он используется для сопоставления значений полей, которые равны указанному значению. |
$ ne | Он используется для сопоставления всех значений поля, которые не равны указанному значению. |
$ gt | Он используется для сопоставления значений полей, превышающих указанное значение. |
$ gte | Он используется для сопоставления значений полей, которые больше указанного значения. |
$ lt | Он используется для сопоставления значений полей, которые меньше указанного значенияo |
$ lte | Он используется для сопоставления значений полей, которые меньше указанного значения |
$ в | Он используется для сопоставления любого из значений, указанных в массиве. |
$ девять | Он используется, чтобы не соответствовать ни одному из значений, указанных в массиве. |
В следующих примерах мы работаем с:
Database: GeeksforGeeks
Collection: contributor
Document: three documents that contain the details of the contributors in the form of field-value pairs.
Matching values using $nin
operator:
В этом примере мы получаем документы только тех сотрудников, имя которых не Амит или Суман.
db.contributor.find({name: {$nin: [ "Amit" , "Suman" ]}}).pretty() |
Matching values using $in
operator:
In this example, we are retrieving only those employee’s documents whose name is either Amit or Suman.
db.contributor.find({name: {$ in : [ "Amit" , "Suman" ]}}).pretty() |
Matching values using $lt
operator:
In this example, we are selecting those documents where the value of the salary field is less than 2000.
db.contributor.find({salary: {$lt: 2000 }}).pretty() |
Matching values using $eq
operator:
In this example, we are selecting those documents where the value of the branch field is equal to CSE.
db.contributor.find({branch: {$eq: "CSE" }}).pretty() |
Matching values using $ne
operator:
В этом примере мы выбираем те документы, в которых значение поля ветки не равно CSE.
db.contributor.find({branch: {$ne: "CSE" }}).pretty() |
Matching values using $gt
operator:
In this example, we are selecting those documents where the value of the salary field is greater than 1000.
db.contributor.find({salary: {$gt: 1000 }}).pretty() |
Matching values using $gte
operator:
In this example, we are selecting those documents where the value of the joiningYear field is greater than equals to 2017.
db.contributor.find({joiningYear: {$gte: 2017 }}) |
Matching values using $lte
operator:
In this example, we are selecting those documents where the value of the salary field is less than equals to 1000.
db.contributor.find({salary: {$lte: 1000 }}).pretty() |