Convert MongoDB query into Spring MongoDB syntax

前端 未结 2 1534
春和景丽
春和景丽 2021-01-27 18:06

Hello I am unable to convert the following mongoDB query into spring query, i have tried multiple ways, but did not get the result.

db.getCollection(\'FarmerCrop         


        
2条回答
  •  萌比男神i
    2021-01-27 18:20

    If you've used JpaRepository then it's easy to relate that you can create an interface and extends MongoRepository just like with JpaRepository and it provides some simple method, and you don't need the implement it.

    you can use (for example consider A Person with First and Last name)

    MongoDB JSON based query methods and field restriction

    public interface PersonRepository extends MongoRepository
    
    @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
    List findByThePersonsFirstname(String firstname);
    }
    

    Geo-spatial repository queries

    public interface PersonRepository extends MongoRepository
    
      // { 'location' : { '$near' : [point.x, point.y], '$maxDistance' : distance}}
      List findByLocationNear(Point location, Distance distance);
    }
    

    Read the Spring document here for MongoDB repositories

提交回复
热议问题