Creating mongo query in Spring using mongoTemplate










0















I need help in 2 things.




  1. Creating a Spring mongoquery from a mongo query using spring mongo template.


  2. Filter the result and get only lower level objects from a document which has a json tree structure.



    I only want to get a list of matching merchant_shops by passing
    community_id,list of package_type_id and category_id.





I am using mongodb version 4.
Thank you in advance.



Document:




"_id" : ObjectId("5bdafff206ba681e30895e50"),
"community_id" : "09a5c059-33b3-4e47-9905-709f5c244580",
"package_types" : [

"package_type_id" : 1,
"categories" : [

"category_id" : "5bd0433d8ac2ce275082ff1f",
"subcategories" : [

"subcategory_id" : "5bd0436d8ac2ce275082ff20",
"merchant_shops" : [

"shop_id" : "5bd19a704ab492427d7326a0",
"distance" : 14.2841995007249
,

"shop_id" : "5bd84aed4ab4926fb2060e76",
"distance" : 14.283313487973
,

"shop_id" : "5bdc14ad4ab492123f29c6a7",
"distance" : 14.2829648551977
,

"shop_id" : "5be9555e4ab4923a01c7b7f8",
"distance" : 11.8215058435006
,

"shop_id" : "5be95a974ab4923a01c7b7fc",
"distance" : 11.8081739265758

]

]

]
,

"package_type_id" : 3,
"categories" : [

"category_id" : "5bd0433d8ac2ce275082ff1f",
"subcategories" : [

"subcategory_id" : "5bd0436d8ac2ce275082ff20",
"merchant_shops" : [

"shop_id" : "5bd84aed4ab4926fb2060e76",
"distance" : 14.283313487973

]

]

]

]



Query that works fine in mongodb:



db.getCollection('community_shop').aggregate([
$match: "community_id": "09a5c059-33b3-4e47-9905-709f5c244580" ,
$project:

package_types:
$filter:
input: '$package_types',
as: 'item',
cond:
$or: [

$in: ['$$item.package_type_id', [1,3]]
,
$eq: ['$$item.categories.category_id', "5bd0433d8ac2ce275082ff1f"]

]




])


Java class is:



@Document(collection = "community_shop")
public class CommunityShopDOB {

@Id
private String id;

@Field("community_id")
private String communityId;

@Field("package_types")
private List<PackageType> packageTypes;

public static class PackageType

@Field("package_type_id")
private Integer packageTypeId;

@Field("categories")
private List<Category> categories;

public static class Category

@Field("category_id")
private String categoryId;

@Field("subcategories")
private List<Subcategory> subcategories;

public static class Subcategory

@Field("subcategory_id")
private String subcategoryId;

@Field("merchant_shops")
private List<MerchantShop> merchantShops;

public static class MerchantShop

@Field("shop_id")
private String shopId;

@Field("distance")
private Double distance;

@Field("filter_value_ids")
private List<String> filterValueIds;






My solution so far is and it is incorrect:



Criteria cr = Criteria.where("communityId").is(society_id);
MatchOperation filterStates = Aggregation.match(cr);
Aggregation aggregation = newAggregation(filterStates, project("packageTypes")
.and(filter("packageTypes").as("item").by(valueOf("item.packageTypeId").equalToValue(1))

).as("packageTypes"));

AggregationResults<CommunityShopDOB> output = mongoTemplate.aggregate(aggregation, CommunityShopDOB.class,
CommunityShopDOB.class);
return output.getUniqueMappedResult();









share|improve this question
























  • Well it's only "working fine" because you are not actually matching anything on the second condition of the $or. and that expression is actually incorrect. More to the direct point is "Where is your attempted code writing this with spring mongo?". There's plenty of spring mongodb aggregation examples around. So instead of asking someone to write all your code for you, it's usually far better received when you show what you attempted so far and which part you have trouble understanding.

    – Neil Lunn
    Nov 14 '18 at 4:18











  • Good point..i m stuck for a week. I have tried several times. I didn't get syntax for problem like this only.

    – Nabin Kumar Khatiwada
    Nov 14 '18 at 5:04











  • I have updated the problem

    – Nabin Kumar Khatiwada
    Nov 15 '18 at 0:16















0















I need help in 2 things.




  1. Creating a Spring mongoquery from a mongo query using spring mongo template.


  2. Filter the result and get only lower level objects from a document which has a json tree structure.



