MongoDB $gt 运算符

MongoDB $gt 运算符 介绍

MongoDB 提供了多种比较查询运算符。$gt(大于)运算符就是这些运算符之一。$gt 运算符用于选择字段值大于给定值的文档。您可以根据需要在 (update, find, like) 方法中使用此运算符。

MongoDB $gt 运算符  语法

{ field : { $gt : value } }  

MongoDB $gt 运算符 例子

在以下示例中,我们正在使用:

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:使用 $gt 运算符

在此示例中,我们正在查找薪级大于 20000 的文档。

>db.employee.find({ salary : { $gt : 20000 }}).pretty()  

输出结果为:

{
    "_id" : 1,
    "name" : "John",
    "father_name" : "Thomas",
    "department" : "Contain Writting",
    "experience" : 2,
    "mobile_no" : 9856321478,
    "gender" : "Male",
    "salary" : 22000,
    "age" : 26
}
{
    "_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" : 6,
    "name" : "John",
    "father_name" : "Liam",
    "department" : "Softwre Tester",
    "experience" : 10,
    "mobile_no" : 9014536987,
    "gender" : "Male",
    "salary" : 55000,
    "age" : 30 
}

示例 2:另一个字段

在此示例中,我们正在查找年龄大于 25 的文档。

>db.employee.find({ age : { $gt : 25 }}).pretty()  

输出结果为:

{
    "_id" : 1,
    "name" : "John",
    "father_name" : "Thomas",
    "department" : "Contain Writting",
    "experience" : 2,
    "mobile_no" : 9856321478,
    "gender" : "Male",
    "salary" : 22000,
    "age" : 26
}
{
    "_id" : 4,
    "name" : "Olivia",
    "father_name" : "Noah",
    "department" : null,
    "experience" : 4,
    "mobile_no" : 9045641478,
    "gender" : "Female",
    "salary" : 30000,
    "age" : 27 
}
{
    "_id" : 6,
    "name" : "John",
    "father_name" : "Liam",
    "department" : "Softwre Tester",
    "experience" : 10,
    "mobile_no" : 9014536987,
    "gender" : "Male",
    "salary" : 55000,
    "age" : 30 
}

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

在此示例中,我们正在更新具有 3 年以上经验的员工的薪水字段。

>db.employee.update({experience: {$gt: 3}}, {$set: {salary: 50000}})  

输出结果为:

{
    "_id" : 3,
    "name" : "Ava",
    "father_name" : "William",
    "department" : "Marketing manager",
    "experience" : 5,
    "mobile_no" : 8789654178,
    "gender" : "Female",
    "salary" : 50000,
    "age" : 25 
}
{
    "_id" : 4,
    "name" : "Olivia",
    "father_name" : "Noah",
    "department" : null,
    "experience" : 4,
    "mobile_no" : 9045641478,
    "gender" : "Female",
    "salary" : 50000,
    "age" : 27 
}
{
    "_id" : 6,
    "name" : "John",
    "father_name" : "Liam",
    "department" : "Softwre Tester",
    "experience" : 10,
    "mobile_no" : 9014536987,
    "gender" : "Male",
    "salary" : 50000,
    "age" : 30 
}

 

热门文章

优秀文章