java - Find MongoDB records where status is a, b or c and it has some array which is greater than 0 -


i struggling build query 1 of criteria, have documents this:

[  {     "status": "a"  },  {     "status": "b"  },  {     "status": "foo"  },  {     "status": "a",     "flightdetails": [         {             "test": "xyz"         }     ]  } ] 

my criteria select this:

  • get data status either a, b or c, or
  • get data status either a, b or c , flightdetails, size > 0
  • where not documents contains flightdetails key.

i tried java code this:

basicdbobject inquery = new basicdbobject(); inquery.append("status", new basicdbobject("$nin", arrays.aslist("a", "b", "c")));  basicdbobject inquery2 = new basicdbobject(); inquery2.append("$where", "return object.keys( this.flightdetails").length > 0");  basicdblist criteria = new basicdblist(); criteria.add(inquery); criteria.add(inquery2);  basicdbobject finalquery = new basicdbobject("$or",criteria); 

but returns data, seems query not correct. searched lot , based on answers, tried build query. can kindly me in this? since beginner in nosql , mongodb , started on few days back.

using dot notation "0" element of array along $exists means has content, , of course exists:

basicdbobject query = basicdbobject("status",      new basicdbobject("$in", arrays.aslist("a", "b", "c")) )     .append("flightdetails.0",       new basicdbobject("$exists",true)     ); 

which is:

{     "status": { "$in": [ "a","b","c" ] },     "flightdetails.0": { "$exists": true } } 

for used json notation.

to incorporate "no array @ all" contruct $or condition:

{     "status": { "$in": [ "a","b","c" ] },     "$or": [         { "flightdetails.0": { "$exists": true } },         { "flightdetails": { "$exists": false } }     ] } 

or java:

basicdbobject query = basicdbobject("status",      new basicdbobject("$in", arrays.aslist("a", "b", "c")) )     .append("$or",arrays.<object>aslist(         new basicdbobject("flightdetails.0",             new basicdbobject("$exists",true)         ),         new basicdbobject("flightdetails",             new basicdbobject("$exists",false)         )     )); 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -