write mongodb lookup with multiple $expr to java










0















I have the following lookup script :-




"$lookup":
"from": "table2",
"let": "s_id": "$s_id","s_date": "$s_date",
"pipeline": [
"$match":
"$expr": "$eq": ["$$s_id","$s_id"],
"$expr": "$eq": ["$$s_date","$s_date"]


],
"as": "table2_values"




$expr value will be dynamic(on 1 field or more than 1 fields) but here i am taking 2 fields for example. I am trying to write above aggregation script to java. Could you please help me



List<String> whereClauseTable1List = new ArrayList<>();
whereClauseTable1List.add("s_id");
whereClauseTable1List.add("s_date");

List<String> whereClauseTable2List = new ArrayList<>();
whereClauseTable2List.add("s_id");
whereClauseTable2List.add("s_date");

BasicDBObject letTable1Var = new BasicDBObject();
for(String whereClauseTable1: whereClauseTable1List)
letTable1Var.put(whereClauseTable1,"$"+whereClauseTable1);


BasicDBObject pipeLineMatchExp = new BasicDBObject();
int index = 0;
for(String whereClauseTable2: whereClauseTable2List)
pipeLineMatchExp.append("$expr", new BasicDBObject("$eq", new
Object"$$" + whereClauseTable2, "$" +
whereClauseTable1List.get(index)));
index++;


DBObject lookupDBOp = new BasicDBObject(
"$lookup",
new BasicDBObject("from","table2")
.append("let", letTable1Var)
.append("pipeline",new Objectnew
BasicDBObject("$match", pipeLineMatchExp))
.append("as", "table2_values")
);


Actually BasicObject does not take the duplicate key, so its always be having 1 $expr condition. I would like to write above script










share|improve this question

















  • 1





    Your $match expression is wrong. You cannot use the same object key of $expr twice. For this you use $and which is an "aggregation operator" so it needs to be "inside" the single $expr key. Also which version of spring-mongo ( or even just plain Java Driver ) is this? BasicDBObject is not used in modern releases and replaced by Document. If you are using an older driver then the bigger concern is compatibility issues with modern MongoDB servers. Document came about before the new $lookup was available.

    – Neil Lunn
    Nov 13 '18 at 22:01











  • @NeilLunn Np, i can use and in query like this "pipeline" : [ "$match" : "$expr" : "$and" : [ "$eq" : [ "$$s_date" , "$s_date"] , "$eq" :["$$s_id" : "$s_id"]]]. But how to write $eq more than once because BasicDBObject doesn't allow duplicate keys. I am using spring-boot-data-mongodb-1.5.10.RELEASE and mongodb-3.6.X

    – SRK
    Nov 14 '18 at 4:29












  • Can you check your dependencies please. I don't know what spring-boot-data-mongodb is. There is spring-data-mongodb and the current releases start with 2.x. At any rate the $and: [ .. ] list just a List, so you can usually just do a Arrays.asList() or similar construction for creating a list of DBObject. But as stated, you really should check the dependencies and update here because those all really should be Document, and more importantly using a modern mongodb-java release.

    – Neil Lunn
    Nov 14 '18 at 4:36











  • with above spring-boot version mongodb-driver-3.4.3 and spring-data-mongodb-1.10.10.RELEASE

    – SRK
    Nov 14 '18 at 4:45











  • Added into string then added using this Arrays.asList(). Its working thanks

    – SRK
    Nov 14 '18 at 10:48















0















I have the following lookup script :-




"$lookup":
"from": "table2",
"let": "s_id": "$s_id","s_date": "$s_date",
"pipeline": [
"$match":
"$expr": "$eq": ["$$s_id","$s_id"],
"$expr": "$eq": ["$$s_date","$s_date"]


],
"as": "table2_values"




$expr value will be dynamic(on 1 field or more than 1 fields) but here i am taking 2 fields for example. I am trying to write above aggregation script to java. Could you please help me



List<String> whereClauseTable1List = new ArrayList<>();
whereClauseTable1List.add("s_id");
whereClauseTable1List.add("s_date");

List<String> whereClauseTable2List = new ArrayList<>();
whereClauseTable2List.add("s_id");
whereClauseTable2List.add("s_date");

BasicDBObject letTable1Var = new BasicDBObject();
for(String whereClauseTable1: whereClauseTable1List)
letTable1Var.put(whereClauseTable1,"$"+whereClauseTable1);


BasicDBObject pipeLineMatchExp = new BasicDBObject();
int index = 0;
for(String whereClauseTable2: whereClauseTable2List)
pipeLineMatchExp.append("$expr", new BasicDBObject("$eq", new
Object"$$" + whereClauseTable2, "$" +
whereClauseTable1List.get(index)));
index++;


DBObject lookupDBOp = new BasicDBObject(
"$lookup",
new BasicDBObject("from","table2")
.append("let", letTable1Var)
.append("pipeline",new Objectnew
BasicDBObject("$match", pipeLineMatchExp))
.append("as", "table2_values")
);


Actually BasicObject does not take the duplicate key, so its always be having 1 $expr condition. I would like to write above script










share|improve this question

















  • 1





    Your $match expression is wrong. You cannot use the same object key of $expr twice. For this you use $and which is an "aggregation operator" so it needs to be "inside" the single $expr key. Also which version of spring-mongo ( or even just plain Java Driver ) is this? BasicDBObject is not used in modern releases and replaced by Document. If you are using an older driver then the bigger concern is compatibility issues with modern MongoDB servers. Document came about before the new $lookup was available.

    – Neil Lunn
    Nov 13 '18 at 22:01











  • @NeilLunn Np, i can use and in query like this "pipeline" : [ "$match" : "$expr" : "$and" : [ "$eq" : [ "$$s_date" , "$s_date"] , "$eq" :["$$s_id" : "$s_id"]]]. But how to write $eq more than once because BasicDBObject doesn't allow duplicate keys. I am using spring-boot-data-mongodb-1.5.10.RELEASE and mongodb-3.6.X

    – SRK
    Nov 14 '18 at 4:29












  • Can you check your dependencies please. I don't know what spring-boot-data-mongodb is. There is spring-data-mongodb and the current releases start with 2.x. At any rate the $and: [ .. ] list just a List, so you can usually just do a Arrays.asList() or similar construction for creating a list of DBObject. But as stated, you really should check the dependencies and update here because those all really should be Document, and more importantly using a modern mongodb-java release.

    – Neil Lunn
    Nov 14 '18 at 4:36











  • with above spring-boot version mongodb-driver-3.4.3 and spring-data-mongodb-1.10.10.RELEASE

    – SRK
    Nov 14 '18 at 4:45











  • Added into string then added using this Arrays.asList(). Its working thanks

    – SRK
    Nov 14 '18 at 10:48













0












0








0








I have the following lookup script :-




"$lookup":
"from": "table2",
"let": "s_id": "$s_id","s_date": "$s_date",
"pipeline": [
"$match":
"$expr": "$eq": ["$$s_id","$s_id"],
"$expr": "$eq": ["$$s_date","$s_date"]


],
"as": "table2_values"




$expr value will be dynamic(on 1 field or more than 1 fields) but here i am taking 2 fields for example. I am trying to write above aggregation script to java. Could you please help me



List<String> whereClauseTable1List = new ArrayList<>();
whereClauseTable1List.add("s_id");
whereClauseTable1List.add("s_date");

List<String> whereClauseTable2List = new ArrayList<>();
whereClauseTable2List.add("s_id");
whereClauseTable2List.add("s_date");

BasicDBObject letTable1Var = new BasicDBObject();
for(String whereClauseTable1: whereClauseTable1List)
letTable1Var.put(whereClauseTable1,"$"+whereClauseTable1);


BasicDBObject pipeLineMatchExp = new BasicDBObject();
int index = 0;
for(String whereClauseTable2: whereClauseTable2List)
pipeLineMatchExp.append("$expr", new BasicDBObject("$eq", new
Object"$$" + whereClauseTable2, "$" +
whereClauseTable1List.get(index)));
index++;


DBObject lookupDBOp = new BasicDBObject(
"$lookup",
new BasicDBObject("from","table2")
.append("let", letTable1Var)
.append("pipeline",new Objectnew
BasicDBObject("$match", pipeLineMatchExp))
.append("as", "table2_values")
);


Actually BasicObject does not take the duplicate key, so its always be having 1 $expr condition. I would like to write above script










share|improve this question














I have the following lookup script :-




"$lookup":
"from": "table2",
"let": "s_id": "$s_id","s_date": "$s_date",
"pipeline": [
"$match":
"$expr": "$eq": ["$$s_id","$s_id"],
"$expr": "$eq": ["$$s_date","$s_date"]


],
"as": "table2_values"




$expr value will be dynamic(on 1 field or more than 1 fields) but here i am taking 2 fields for example. I am trying to write above aggregation script to java. Could you please help me



List<String> whereClauseTable1List = new ArrayList<>();
whereClauseTable1List.add("s_id");
whereClauseTable1List.add("s_date");

List<String> whereClauseTable2List = new ArrayList<>();
whereClauseTable2List.add("s_id");
whereClauseTable2List.add("s_date");

BasicDBObject letTable1Var = new BasicDBObject();
for(String whereClauseTable1: whereClauseTable1List)
letTable1Var.put(whereClauseTable1,"$"+whereClauseTable1);


BasicDBObject pipeLineMatchExp = new BasicDBObject();
int index = 0;
for(String whereClauseTable2: whereClauseTable2List)
pipeLineMatchExp.append("$expr", new BasicDBObject("$eq", new
Object"$$" + whereClauseTable2, "$" +
whereClauseTable1List.get(index)));
index++;


DBObject lookupDBOp = new BasicDBObject(
"$lookup",
new BasicDBObject("from","table2")
.append("let", letTable1Var)
.append("pipeline",new Objectnew
BasicDBObject("$match", pipeLineMatchExp))
.append("as", "table2_values")
);


Actually BasicObject does not take the duplicate key, so its always be having 1 $expr condition. I would like to write above script







java mongodb spring-boot aggregation-framework spring-data-mongodb






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 15:40









SRKSRK

185




185







  • 1





    Your $match expression is wrong. You cannot use the same object key of $expr twice. For this you use $and which is an "aggregation operator" so it needs to be "inside" the single $expr key. Also which version of spring-mongo ( or even just plain Java Driver ) is this? BasicDBObject is not used in modern releases and replaced by Document. If you are using an older driver then the bigger concern is compatibility issues with modern MongoDB servers. Document came about before the new $lookup was available.

    – Neil Lunn
    Nov 13 '18 at 22:01











  • @NeilLunn Np, i can use and in query like this "pipeline" : [ "$match" : "$expr" : "$and" : [ "$eq" : [ "$$s_date" , "$s_date"] , "$eq" :["$$s_id" : "$s_id"]]]. But how to write $eq more than once because BasicDBObject doesn't allow duplicate keys. I am using spring-boot-data-mongodb-1.5.10.RELEASE and mongodb-3.6.X

    – SRK
    Nov 14 '18 at 4:29












  • Can you check your dependencies please. I don't know what spring-boot-data-mongodb is. There is spring-data-mongodb and the current releases start with 2.x. At any rate the $and: [ .. ] list just a List, so you can usually just do a Arrays.asList() or similar construction for creating a list of DBObject. But as stated, you really should check the dependencies and update here because those all really should be Document, and more importantly using a modern mongodb-java release.

    – Neil Lunn
    Nov 14 '18 at 4:36











  • with above spring-boot version mongodb-driver-3.4.3 and spring-data-mongodb-1.10.10.RELEASE

    – SRK
    Nov 14 '18 at 4:45











  • Added into string then added using this Arrays.asList(). Its working thanks

    – SRK
    Nov 14 '18 at 10:48












  • 1





    Your $match expression is wrong. You cannot use the same object key of $expr twice. For this you use $and which is an "aggregation operator" so it needs to be "inside" the single $expr key. Also which version of spring-mongo ( or even just plain Java Driver ) is this? BasicDBObject is not used in modern releases and replaced by Document. If you are using an older driver then the bigger concern is compatibility issues with modern MongoDB servers. Document came about before the new $lookup was available.

    – Neil Lunn
    Nov 13 '18 at 22:01











  • @NeilLunn Np, i can use and in query like this "pipeline" : [ "$match" : "$expr" : "$and" : [ "$eq" : [ "$$s_date" , "$s_date"] , "$eq" :["$$s_id" : "$s_id"]]]. But how to write $eq more than once because BasicDBObject doesn't allow duplicate keys. I am using spring-boot-data-mongodb-1.5.10.RELEASE and mongodb-3.6.X

    – SRK
    Nov 14 '18 at 4:29












  • Can you check your dependencies please. I don't know what spring-boot-data-mongodb is. There is spring-data-mongodb and the current releases start with 2.x. At any rate the $and: [ .. ] list just a List, so you can usually just do a Arrays.asList() or similar construction for creating a list of DBObject. But as stated, you really should check the dependencies and update here because those all really should be Document, and more importantly using a modern mongodb-java release.

    – Neil Lunn
    Nov 14 '18 at 4:36











  • with above spring-boot version mongodb-driver-3.4.3 and spring-data-mongodb-1.10.10.RELEASE

    – SRK
    Nov 14 '18 at 4:45











  • Added into string then added using this Arrays.asList(). Its working thanks

    – SRK
    Nov 14 '18 at 10:48







1




1





Your $match expression is wrong. You cannot use the same object key of $expr twice. For this you use $and which is an "aggregation operator" so it needs to be "inside" the single $expr key. Also which version of spring-mongo ( or even just plain Java Driver ) is this? BasicDBObject is not used in modern releases and replaced by Document. If you are using an older driver then the bigger concern is compatibility issues with modern MongoDB servers. Document came about before the new $lookup was available.

– Neil Lunn
Nov 13 '18 at 22:01





Your $match expression is wrong. You cannot use the same object key of $expr twice. For this you use $and which is an "aggregation operator" so it needs to be "inside" the single $expr key. Also which version of spring-mongo ( or even just plain Java Driver ) is this? BasicDBObject is not used in modern releases and replaced by Document. If you are using an older driver then the bigger concern is compatibility issues with modern MongoDB servers. Document came about before the new $lookup was available.

– Neil Lunn
Nov 13 '18 at 22:01













@NeilLunn Np, i can use and in query like this "pipeline" : [ "$match" : "$expr" : "$and" : [ "$eq" : [ "$$s_date" , "$s_date"] , "$eq" :["$$s_id" : "$s_id"]]]. But how to write $eq more than once because BasicDBObject doesn't allow duplicate keys. I am using spring-boot-data-mongodb-1.5.10.RELEASE and mongodb-3.6.X

– SRK
Nov 14 '18 at 4:29






@NeilLunn Np, i can use and in query like this "pipeline" : [ "$match" : "$expr" : "$and" : [ "$eq" : [ "$$s_date" , "$s_date"] , "$eq" :["$$s_id" : "$s_id"]]]. But how to write $eq more than once because BasicDBObject doesn't allow duplicate keys. I am using spring-boot-data-mongodb-1.5.10.RELEASE and mongodb-3.6.X

– SRK
Nov 14 '18 at 4:29














Can you check your dependencies please. I don't know what spring-boot-data-mongodb is. There is spring-data-mongodb and the current releases start with 2.x. At any rate the $and: [ .. ] list just a List, so you can usually just do a Arrays.asList() or similar construction for creating a list of DBObject. But as stated, you really should check the dependencies and update here because those all really should be Document, and more importantly using a modern mongodb-java release.

– Neil Lunn
Nov 14 '18 at 4:36





Can you check your dependencies please. I don't know what spring-boot-data-mongodb is. There is spring-data-mongodb and the current releases start with 2.x. At any rate the $and: [ .. ] list just a List, so you can usually just do a Arrays.asList() or similar construction for creating a list of DBObject. But as stated, you really should check the dependencies and update here because those all really should be Document, and more importantly using a modern mongodb-java release.

– Neil Lunn
Nov 14 '18 at 4:36













with above spring-boot version mongodb-driver-3.4.3 and spring-data-mongodb-1.10.10.RELEASE

– SRK
Nov 14 '18 at 4:45





with above spring-boot version mongodb-driver-3.4.3 and spring-data-mongodb-1.10.10.RELEASE

– SRK
Nov 14 '18 at 4:45













Added into string then added using this Arrays.asList(). Its working thanks

– SRK
Nov 14 '18 at 10:48





Added into string then added using this Arrays.asList(). Its working thanks

– SRK
Nov 14 '18 at 10:48












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%2f53284514%2fwrite-mongodb-lookup-with-multiple-expr-to-java%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%2f53284514%2fwrite-mongodb-lookup-with-multiple-expr-to-java%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

ReactJS Fetched API data displays live - need Data displayed static

政党