ArangoDB ArangoRepository扩展类中的方法在查询注解中使用COLLECT来分组,但计数不起作用

8gsdolmq  于 2022-12-09  发布在  Go
关注(0)|答案(1)|浏览(167)

我有一个简单的节点,如下所示

@Document("users")
public class User {
  @Id // db document field: _key
  private String id;

  @ArangoId // db document field: _id
  private String arangoId;

  private String firstName;
  private String lastName;
  private String country;

  public User() {
    super();
  }

  public User(String id) {
    this.id = id;
  }

  public User(String id, String country) {
    this.id = id;
    this.country = country;
  }

  // getter & setter

  @Override
  public String toString() {
    return "User [id=" + id + ", name=" + firstName + ", surname=" + lastName + "]";
  }

  public String getId() {
    return id;
  }
}

这是一个存储库类,但是方法getListOfCountryAndNumUsers返回null,即使我已经将不同国家的用户插入到数据库中。

public interface UserRepository extends ArangoRepository<User, String> {

@Query("FOR u IN users COLLECT country = u.country WITH COUNT INTO length RETURN 
  {\"country\" : country, \"count\" : length }")
  Iterable<CountryAndNumUsers> getListOfCountryAndNumUsers();

}

我认为问题可能出在查询注解中的查询语法上。我在arangodb文档here的spring data arango db部分中没有看到任何使用collect操作的直接示例,但我在arangoDb文档here的“高级操作”部分中看到了collect操作
请帮帮忙。谢谢!

3bygqnnd

3bygqnnd1#

所以我发现了我的错误。它在一个我没有在问题中添加的类中。那是方法getListOfCountryAndNumUsers()的返回对象的类,即类CountryAndNumUsers。

public class CountryAndNumUsers {
  private String country;
  private Integer numberOfUsers;

  public CountryAndNumUsers(String country, Integer numberOfUsers) {
    this.country = country;
    this.numberOfUsers = numberOfUsers;
  }

  public String getCountry() {
    return country;
  }

  public Integer getNumberOfUsers() {
    return numberOfUsers;
  }
}

因此存在Map不匹配,因为查询返回的对象具有不同的字段名。

@Query("FOR u IN users COLLECT country = u.country WITH COUNT INTO length RETURN {\"country\" : country, \"numberOfUsers\" : length }")

相关问题