java - BasicDBObjectBuilder not appending mutiple criteria for a single object -


i using java driver mongo-db , trying add multiple query criteria using basicdbobjectbuilder. have text field xml stored string using regex form query.

below query , output getting:

regexquery.put("request_xml",basicdbobjectbuilder .start("$regex", ".*main>[\r\n]<.?.?.?.?action>"+mainvalue+".*") .add("$regex", ".*details>[\r\n]<.?.?.?.?action>" + detailvalue+ ".*").get()); regexquery.put("name", "video"); 

what getting query :

{ "request_xml" : { "$regex" : ".*details>[\r\n]<.?.?.?.?action>change.*"} , "name" : "video"} 

the first part .start("$regex", ".main>[\r\n]<.?.?.?.?action>"+mainvalue+".") not getting added query.

can please let me know issue ?

you overwriting key value pair. "$regex", ".*details>[\r\n]<.?.?.?.?action>" + detailvalue+ ".*" overwrites "$regex", ".*main>[\r\n]<.?.?.?.?action>"+mainvalue+".*".

use $or pass both regex expression.

something like

basicdbobject regexquery = new basicdbobject(); regexquery.put("$or", arrays.aslist(new basicdbobject("request_xml", new basicdbobject("$regex", ".*main>[\r\n]<.?.?.?.?action>"+".*")),                 new basicdbobject("request_xml", new basicdbobject("$regex", ".*details>[\r\n]<.?.?.?.?action>"+".*")))); regexquery.put("name", "video"); 

this should output query like

{ "$or" : [{ "request_xml" : { "$regex" : ".*main>[\r\n]<.?.?.?.?action>.*" } }, { "request_xml" : { "$regex" : ".*details>[\r\n]<.?.?.?.?action>.*" } }], "name" : "video" } 

using 3.x driver

import static com.mongodb.client.model.filters.or; import static com.mongodb.client.model.filters.regex; bson regexquery = or(regex("request_xml", ".*main>[\r\n]<.?.?.?.?action>"+".*"), regex("$regex", ".*details>[\r\n]<.?.?.?.?action>"+".*")); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -