0

I am new to writing MongoDb queries to fetch data. I am trying to write a nested query to fetch user data from the sessions collections.

Basically I am trying to get db.sessions.passport.user.email. But I am unable to get the required value which is vdipali8@gmail.com.

Query that I am using is as follows:

db.sessions.find({"session.passport.user.email":"vdipali8@gmail" });

Following is my sessions collection:

db.sessions.find().pretty()

{
        "_id" : "xi8sMgfYywLJdoZPpUHKa3Uau3wY",
        "session" : "{\"cookie\":{\"originalMaxAge\":null,\"expires\":null,\"httpOnly\":true,\"path\":\"/\"},\"passport\":{\"user\":{\"_id\":\"5750ec592ecb2c0bfb5d39\",\"email\":\"vdipali8@gmail.com\",\"lastName\":\"Vl\",\"firstName\":\"Dipali\",\"facebookid\":\"13596511\",\"__v\":0}}}",
        "expires" : ISODate("2016-06-17T02:47:45.042Z")
}

The sessions collection is generated as follows in my express Node server file:

app.use(session({ secret: 'keyboard cat', 
    store: new MongoStore({ 
            mongooseConnection: mongoose.connection,
            collection: 'sessions'
        }) 
}));

and the schema for the same is declared as follows:

var SessionSchema = new Schema({
        session: String,
        expires: String
        }, {collection: 'sessions'}
    );

Yes, I am trying to retrieve the "email" along with few other fields.

Dipali
  • 65
  • 1
  • 1
  • 6
  • Have you tried by breaking the find query? Like `db.sessions.find({"session.passport})` or `db.sessions.find({"session.passport.user})`. See if you are getting results for these queries. – Shrabanee Jun 03 '16 at 04:32
  • Possibility of duplicate http://stackoverflow.com/questions/10885044/mongodb-how-to-query-for-a-nested-item-inside-a-collection – Harshad Jun 03 '16 at 04:37
  • @titi23: I tried it but I do not see any results. Only result I see when I query for "_id" : "xi8sMgfYywLJdoZPpUHKa3Uau3wY". – Dipali Jun 03 '16 at 04:49
  • Where is the string coming from? Is "session" field value the same format for all your documents? Do you want to retrieve the "email" for all your documents? Please use the [edit link](https://stackoverflow.com/posts/37605365/edit) to update your question with those informations – styvane Jun 03 '16 at 05:16

1 Answers1

0

its not possible , you query is correct but session field is STRING so you cant use dot Operator in mongodb in STRING field. pls put object in session field then it will work.

like this

{
    "_id": "xi8sMgfYywLJdoZPpUHKa3Uau3wY",
    "session": {
        "cookie": {
            "originalMaxAge": null,
            "expires": null,
            "httpOnly": true,
            "path": ""
        },
        "passport": {
            "user": {
                "_id": "5750ec592ecb2c0bfb5d39",
                "email": "vdipali8@gmail.com",
                "lastName": "Vl",
                "firstName": "Dipali",
                "facebookid": "13596511",
                "__v": 0
            }
        }
    },
    "expires": ISODate("2016-06-17T02:47:45.042Z")

}

karthi
  • 880
  • 6
  • 10
  • thanks for your answer. I am new in writing queries for Mongodb. I am not quite sure what you mean by "put object in session field" ? – Dipali Jun 03 '16 at 05:01
  • i changed answer see above – karthi Jun 03 '16 at 05:25
  • the following query is giving me the result: db.sessions.find({"session":/passport/}); – Dipali Jun 03 '16 at 05:48
  • i have edited my comment above. But I need to write a query which retrieve's just the email id value and not the entire document. – Dipali Jun 03 '16 at 05:51