Miguel Haba
Miguel Haba

Reputation: 57

Why $lt in MongoDB doesn´t work in this query?

I am developing a web platform for people who is learning languages, I have a collection for users like that:

{
    "_id" : ObjectId("54a15e7e3453b5741a6c7be0"),
    "name" : "Miguel Lopez",
    "lng" : [ 
        {
            "ES" : 5
        }, 
        {
            "EN" : 4
        }, 
        {
            "IT" : 3
        }
    ],
    "email" : "[email protected]",
    "password" : "123456"
}

In "lng" field I save the languages that this user is learning, each language have a level between 1 and 5. To get the list of users who are learning "ES" and have level 5, I use this query:

db.users.find({lng : {ES:5}})

And it´s working perfectly, but when I want the get users who have a specific language (for example "EN") with a level less than 5, I use this query:

db.users.find({lng : {EN:{$lt: 5}}})

But this query doesn´t work, and I dont know the reason, when I try to execute this query in Robomongo I get this message: "Script executed succesfully, but there is no results to show"

Upvotes: 1

Views: 207

Answers (1)

Sede
Sede

Reputation: 61225

You need to use $elemMatch or dot notation

db.users.find({lng : {$elemMatch: {EN:{$lt: 5}}}})
db.users.find({ "lng.EN" : { "$lt" : 5 } })

Upvotes: 1

Related Questions