    I only want to get a list of matching merchant_shops by passing
    community_id,list of package_type_id and category_id.





I am using mongodb version 4.
Thank you in advance.



Document:




"_id" : ObjectId("5bdafff206ba681e30895e50"),
"community_id" : "09a5c059-33b3-4e47-9905-709f5c244580",
"package_types" : [

"package_type_id" : 1,
"categories" : [

"category_id" : "5bd0433d8ac2ce275082ff1f",
"subcategories" : [

"subcategory_id" : "5bd0436d8ac2ce275082ff20",
"merchant_shops" : [

"shop_id" : "5bd19a704ab492427d7326a0",
"distance" : 14.2841995007249
,

"shop_id" : "5bd84aed4ab4926fb2060e76",
"distance" : 14.283313487973
,

"shop_id" : "5bdc14ad4ab492123f29c6a7",
"distance" : 14.2829648551977
,

"shop_id" : "5be9555e4ab4923a01c7b7f8",
"distance" : 11.8215058435006
,

"shop_id" : "5be95a974ab4923a01c7b7fc",
"distance" : 11.8081739265758

]

]

]
,

"package_type_id" : 3,
"categories" : [

"category_id" : "5bd0433d8ac2ce275082ff1f",
"subcategories" : [

"subcategory_id" : "5bd0436d8ac2ce275082ff20",
"merchant_shops" : [

"shop_id" : "5bd84aed4ab4926fb2060e76",
"distance" : 14.283313487973

]

]

]

]



Query that works fine in mongodb:



db.getCollection('community_shop').aggregate([
$match: "community_id": "09a5c059-33b3-4e47-9905-709f5c244580" ,
$project:

package_types:
$filter:
input: '$package_types',
as: 'item',
cond:
$or: [

$in: ['$$item.package_type_id', [1,3]]
,
$eq: ['$$item.categories.category_id', "5bd0433d8ac2ce275082ff1f"]

]




])


Java class is:



@Document(collection = "community_shop")
public class CommunityShopDOB {

@Id
private String id;

@Field("community_id")
private String communityId;

@Field("package_types")
private List<PackageType> packageTypes;

public static class PackageType

@Field("package_type_id")
private Integer packageTypeId;

@Field("categories")
private List<Category> categories;

public static class Category

@Field("category_id")
private String categoryId;

@Field("subcategories")
private List<Subcategory> subcategories;

public static class Subcategory

@Field("subcategory_id")
private String subcategoryId;

@Field("merchant_shops")
private List<MerchantShop> merchantShops;

public static class MerchantShop

@Field("shop_id")
private String shopId;

@Field("distance")
private Double distance;

@Field("filter_value_ids")
private List<String> filterValueIds;






My solution so far is and it is incorrect:



Criteria cr = Criteria.where("communityId").is(society_id);
MatchOperation filterStates = Aggregation.match(cr);
Aggregation aggregation = newAggregation(filterStates, project("packageTypes")
.and(filter("packageTypes").as("item").by(valueOf("item.packageTypeId").equalToValue(1))

).as("packageTypes"));

AggregationResults<CommunityShopDOB> output = mongoTemplate.aggregate(aggregation, CommunityShopDOB.class,
CommunityShopDOB.class);
return output.getUniqueMappedResult();









share|improve this question
























  • Well it's only "working fine" because you are not actually matching anything on the second condition of the $or. and that expression is actually incorrect. More to the direct point is "Where is your attempted code writing this with spring mongo?". There's plenty of spring mongodb aggregation examples around. So instead of asking someone to write all your code for you, it's usually far better received when you show what you attempted so far and which part you have trouble understanding.

    – Neil Lunn
    Nov 14 '18 at 4:18











  • Good point..i m stuck for a week. I have tried several times. I didn't get syntax for problem like this only.

    – Nabin Kumar Khatiwada
    Nov 14 '18 at 5:04











  • I have updated the problem

    – Nabin Kumar Khatiwada
    Nov 15 '18 at 0:16













0












0








0








I need help in 2 things.




  1. Creating a Spring mongoquery from a mongo query using spring mongo template.


  2. Filter the result and get only lower level objects from a document which has a json tree structure.



    I only want to get a list of matching merchant_shops by passing
    community_id,list of package_type_id and category_id.





I am using mongodb version 4.
Thank you in advance.



Document:




"_id" : ObjectId("5bdafff206ba681e30895e50"),
"community_id" : "09a5c059-33b3-4e47-9905-709f5c244580",
"package_types" : [

"package_type_id" : 1,
"categories" : [

"category_id" : "5bd0433d8ac2ce275082ff1f",
"subcategories" : [

"subcategory_id" : "5bd0436d8ac2ce275082ff20",
"merchant_shops" : [

"shop_id" : "5bd19a704ab492427d7326a0",
"distance" : 14.2841995007249
,

"shop_id" : "5bd84aed4ab4926fb2060e76",
"distance" : 14.283313487973
,

"shop_id" : "5bdc14ad4ab492123f29c6a7",
"distance" : 14.2829648551977
,

"shop_id" : "5be9555e4ab4923a01c7b7f8",
"distance" : 11.8215058435006
,

"shop_id" : "5be95a974ab4923a01c7b7fc",
"distance" : 11.8081739265758

]

]

]
,

"package_type_id" : 3,
"categories" : [

"category_id" : "5bd0433d8ac2ce275082ff1f",
"subcategories" : [

"subcategory_id" : "5bd0436d8ac2ce275082ff20",
"merchant_shops" : [

"shop_id" : "5bd84aed4ab4926fb2060e76",
"distance" : 14.283313487973

]

]

]

]



Query that works fine in mongodb:



db.getCollection('community_shop').aggregate([
$match: "community_id": "09a5c059-33b3-4e47-9905-709f5c244580" ,
$project:

package_types:
$filter:
input: '$package_types',
as: 'item',
cond:
$or: [

$in: ['$$item.package_type_id', [1,3]]
,
$eq: ['$$item.categories.category_id', "5bd0433d8ac2ce275082ff1f"]

]




])


Java class is:



@Document(collection = "community_shop")
public class CommunityShopDOB {

@Id
private String id;

@Field("community_id")
private String communityId;

@Field("package_types")
private List<PackageType> packageTypes;

public static class PackageType

@Field("package_type_id")
private Integer packageTypeId;

@Field("categories")
private List<Category> categories;

public static class Category

@Field("category_id")
private String categoryId;

@Field("subcategories")
private List<Subcategory> subcategories;

public static class Subcategory

@Field("subcategory_id")
private String subcategoryId;

@Field("merchant_shops")
private List<MerchantShop> merchantShops;

public static class MerchantShop

@Field("shop_id")
private String shopId;

@Field("distance")
private Double distance;

@Field("filter_value_ids")
private List<String> filterValueIds;






My solution so far is and it is incorrect:



Criteria cr = Criteria.where("communityId").is(society_id);
MatchOperation filterStates = Aggregation.match(cr);
Aggregation aggregation = newAggregation(filterStates, project("packageTypes")
.and(filter("packageTypes").as("item").by(valueOf("item.packageTypeId").equalToValue(1))

).as("packageTypes"));

AggregationResults<CommunityShopDOB> output = mongoTemplate.aggregate(aggregation, CommunityShopDOB.class,
CommunityShopDOB.class);
return output.getUniqueMappedResult();









share|improve this question
















I need help in 2 things.




  1. Creating a Spring mongoquery from a mongo query using spring mongo template.


  2. Filter the result and get only lower level objects from a document which has a json tree structure.



    I only want to get a list of matching merchant_shops by passing
    community_id,list of package_type_id and category_id.





I am using mongodb version 4.
Thank you in advance.



Document:




"_id" : ObjectId("5bdafff206ba681e30895e50"),
"community_id" : "09a5c059-33b3-4e47-9905-709f5c244580",
"package_types" : [

"package_type_id" : 1,
"categories" : [

"category_id" : "5bd0433d8ac2ce275082ff1f",
"subcategories" : [

"subcategory_id" : "5bd0436d8ac2ce275082ff20",
"merchant_shops" : [

"shop_id" : "5bd19a704ab492427d7326a0",
"distance" : 14.2841995007249
,

"shop_id" : "5bd84aed4ab4926fb2060e76",
"distance" : 14.283313487973
,

"shop_id" : "5bdc14ad4ab492123f29c6a7",
"distance" : 14.2829648551977
,

"shop_id" : "5be9555e4ab4923a01c7b7f8",
"distance" : 11.8215058435006
,

"shop_id" : "5be95a974ab4923a01c7b7fc",
"distance" : 11.8081739265758

]

]

]
,

"package_type_id" : 3,
"categories" : [

"category_id" : "5bd0433d8ac2ce275082ff1f",
"subcategories" : [

"subcategory_id" : "5bd0436d8ac2ce275082ff20",
"merchant_shops" : [

"shop_id" : "5bd84aed4ab4926fb2060e76",
"distance" : 14.283313487973

]

]

]

]



Query that works fine in mongodb:



db.getCollection('community_shop').aggregate([
$match: "community_id": "09a5c059-33b3-4e47-9905-709f5c244580" ,
$project:

package_types:
$filter:
input: '$package_types',
as: 'item',
cond:
$or: [

$in: ['$$item.package_type_id', [1,3]]
,
$eq: ['$$item.categories.category_id', "5bd0433d8ac2ce275082ff1f"]

]




])


Java class is:



@Document(collection = "community_shop")
public class CommunityShopDOB {

@Id
private String id;

@Field("community_id")
private String communityId;

@Field("package_types")
private List<PackageType> packageTypes;

public static class PackageType

@Field("package_type_id")
private Integer packageTypeId;

@Field("categories")
private List<Category> categories;

public static class Category

@Field("category_id")
private String categoryId;

@Field("subcategories")
private List<Subcategory> subcategories;

public static class Subcategory

@Field("subcategory_id")
private String subcategoryId;

@Field("merchant_shops")
private List<MerchantShop> merchantShops;

public static class MerchantShop

@Field("shop_id")
private String shopId;

@Field("distance")
private Double distance;

@Field("filter_value_ids")
private List<String> filterValueIds;






My solution so far is and it is incorrect:



Criteria cr = Criteria.where("communityId").is(society_id);
MatchOperation filterStates = Aggregation.match(cr);
Aggregation aggregation = newAggregation(filterStates, project("packageTypes")
.and(filter("packageTypes").as("item").by(valueOf("item.packageTypeId").equalToValue(1))

).as("packageTypes"));

AggregationResults<CommunityShopDOB> output = mongoTemplate.aggregate(aggregation, CommunityShopDOB.class,
CommunityShopDOB.class);
return output.getUniqueMappedResult();






java spring mongodb mongotemplate






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 19:40







Nabin Kumar Khatiwada

















asked Nov 14 '18 at 3:36









Nabin Kumar KhatiwadaNabin Kumar Khatiwada

63249




63249












  • Well it's only "working fine" because you are not actually matching anything on the second condition of the $or. and that expression is actually incorrect. More to the direct point is "Where is your attempted code writing this with spring mongo?". There's plenty of spring mongodb aggregation examples around. So instead of asking someone to write all your code for you, it's usually far better received when you show what you attempted so far and which part you have trouble understanding.

    – Neil Lunn
    Nov 14 '18 at 4:18











  • Good point..i m stuck for a week. I have tried several times. I didn't get syntax for problem like this only.

    – Nabin Kumar Khatiwada
    Nov 14 '18 at 5:04











  • I have updated the problem

    – Nabin Kumar Khatiwada
    Nov 15 '18 at 0:16

















  • Well it's only "working fine" because you are not actually matching anything on the second condition of the $or. and that expression is actually incorrect. More to the direct point is "Where is your attempted code writing this with spring mongo?". There's plenty of spring mongodb aggregation examples around. So instead of asking someone to write all your code for you, it's usually far better received when you show what you attempted so far and which part you have trouble understanding.

    – Neil Lunn
    Nov 14 '18 at 4:18











  • Good point..i m stuck for a week. I have tried several times. I didn't get syntax for problem like this only.

    – Nabin Kumar Khatiwada
    Nov 14 '18 at 5:04











  • I have updated the problem

    – Nabin Kumar Khatiwada
    Nov 15 '18 at 0:16
















Well it's only "working fine" because you are not actually matching anything on the second condition of the $or. and that expression is actually incorrect. More to the direct point is "Where is your attempted code writing this with spring mongo?". There's plenty of spring mongodb aggregation examples around. So instead of asking someone to write all your code for you, it's usually far better received when you show what you attempted so far and which part you have trouble understanding.

– Neil Lunn
Nov 14 '18 at 4:18





Well it's only "working fine" because you are not actually matching anything on the second condition of the $or. and that expression is actually incorrect. More to the direct point is "Where is your attempted code writing this with spring mongo?". There's plenty of spring mongodb aggregation examples around. So instead of asking someone to write all your code for you, it's usually far better received when you show what you attempted so far and which part you have trouble understanding.

– Neil Lunn
Nov 14 '18 at 4:18













Good point..i m stuck for a week. I have tried several times. I didn't get syntax for problem like this only.

– Nabin Kumar Khatiwada
Nov 14 '18 at 5:04





Good point..i m stuck for a week. I have tried several times. I didn't get syntax for problem like this only.

– Nabin Kumar Khatiwada
Nov 14 '18 at 5:04













I have updated the problem

– Nabin Kumar Khatiwada
Nov 15 '18 at 0:16





I have updated the problem

– Nabin Kumar Khatiwada
Nov 15 '18 at 0:16












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292836%2fcreating-mongo-query-in-spring-using-mongotemplate%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292836%2fcreating-mongo-query-in-spring-using-mongotemplate%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線