MongoDB $lte操作符

MongoDB $lte操作符

MongoDB中的$lte操作符是什么

MongoDB提供了各种比较查询操作符。$lte(小于等于)操作符是其中之一。$lte操作符用于选择字段的值小于或等于(即 <=)给定值的文档。根据您的需求,您可以在更新、查找或类似方法中使用此操作符。

$lte操作符的语法

{ field : { $lte : value } }

示例

以下是我们要使用的示例:

Database: JavaTpoint
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:使用$lte操作符

在这个示范中,我们正在寻找工资规模等于或小于30000的文档。

>db.employee.find({ salary : { $lte : 30000 }}).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:另一个字段

在这个示例中,我们要找到年龄小于或等于26的文件。

>db.employee.find({ age : { $lte : 26 }}).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" : 3,
    "name" : "Ava",
    "father_name" : "William",
    "department" : "Marketing manager",
    "experience" : 5,
    "mobile_no" : 8789654178,
    "gender" : "Female",
    "salary" : 36500,
    "age" : 25 
}
{
    "_id" : 5,
    "name" : "Elijah",
    "father_name" : "John",
    "department" : "HR",
    "experience" : 0,
    "mobile_no" : 6589741230,
    "gender" : "Male",
    "salary" : 15000,
    "age" : 20
}

示例3:使用$lte运算符更新值

在这个示例中,我们正在更新工作经验少于或等于3年的员工的薪水字段。

>db.employee.update({experience : {lte : 3}}, {set : {salary : 20000}})

输出:

{
    "_id" : 1,
    "name" : "John",
    "father_name" : "Thomas",
    "department" : "Contain Writting",
    "experience" : 2,
    "mobile_no" : 9856321478,
    "gender" : "Male",
    "salary" : 20000,
    "age" : 26
}
{
    "_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" : 20000,
    "age" : 20
}

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程