1

I have a basic structure like this:

> db.users.findOne()
{
    "_id" : ObjectId("4f384903cd087c6f720066d7"),
    "current_sign_in_at" : ISODate("2012-02-12T23:19:31Z"),
    "current_sign_in_ip" : "127.0.0.1",
    "email" : "something@gmail.com",
    "encrypted_password" : "$2a$10$fu9B3M/.Gmi8qe7pXtVCPu94mBVC.gn5DzmQXH.g5snHT4AJSZYCu",
    "last_sign_in_at" : ISODate("2012-02-12T23:19:31Z"),
    "last_sign_in_ip" : "127.0.0.1",
    "name" : "Trip Jameson",
    "sign_in_count" : 100,
    "usertimes" : [
        ...thousands and thousands of records like this one....
        {
            "enddate" : 348268392.115282,
            "idle" : 0,
            "startdate" : 348268382.116728,
            "title" : "My Awesome Title"
        },
    ]
}

So I want to find only usertimes for a single user where the title was "My Awesome Title", and then I want to see what the value for "idle" was in that record(s)

So far all I can figure out is that I can find the entire user record with a search like:

> db.users.find({'usertimes.title':"My Awesome Title"})

This just returns the entire User record though, which is useless for my purposes. Am I misunderstanding something?

Community
  • 1
  • 1
Jameson
  • 968
  • 1
  • 11
  • 24

2 Answers2

3

Return only partial embedded documents is currently not supported by MongoDB The matching User record will always be returned (at least with the current MongoDB version).

see this question for similar reference

Filtering embedded documents in MongoDB

This is the correspondent Jira on MongoDB space

http://jira.mongodb.org/browse/SERVER-142

Community
  • 1
  • 1
  • Well then I must be misunderstanding something. How would MongoDB ever be useful if we can't get values out of our embedded records? This array may contain millions of records for some users, I'm not going to put that in memory to parse. – Jameson Feb 13 '12 at 00:05
  • You could have a more traditional approach where you have a collection users and another `usertimes` and an field `user_id` where you have the ID of the user. It is the same design you would do apply with a RDMS. there is a new aggregation framework in 2.1 (http://www.mongodb.org/display/DOCS/Aggregation+Framework), it might partially answer to your need. –  Feb 13 '12 at 00:12
  • That makes sense. I guess the issue here is that I shouldn't actually be embedding this because it is known to be very large in most cases. I appreciate the feedback. – Jameson Feb 13 '12 at 00:15
  • One more thing: Do you know of any example schemas I could look at? I'm new to the whole NoSQL/MongoDB thing. – Jameson Feb 13 '12 at 00:16
  • In this case, the first question you should ask yourself is if MongoDB is fitting your needs. NoSQL is good for some projects, while RDMS is (still) good for others. A good starting point is surely the MongoDB documentation (very rich of example, and with a page on DB design http://www.mongodb.org/display/DOCS/Schema+Design), and this forum as well MongoDB mailing list are another good source of info. Finally, for the major languages there are dedicated libraries with specific features and documentation. –  Feb 13 '12 at 00:42
-1

Use:

db.users.find({'usertimes.title': "My Awesome Title"}, {'idle': 1});

May I suggest you take a more detailed look at http://www.mongodb.org/display/DOCS/Querying, it'll explain things for you.

twilson
  • 2,062
  • 14
  • 19
  • That just returns: { "_id" : ObjectId("4f384903cd087c6f720066d7") } The id of the User, still not giving me any info on the usertime's it's found – Jameson Feb 13 '12 at 00:02