在带有员工记录的MongoDB文档中仅显示具有特定薪水的员工姓名?

要仅显示具有特定薪水的员工姓名,请在MongoDB $in中设置薪水并获取姓名。让我们创建包含文档的集合

> db.demo666.insertOne({"EmployeeName":"John","EmployeeSalary":25000});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea1c04824113ea5458c7d0d")
}
> db.demo666.insertOne({"EmployeeName":"Chris","EmployeeSalary":35000});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea1c05524113ea5458c7d0e")
}
> db.demo666.insertOne({"EmployeeName":"David","EmployeeSalary":65000});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea1c06024113ea5458c7d0f")
}
> db.demo666.insertOne({"EmployeeName":"Carol","EmployeeSalary":40000});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea1c06f24113ea5458c7d10")
}

find()方法的帮助下显示集合中的所有文档-

> db.demo666.find();

这将产生以下输出-

{ "_id" : ObjectId("5ea1c04824113ea5458c7d0d"), "EmployeeName" : "John", "EmployeeSalary" : 25000 }
{ "_id" : ObjectId("5ea1c05524113ea5458c7d0e"), "EmployeeName" : "Chris", "EmployeeSalary" : 35000 }
{ "_id" : ObjectId("5ea1c06024113ea5458c7d0f"), "EmployeeName" : "David", "EmployeeSalary" : 65000 }
{ "_id" : ObjectId("5ea1c06f24113ea5458c7d10"), "EmployeeName" : "Carol", "EmployeeSalary" : 40000 }

以下是使用$in并获取具有特定薪水的员工姓名的查询-

> db.demo666.find({"EmployeeSalary":{$in:[35000,40000]}},{_id:0,"EmployeeSalary":0});

这将产生以下输出-

{ "EmployeeName" : "Chris" }
{ "EmployeeName" : "Carol" }