Results 1 to 4 of 4

Thread: How do I retrieve only a subset of data from Mongo?

  1. #1
    Join Date
    Jul 2008
    Posts
    116

    Default How do I retrieve only a subset of data from Mongo?

    Let's say I want to retrieve a list of users from mongo, but I want to make the query faster by only returning the user data I will be working with.

    E.g. autoCompleteUsername(username)

    Result:
    List<User>
    Where only user.id and user.username are populated.

    How would I do this?

    In mongo it would look something like this:
    db.testdb.user.find({"username" : /^johnsmi*/i}, {username:1}).skip(0).limit(30)

    Cheers
    bjorn
    Last edited by bjornharvold; Jul 15th, 2011 at 04:30 PM.

  2. #2
    Join Date
    Jul 2008
    Posts
    116

    Default

    Any resolution on this. I still haven't been able to figure this out.

    Thanks
    bjorn

  3. #3
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    The Query class has a fields() method which returns a Fields object that you can programmatically set the fields to include or exclude. If you're using the repository abstraction there's an @Query annotation having a fields attribute, that takes a JSON string just as you posted above which can be equiped with placeholders (e.g. ?0) to reference method parameters.

  4. #4
    Join Date
    Jul 2008
    Posts
    116

    Default

    Thanks Oliver!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •