如何迭代Map属性进行搜索

时间:2019-07-04 10:33:06

标签: mongodb rest spring-boot amazon-s3

嗨,我要根据属性“地图属性=新LinkedHashMap();中存在的属性”来获取人员数据。 使用查询mongoTemplate Creteria。这是我的代码 基于属性,我需要获取Documents实体类详细信息的任何键和值对。通过使用邮递员

这是我的POJO /实体类

@Document(collection = "document")
public class Documents {

    @Id
    private String _id;

    @Field(value = "username")
    private String username;

    @Field(value = "userrole")
    private String userrole;

@Field(value = "attributes")
    Map<String, Object> attributes = new LinkedHashMap<>();

    /**
     * @return the attributes
     */
    public Map<String, Object> getAttributes() {
        return attributes;
    }



    @JsonAnySetter
    public void setAttributes(String key, Object value) {


            this.attributes.put(key, value);


    }





this is my DAO/Repository interface

public interface Repository{
List<Documents> findByAttributes(Map<String, Object> attributes);
}



this is my service implementation class
@Autowired
    public DatabaseServiceImpl(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

@Override
    public List<Documents> findByAttributes(Map<String, Object> attributes) {

Query query = new Query();
query.addCriteria(Criteria.where("attributes").is(attributes));     
return mongoTemplate.find(query, Documents.class);
}
  and controller class

@requestMapping("/files")
 class controlle{

@GetMapping("/nr")
    public List<Documents> getByAttributes(@RequestParam Map<String, Object> attributes) {
        logger.info("get Attribute controller" + attributes);
        return dataService.findByAttributes(attributes);
    }

}

存储在mongodb中的以下值

    "_id": "5d1b2c3a774818444c9e7356",
    "documentTypeGroup": null,
    "attributes": {
        "name": "ayush",
        "vin": "hello",
        "sss": "1234",
        "appid": "1234abcd"

1 个答案:

答案 0 :(得分:0)

您只需遍历地图的键集并grep每个步骤中每个键的值即可。