从MongoTemplate和Query API的可用数组对象列表中仅提取匹配的数组对象。

huangapple go评论87阅读模式
英文:

Fetch only matched array object from a list of available array objects with MongoTemplate and Query API

问题

I can help you with the translation. Here's the translated content:

有一个情况,我将参数传递给MongoDB中的一个集合,并仅获取与我的条件匹配的特定数组对象"serviceType"= Repairs & Fixes。

集合示例

"serviceTypes": [
{
"serviceType": "Installation/Uninstallation Service",
},
{
"serviceType": "Repairs & Fixes",
},
{
"serviceType": "Electricity Breakdown",
},
{
"serviceType": "Electricity Wiring",
}
]

根据我的实现,它获取所有数组对象,而不是特定的数组对象。上面的集合示例是以下代码的实际响应。

  1. import org.springframework.data.mongodb.core.MongoTemplate;
  2. import org.springframework.data.mongodb.core.mapping.Field;
  3. import org.springframework.data.mongodb.core.query.Criteria;
  4. import org.springframework.data.mongodb.core.query.Query;
  5. @Override
  6. public ServiceList findOneByServiceType(String categoryName, String serviceType1) {
  7. // TODO Auto-generated method stub
  8. Query query = new Query() ;
  9. query.addCriteria(Criteria.where("categoryName").is(categoryName).andOperator(Criteria.where("serviceTypes").elemMatch(Criteria.where("serviceType").is(serviceType1))));
  10. query.fields().include("serviceTypes.serviceType");
  11. return (ServiceList) mongoTemplate.findOne(query,ServiceList.class);
  12. }

如何使用Spring Boot Mongo的mongoTemplate和Query API处理此情况?

英文:

I have a scenario where i pass a parameters to a collection in MongoDB and fetch only that particular array object serviceType which matched my criteria "serviceType"= Repairs & Fixes.

Collection Sample

  1. "serviceTypes": [
  2. {
  3. "serviceType": "Installation/Uninstallation Service",
  4. },
  5. {
  6. "serviceType": "Repairs & Fixes",
  7. },
  8. {
  9. "serviceType": "Electricity Breakdown",
  10. },
  11. {
  12. "serviceType": "Electricity Wiring",
  13. }
  14. ]

As per my implementation below, it fetches all the array objects instead of that particular array object. The above collection sample is the actual response for the below code.

  1. import org.springframework.data.mongodb.core.MongoTemplate;
  2. import org.springframework.data.mongodb.core.mapping.Field;
  3. import org.springframework.data.mongodb.core.query.Criteria;
  4. import org.springframework.data.mongodb.core.query.Query;
  5. @Override
  6. public ServiceList findOneByServiceType(String categoryName, String
  7. serviceType1) {
  8. // TODO Auto-generated method stub
  9. Query query = new Query() ;
  10. query.addCriteria(Criteria.where("categoryName").is(categoryName).andOperator(Criteria.where("serviceTypes").elemMatch(Criteria.where("serviceType").is(serviceType1))));
  11. query.fields().include("serviceTypes.serviceType");
  12. return (ServiceList) mongoTemplate.findOne(query,ServiceList.class);
  13. }

How to handle this scenario with mongotemplate and Query api of Spring boot Mongo?

答案1

得分: 1

只需将这一行替换为以下行:

  1. query.fields().include("serviceTypes").position("serviceTypes",1);
英文:

You just need to replace this line

  1. query.fields().include("serviceTypes.serviceType");

with the below line

  1. query.fields().include("serviceTypes").position("serviceTypes",1);

huangapple
  • 本文由 发表于 2020年8月14日 00:03:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63398995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定