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

975 views
Skip to first unread message

Soorya Prakash

unread,
Mar 28, 2015, 9:54:11 AM3/28/15
to mongod...@googlegroups.com
Hi,
 I am having the list of users id and i need ti find the active users from the list in session collecction by passing the list of user ids. The session collection lokks like 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
}

I am user id list like ["54f5bfb0336a15084785c379","54f5bfb0336a15084785c371"] as "userList".
I am querying like db.sessions.find({"session.cookie.userid":{$in:userList}}) and i am getting null as result.


When i query like below i am able to get the result.
db.sessions.find({"session":/54f5bfb0336a15084785c379/})


But i need to find the list of active users from the users list listing node js mongodb client. Can anyone help me in this please? 

Will Berkeley

unread,
Mar 30, 2015, 10:13:49 AM3/30/15
to mongod...@googlegroups.com
The value of the session key is a string representation of an embedded object, not an actual embedded objects - that's why an $in query for subfields of an embedded object

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

is not returning results, while a regex match

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

is returning results. It's essentially the same problem as storing the string "4" when you meant to store the object 4. How are you inserting or importing the data? You'll want to fix it so the sessions field gets a value that is an object, not a string.

-Will

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/88ecdcc9-7cb7-4570-9dd5-80ea9a20354a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Soorya Prakash

unread,
Mar 30, 2015, 10:20:12 AM3/30/15
to mongod...@googlegroups.com
Hi,
 I am using the express js session handler to store the session. I am inserting the userid while saving the session.Is there any way to achieve the query with existing db structure? or otherwise if i use the below  query as a iteration.
db.sessions.find({"session":/54f5bfb0336a15084785c379/}) like this. Then please tell me how i can i execute in node js .

You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/LxioC2pLUJc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.

To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

Will Berkeley

unread,
Mar 30, 2015, 12:59:21 PM3/30/15
to mongod...@googlegroups.com
Can you show us a bit of code to show how you're storing the session with express? I'm guessing you receive a json string and aren't parsing it before you store it the session field.

-Will

Soorya Prakash

unread,
Mar 31, 2015, 2:47:37 AM3/31/15
to mongod...@googlegroups.com
Hi,
function (req, res, next) {
        req.session.cookie.maxAge = 28 * 24 * hour; //4 weeks
   
      //Add the dbname in session for accessing the client database respectively
      req.session.dbname = dbname;

      //Add the session user id of inner company database for future api calls from inner datatabse
        req.session.userid = dbUser._id.toString();
         req.session.save(function (err) {
              if (err) {
                   res.send("Internal error saving session: " + err.message, 500);
              } else {
                  res.send(200);
              }
        });
}

like this when i doing the user login.
Reply all
Reply to author
Forward
0 new messages