MongoDB $lt 运算符
MongoDB $lt 运算符 介绍
MongoDB 提供了多种比较查询运算符。$lt(小于)运算符就是这些运算符之一。$lt 运算符用于选择字段值小于 (<) 给定值的文档。您可以根据您的要求在更新、查找等方法中使用此运算符。
MongoDB $lt 运算符 语法
{ field : { $lt : value } }
MongoDB $lt 运算符 例子
在以下示例中,我们正在使用:
Database: Yiidian
Collection: employee
Document: Six documents that contain the details of the employees.
>db.employee.find()
{
"_id" : 1,
"name" : "John",
"father_name" : "Thomas",
"department" : "Contain Writting",
"experience" : 2,
"mobile_no" : 9856321478,
"gender" : "Male",
"salary" : 22000,
"age" : 26
}
{
"_id" : 2,
"name" : "William",
"father_name" : "Rebort",
"department" : "Softwre Tester",
"experience" : 1,
"mobile_no" : 7896541478,
"gender" : "Male",
"salary" : 20000,
"age" : 21
}
{
"_id" : 3,
"name" : "Ava",
"father_name" : "William",
"department" : "Marketing manager",
"experience" : 5,
"mobile_no" : 8789654178,
"gender" : "Female",
"salary" : 36500,
"age" : 25
}
{
"_id" : 4,
"name" : "Olivia",
"father_name" : "Noah",
"department" : null,
"experience" : 4,
"mobile_no" : 9045641478,
"gender" : "Female",
"salary" : 30000,
"age" : 27
}
{
"_id" : 5,
"name" : "Elijah",
"father_name" : "John",
"department" : "HR",
"experience" : 0,
"mobile_no" : 6589741230,
"gender" : "Male",
"salary" : 15000,
"age" : 20
}
{
"_id" : 6,
"name" : "John",
"father_name" : "Liam",
"department" : "Softwre Tester",
"experience" : 10,
"mobile_no" : 9014536987,
"gender" : "Male",
"salary" : 55000,
"age" : 30
}
示例 1:使用 $lt 运算符
在此示例中,我们正在查找薪级低于 35000 的文档。
>db.employee.find({ salary : { $lt : 35000 }}).pretty()
输出结果为:
{
"_id" : 1,
"name" : "John",
"father_name" : "Thomas",
"department" : "Contain Writting",
"experience" : 2,
"mobile_no" : 9856321478,
"gender" : "Male",
"salary" : 22000,
"age" : 26
}
{
"_id" : 2,
"name" : "William",
"father_name" : "Rebort",
"department" : "Softwre Tester",
"experience" : 1,
"mobile_no" : 7896541478,
"gender" : "Male",
"salary" : 20000,
"age" : 21
}
{
"_id" : 4,
"name" : "Olivia",
"father_name" : "Noah",
"department" : null,
"experience" : 4,
"mobile_no" : 9045641478,
"gender" : "Female",
"salary" : 30000,
"age" : 27
}
{
"_id" : 5,
"name" : "Elijah",
"father_name" : "John",
"department" : "HR",
"experience" : 0,
"mobile_no" : 6589741230,
"gender" : "Male",
"salary" : 15000,
"age" : 20
}
示例 2:另一个字段
在此示例中,我们正在查找年龄小于 25 的文档。
>db.employee.find({ age : { $lt : 25 }}).pretty()
输出结果为:
{
"_id" : 2,
"name" : "William",
"father_name" : "Rebort",
"department" : "Softwre Tester",
"experience" : 1,
"mobile_no" : 7896541478,
"gender" : "Male",
"salary" : 20000,
"age" : 21
}
{
"_id" : 5,
"name" : "Elijah",
"father_name" : "John",
"department" : "HR",
"experience" : 0,
"mobile_no" : 6589741230,
"gender" : "Male",
"salary" : 15000,
"age" : 20
}
示例 3:使用 $lt 运算符更新值
在此示例中,我们将更新工作经验不足 3 年的员工的薪水字段。
>db.employee.update({experience : {$lt : 3}}, {$set : {salary : 25000}})
输出结果为:
{
"_id" : 1,
"name" : "John",
"father_name" : "Thomas",
"department" : "Contain Writting",
"experience" : 2,
"mobile_no" : 9856321478,
"gender" : "Male",
"salary" : 25000,
"age" : 26
}
{
"_id" : 2,
"name" : "William",
"father_name" : "Rebort",
"department" : "Softwre Tester",
"experience" : 1,
"mobile_no" : 7896541478,
"gender" : "Male",
"salary" : 25000,
"age" : 21
}
{
"_id" : 5,
"name" : "Elijah",
"father_name" : "John",
"department" : "HR",
"experience" : 0,
"mobile_no" : 6589741230,
"gender" : "Male",
"salary" : 25000,
"age" : 20
}
热门文章
优秀文章