write mongodb lookup with multiple $expr to java
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
add a comment |
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
1
Your$matchexpression is wrong. You cannot use the same object key of$exprtwice. For this you use$andwhich is an "aggregation operator" so it needs to be "inside" the single$exprkey. Also which version of spring-mongo ( or even just plain Java Driver ) is this?BasicDBObjectis not used in modern releases and replaced byDocument. If you are using an older driver then the bigger concern is compatibility issues with modern MongoDB servers.Documentcame about before the new$lookupwas available.
– Neil Lunn
Nov 13 '18 at 22:01
@NeilLunn Np, i can useandin query like this"pipeline" : [ "$match" : "$expr" : "$and" : [ "$eq" : [ "$$s_date" , "$s_date"] , "$eq" :["$$s_id" : "$s_id"]]]. But how to write$eqmore than once becauseBasicDBObjectdoesn't allow duplicate keys. I am usingspring-boot-data-mongodb-1.5.10.RELEASEandmongodb-3.6.X
– SRK
Nov 14 '18 at 4:29
Can you check your dependencies please. I don't know whatspring-boot-data-mongodbis. There isspring-data-mongodband the current releases start with2.x. At any rate the$and: [ .. ]list just aList, so you can usually just do aArrays.asList()or similar construction for creating a list ofDBObject. But as stated, you really should check the dependencies and update here because those all really should beDocument, and more importantly using a modernmongodb-javarelease.
– Neil Lunn
Nov 14 '18 at 4:36
with above spring-boot versionmongodb-driver-3.4.3andspring-data-mongodb-1.10.10.RELEASE
– SRK
Nov 14 '18 at 4:45
Added intostringthen added using thisArrays.asList(). Its working thanks
– SRK
Nov 14 '18 at 10:48
add a comment |
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
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
java mongodb spring-boot aggregation-framework spring-data-mongodb
asked Nov 13 '18 at 15:40
SRKSRK
185
185
1
Your$matchexpression is wrong. You cannot use the same object key of$exprtwice. For this you use$andwhich is an "aggregation operator" so it needs to be "inside" the single$exprkey. Also which version of spring-mongo ( or even just plain Java Driver ) is this?BasicDBObjectis not used in modern releases and replaced byDocument. If you are using an older driver then the bigger concern is compatibility issues with modern MongoDB servers.Documentcame about before the new$lookupwas available.
– Neil Lunn
Nov 13 '18 at 22:01
@NeilLunn Np, i can useandin query like this"pipeline" : [ "$match" : "$expr" : "$and" : [ "$eq" : [ "$$s_date" , "$s_date"] , "$eq" :["$$s_id" : "$s_id"]]]. But how to write$eqmore than once becauseBasicDBObjectdoesn't allow duplicate keys. I am usingspring-boot-data-mongodb-1.5.10.RELEASEandmongodb-3.6.X
– SRK
Nov 14 '18 at 4:29
Can you check your dependencies please. I don't know whatspring-boot-data-mongodbis. There isspring-data-mongodband the current releases start with2.x. At any rate the$and: [ .. ]list just aList, so you can usually just do aArrays.asList()or similar construction for creating a list ofDBObject. But as stated, you really should check the dependencies and update here because those all really should beDocument, and more importantly using a modernmongodb-javarelease.
– Neil Lunn
Nov 14 '18 at 4:36
with above spring-boot versionmongodb-driver-3.4.3andspring-data-mongodb-1.10.10.RELEASE
– SRK
Nov 14 '18 at 4:45
Added intostringthen added using thisArrays.asList(). Its working thanks
– SRK
Nov 14 '18 at 10:48
add a comment |
1
Your$matchexpression is wrong. You cannot use the same object key of$exprtwice. For this you use$andwhich is an "aggregation operator" so it needs to be "inside" the single$exprkey. Also which version of spring-mongo ( or even just plain Java Driver ) is this?BasicDBObjectis not used in modern releases and replaced byDocument. If you are using an older driver then the bigger concern is compatibility issues with modern MongoDB servers.Documentcame about before the new$lookupwas available.
– Neil Lunn
Nov 13 '18 at 22:01
@NeilLunn Np, i can useandin query like this"pipeline" : [ "$match" : "$expr" : "$and" : [ "$eq" : [ "$$s_date" , "$s_date"] , "$eq" :["$$s_id" : "$s_id"]]]. But how to write$eqmore than once becauseBasicDBObjectdoesn't allow duplicate keys. I am usingspring-boot-data-mongodb-1.5.10.RELEASEandmongodb-3.6.X
– SRK
Nov 14 '18 at 4:29
Can you check your dependencies please. I don't know whatspring-boot-data-mongodbis. There isspring-data-mongodband the current releases start with2.x. At any rate the$and: [ .. ]list just aList, so you can usually just do aArrays.asList()or similar construction for creating a list ofDBObject. But as stated, you really should check the dependencies and update here because those all really should beDocument, and more importantly using a modernmongodb-javarelease.
– Neil Lunn
Nov 14 '18 at 4:36
with above spring-boot versionmongodb-driver-3.4.3andspring-data-mongodb-1.10.10.RELEASE
– SRK
Nov 14 '18 at 4:45
Added intostringthen added using thisArrays.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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Your
$matchexpression is wrong. You cannot use the same object key of$exprtwice. For this you use$andwhich is an "aggregation operator" so it needs to be "inside" the single$exprkey. Also which version of spring-mongo ( or even just plain Java Driver ) is this?BasicDBObjectis not used in modern releases and replaced byDocument. If you are using an older driver then the bigger concern is compatibility issues with modern MongoDB servers.Documentcame about before the new$lookupwas available.– Neil Lunn
Nov 13 '18 at 22:01
@NeilLunn Np, i can use
andin query like this"pipeline" : [ "$match" : "$expr" : "$and" : [ "$eq" : [ "$$s_date" , "$s_date"] , "$eq" :["$$s_id" : "$s_id"]]]. But how to write$eqmore than once becauseBasicDBObjectdoesn't allow duplicate keys. I am usingspring-boot-data-mongodb-1.5.10.RELEASEandmongodb-3.6.X– SRK
Nov 14 '18 at 4:29
Can you check your dependencies please. I don't know what
spring-boot-data-mongodbis. There isspring-data-mongodband the current releases start with2.x. At any rate the$and: [ .. ]list just aList, so you can usually just do aArrays.asList()or similar construction for creating a list ofDBObject. But as stated, you really should check the dependencies and update here because those all really should beDocument, and more importantly using a modernmongodb-javarelease.– Neil Lunn
Nov 14 '18 at 4:36
with above spring-boot version
mongodb-driver-3.4.3andspring-data-mongodb-1.10.10.RELEASE– SRK
Nov 14 '18 at 4:45
Added into
stringthen added using thisArrays.asList(). Its working thanks– SRK
Nov 14 '18 at 10:48