我需要选择具有3到9经验的< = 4员工,并且他们的工资总额应该小于或等于200000来自以下数据
{
"_id" : ObjectId("593a311650692f17327f9db3"),
"name" : "Anderson Heidenreich",
"experience" : 6,
"expected_salary" : 36252
}
{
"_id" : ObjectId("593a311650692f17327f9db4"),
"name" : "Dr. Collin Stanton IV",
"experience" : 8,
"expected_salary" : 56000
}
{
"_id" : ObjectId("593a311650692f17327f9db5"),
"name" : "Zita Von",
"experience" : 2,
"expected_salary" : 41792
}
{
"_id" : ObjectId("593a311650692f17327f9db6"),
"name" : "Gregory Reilly",
"experience" : 7,
"expected_salary" : 77000
}
{
"_id" : ObjectId("593a311650692f17327f9db7"),
"name" : "Prof. Myles Hackett",
"experience" : 3,
"expected_salary" : 49133
}
{
"_id" : ObjectId("593a311650692f17327f9db8"),
"name" : "Janelle Thiel",
"experience" : 4,
"expected_salary" : 78795
}
{
"_id" : ObjectId("593a311650692f17327f9db9"),
"name" : "Dr. Elsie Lang",
"experience" : 2,
"expected_salary" : 99135
}
{
"_id" : ObjectId("593a311650692f17327f9dba"),
"name" : "Sean Braun",
"experience" : 2,
"expected_salary" : 63608
}
{
"_id" : ObjectId("593a311650692f17327f9dbb"),
"name" : "Murray Simonis",
"experience" : 7,
"expected_salary" : 66000
}
预期输出如
{
"_id" : ObjectId("593a311650692f17327f9db4"),
"name" : "Dr. Collin Stanton IV",
"experience" : 8,
"expected_salary" : 56000
}
{
"_id" : ObjectId("593a311650692f17327f9db6"),
"name" : "Gregory Reilly",
"experience" : 7,
"expected_salary" : 7700
}
{
"_id" : ObjectId("593a311650692f17327f9dbb"),
"name" : "Murray Simonis",
"experience" : 7,
"expected_salary" : 66000
}
你能建议我怎么做mongodb查询吗?
谢谢
答案 0 :(得分:0)
我会使用经验范围进行简单的查找查询,然后按工资对结果进行排序,并将结果限制为仅为4。
db.stack.find( { "experience": {$gt: 3, $lt: 9} }).sort({"expected_salary" : -1 }).limit(4)
然后输出以下结果:
{ "_id" : ObjectId("593a311650692f17327f9db8"), "name" : "Janelle Thiel", "experience" : 4, "expected_salary" : 78795 }
{ "_id" : ObjectId("593a311650692f17327f9db6"), "name" : "Gregory Reilly", "experience" : 7, "expected_salary" : 77000 }
{ "_id" : ObjectId("593a311650692f17327f9dbb"), "name" : "Murray Simonis", "experience" : 7, "expected_salary" : 66000 }
{ "_id" : ObjectId("593a311650692f17327f9db4"), "name" : "Dr. Collin Stanton IV", "experience" : 8, "expected_salary" : 56000 }
现在在我们的客户端,我们可以循环查看结果,直到我们得到预期值
我们使用了一些mongodb查询运算符(https://docs.mongodb.com/manual/reference/operator/query/)
$gt:
- 大于
$lt:
- 小于