简体   繁体   English

使用弹簧数据获取键值的mongodb查询

[英]mongodb query with spring data for key-value

I've been started a project with mongodb and spring boot and spring JPA data and I realised I cannot map my data model to entity and make a query on that easily, so I have two questions, 我已经启动了一个带有mongodb和spring boot以及spring JPA数据的项目,我意识到我无法将我的数据模型映射到实体并对其进行简单的查询,所以我有两个问题,

My data model is like that ( just for one Collection ) 我的数据模型就是这样(仅适用于一个集合)

{
   name: "Name",
   lastName: "Last Name",
   attributes: {
      age: 25
      eye: {
         color: "RED",
         size: "BIG"
      }
   }
}

And my entity is 我的实体是

@Entity // or @Document
public class User{
   private String name;
   private String lastName;
   private Map<String, ?> attributes = new HashMap<>();

   // id or the setter getter are omitted
}
  1. Can I map attributes property in my mongodb collection like I did ( Map ) 我可以像我一样映射我的mongodb集合中的属性属性(Map)
  2. How can I make query for finding the attributes? 如何查询查找属性?

    Can I do it like that? 我能这样做吗? List<User> findAllByAttributesAge(Integer age);

Can I map attributes property in my mongodb collection like I did ( Map ) 我可以像我一样映射我的mongodb集合中的属性属性(Map)

Yes, you can, and it may prove useful (though tricky) for a " dynamic " or " partly defined " schema. 是的,您可以,并且它可能对“ 动态 ”或“ 部分定义 ”架构有用(尽管很棘手)。

If you know your schema in advance, I strongly recommend that it shows in your domain object. 如果您事先知道自己的架构,我强烈建议您在域对象中显示它。 Take a look here . 看看这里

How can I make query for finding the attributes? 如何查询查找属性?

If you don't use a dynamic schema, property traversal for nested properties is clearly explained in the Spring Data MongoDB Reference Documentation . 如果不使用动态模式,则在Spring Data MongoDB参考文档中会清楚地解释嵌套属性的属性遍历。

If you use dynamic schema, you'll most probably use MongoDB JSON based query methods . 如果使用动态模式,则最有可能使用基于MongoDB JSON的查询方法

Today I had a problem with Map query in Spring Mongodb, and since this question pops the first one in google I will provide another answer. 今天我在Spring Mongodb中遇到了Map查询的问题,而且由于这个问题弹出谷歌的第一个,我将提供另一个答案。

Since the other answer references to documentation, and that documentation does not have a lot of information, I am going to put an example of how good is the @Query annotation for dynamic schema: 由于其他答案引用了文档,并且该文档没有很多信息,因此我将举例说明动态模式的@Query注释有多好:

@Query(value = "{'attributes.age' : ?0}")
List<User> findAllByAttributesAge(int age);

Also, you could query eye color, too: 此外,您还可以查询眼睛颜色:

@Query(value = "{'attributes.eye.color' : ?0}")
List<User> findAllByAttributesEyeColor(String color);

As the other answers documentation says, you could filter the result, and receive only the part of the document that you prefer: 正如文档所说的其他答案,您可以过滤结果,只接收您喜欢的文档部分:

// It will return the users with only name and last name
@Query(value = "{'attributes.age' : ?0}", fields = "{ name : 1, lastName : 1 }")
List<User> findAllByAttributesAge(int age);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM