为什么 MongoDB 中的 $lt 在此查询中不起作用?
Why $lt in MongoDB doesn´t work in this query?
我正在为学习语言的人开发一个网络平台,我有一个集合供这样的用户使用:
{
"_id" : ObjectId("54a15e7e3453b5741a6c7be0"),
"name" : "Miguel Lopez",
"lng" : [
{
"ES" : 5
},
{
"EN" : 4
},
{
"IT" : 3
}
],
"email" : "miguel@gmail.com",
"password" : "123456"
}
在 "lng" 字段中,我保存了该用户正在学习的语言,每种语言的级别在 1 到 5 之间。要获取正在学习 "ES" 且级别为 5 的用户列表,我使用这个查询:
db.users.find({lng : {ES:5}})
它工作得很好,但是当我想要获取具有特定语言(例如 "EN")且级别低于 5 的用户时,我使用此查询:
db.users.find({lng : {EN:{$lt: 5}}})
但是这个查询不起作用,我不知道原因,当我尝试在 Robomongo 中执行这个查询时,我收到这条消息:"Script executed succesfully, but there is no results to show"
您需要使用$elemMatch
or dot notation
db.users.find({lng : {$elemMatch: {EN:{$lt: 5}}}})
db.users.find({ "lng.EN" : { "$lt" : 5 } })
我正在为学习语言的人开发一个网络平台,我有一个集合供这样的用户使用:
{
"_id" : ObjectId("54a15e7e3453b5741a6c7be0"),
"name" : "Miguel Lopez",
"lng" : [
{
"ES" : 5
},
{
"EN" : 4
},
{
"IT" : 3
}
],
"email" : "miguel@gmail.com",
"password" : "123456"
}
在 "lng" 字段中,我保存了该用户正在学习的语言,每种语言的级别在 1 到 5 之间。要获取正在学习 "ES" 且级别为 5 的用户列表,我使用这个查询:
db.users.find({lng : {ES:5}})
它工作得很好,但是当我想要获取具有特定语言(例如 "EN")且级别低于 5 的用户时,我使用此查询:
db.users.find({lng : {EN:{$lt: 5}}})
但是这个查询不起作用,我不知道原因,当我尝试在 Robomongo 中执行这个查询时,我收到这条消息:"Script executed succesfully, but there is no results to show"
您需要使用$elemMatch
or dot notation
db.users.find({lng : {$elemMatch: {EN:{$lt: 5}}}})
db.users.find({ "lng.EN" : { "$lt" : 5 } })