如何在没有模型类的情况下通过 MongoTemplate 获取 MongoDB 对象

Big*_*ev. 5 java mongodb mongotemplate

我正在尝试获取 Mongo 集合对象。我不想使用模型,因为我的字段在 Mongo 集合中没有固定。

这里 mycustomer_ref_id是固定字段,其他字段不是。可能会添加或删除字段。

如何访问$ref引用$id其他集合的字段?

{
  "_id" : "SQ74P",
  "_class" : "com.vo.License",
  "vendor" : "te",
  "product" : "ty",
  "product_id" : "7.4",
  "version" : "17.0",
  "is_deleted" : false,
  "customer_ref" : {
    "$ref" : "customer",
    "$id" : "IG_7.4"
  }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过这个:

控制器:

@RestController
@EnableAutoConfiguration
@RequestMapping("/license/demo")
public class DemoLiController {

  @Autowired
  DemoLicRepository demoLicRepository;

  @RequestMapping(value = "/lic", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<Map<String, Object>> findLicense() throws Exception {
    Map<String, Object> test = demoLicRepository.findbylic("SIT1");
    return  new ResponseEntity<Map<String, Object>>(test, HttpStatus.OK);
  }
Run Code Online (Sandbox Code Playgroud)

存储库:

@Repository
public class DemoLicRepository {

  @Autowired
  MongoTemplate template;

  public Map<String, Object> findbylicenseid(String key) throws Exception {
    Query findQuery = new Query();
    findQuery.addCriteria(Criteria.where("_id").is(key));
    template.findOne(findQuery, Map.class, "win_lic");
    Map<String, Object> res = template.findOne(findQuery, Map.class, "win_licenses_poc");
    return template.findOne(findQuery, Map.class, "win_licenses_poc")
}
Run Code Online (Sandbox Code Playgroud)

如果我删除该customer_ref块,代码就可以正常工作。