Announcement Announcement Module
Collapse
No announcement yet.
MongoDB query : selecting fields Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MongoDB query : selecting fields

    Hi,

    through Spring mongoDB's support, how can I specify :
    Code:
    db.accounts.find( { }, { name: 1, status: 0 } );
    I saw, we can call
    Code:
    org.springframework.data.mongodb.core.query.Query(Criteria).fields().include("some_field")
    but the returned type will be Field not Query. So, not much of help with mongoTemplate.find(Query, ...)

    Any help welcome.

    Bardamu

  • #2
    Well, sorry. I'm quite tired...
    Doing the stuff in two phases :
    Query q = new Query(...);
    q.fields().include("field_name");
    mongoTemplate.find(q, ...);

    Comment


    • #3
      Are you using a Repostiory?

      If you are using the repository then you can follow the documentation here.
      http://static.springsource.org/sprin.../html/#d0e3293
      and do
      Code:
      public interface PersonRepository extends MongoRepository<Person, String>
      
        @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
        List<Person> findByThePersonsFirstname(String firstname);
      
      }

      Comment

      Working...
      X