How to find the list of active user based on the session in mongo db collection

I have a list of user IDs and I need to find the active users from the list in the session collection by passing the list of user IDs. The session collection looks like below: This is created automatically using the express js session handler.

{
    "_id" : "sXfI3vMxkZzsTkxuovUFONYA",
    "session" : "{\"cookie\":{\"originalMaxAge\":14399998,\"expires\":\"2015-03-28T17:24:18.996Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"},\"lastAccess\":1427549058996,\"dbname\":\"abc\",\"userid\":\"54f5bfb0336a15084785c379\"}",
    "expires" : 1427563458000
}
{
    "_id" : "sXfI3vMxkZzsTkxuovUFONYB",
    "session" : "{\"cookie\":{\"originalMaxAge\":14399998,\"expires\":\"2015-03-28T17:24:18.996Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"},\"lastAccess\":1427549058996,\"dbname\":\"abc\",\"userid\":\"54f5bfb0336a15084785c371\"}",
    "expires" : 1427563458000
}
{
    "_id" : "sXfI3vMxkZzsTkxuovUFONYC",
    "session" : "{\"cookie\":{\"originalMaxAge\":14399998,\"expires\":\"2015-03-28T17:24:18.996Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"},\"lastAccess\":1427549058996,\"dbname\":\"abc\",\"userid\":\"54f5bfb0336a15084785c370\"}",
    "expires" : 1427563458000
}

I have an user id list like ["54f5bfb0336a15084785c379","54f5bfb0336a15084785c371"] as "userList". I am querying like this:

db.sessions.find({"session.cookie.userid":{$in:userList}})`

and I am getting null as result.

I am able to get the result when querying like this:

db.sessions.find({"session":/54f5bfb0336a15084785c379/})

But I need to find the list of active users from the users list using node js mongodb client. Can anyone help me with this please?

First you should understand what is JSON. And as per your data mongo never find out session.cookie.userid because of session is key and after colon it's value of given key. So you should chage your JSON as below

    [{
    "_id": "sXfI3vMxkZzsTkxuovUFONYA",
    "session": {
        "cookie": {
            "originalMaxAge": 14399998,
            "expires": "2015-03-28T17:24:18.996Z",
            "secure": true,
            "httpOnly": true,
            "path": "/"
        },
        "lastAccess": 1427549058996,
        "dbname": "abc",
        "userid": "54f5bfb0336a15084785c379"
    },
    "expires": 1427563458000
    },
    {
    "_id": "sXfI3vMxkZzsTkxuovUFONYB",
    "session": {
        "cookie": {
            "originalMaxAge": 14399998,
            "expires": "2015-03-28T17:24:18.996Z",
            "secure": true,
            "httpOnly": true,
            "path": "/"
        },
        "lastAccess": 1427549058996,
        "dbname": "abc",
        "userid": "54f5bfb0336a15084785c371"
    },
    "expires": 1427563458000
    },
    {
    "_id": "sXfI3vMxkZzsTkxuovUFONYC",
    "session": {
        "cookie": {
            "originalMaxAge": 14399998,
            "expires": "2015-03-28T17:24:18.996Z",
            "secure": true,
            "httpOnly": true,
            "path": "/"
        },
        "lastAccess": 1427549058996,
        "dbname": "abc",
        "userid": "54f5bfb0336a15084785c370"
    },
    "expires": 1427563458000
    }]

If you insert above documents in your collections then simple used follwing query which will return results which you expected.

db.collectionName.find({"session.userid":
                     {"$in":["54f5bfb0336a15084785c379","54f5bfb0336a15084785c371"]}}
                     ).pretty()

I think you session object is wrong.Dont use black slash just befor double quote instead use single quote inside as you are using double quote outside "session" : "{'cookie':{'originalMaxAge':14399998,'expires':'2015-03-28T17:24:18.996Z','secure':true,'httpOnly':true,'path':'/\'},'lastAccess':1427549058996,'dbname':'abc','userid':'54f5bfb0336a15084785c379'}"