1

my collection structure is as follows

{ name : "xyz" , priLoc :{ area : a , country : c } , secondLoc :
[ {area : b ,country : d},{area : b ,country : d} ]}

I want to make query that Oring the area both priLoc and secondLoc and anding the country both priLoc and secondLoc.

I have make query like below

  var query = new BasicDBObject()

   val areaObjList = new BasicDBList
    var primaryArea = new BasicDBObject("priLoc.area", areaList)
   var secondaryArea = new BasicDBObject("secondLoc.area",areaList)
   areaObjList.add(primaryArea)
   areaObjList.add(secondaryArea)
   query.put("$or", areaObjList)

   val countryObjList = new BasicDBList
   val primaryCountry = new BasicDBObject("priLoc.country",countryList)
   val secondaryCountry = new BasicDBObject("secondLoc.country",countryList)
   countryObjList.add(primaryCountry);
   countryObjList.add(secondaryCountry);
   query.put("$or", countryObjList)

but when this this code run areaObjList is replayed by countryObjList so at time only one or object present in query object

I want to make query which and both or object means areaObjList and countryObjList.

how do implement this . or I am making something wrong

if some one knows plz reply.

Thanks

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
Swapnil Sonawane
  • 1,445
  • 3
  • 22
  • 37

2 Answers2

2

$and is a requested feature in mongo and is coming soon https://jira.mongodb.org/browse/SERVER-1089 . By default operators are anded together but in instances like this where you have $or mixed with ands an $and operator is needed.

Michael Papile
  • 6,836
  • 30
  • 30
1

MongoDB does not provide a generic query language as you might expect it from SQL. Basically all query expressions are combined using AND (implictely). You can use $or operator to combine an arbitrary number of expressions. But there is no generic for query something like

(a and b) or (c and d) or not (x and y) etc....

http://www.mongodb.org/display/DOCS/Advanced+Queries

  • And the downvote is for what? Leave a comment or just tell me that I am wrong :) –  Apr 13 '11 at 14:38
  • https://jira.mongodb.org/browse/SERVER-1089?focusedCommentId=21810&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21810 – Lucas Zamboulis Apr 14 '11 at 14:33
  • +1 what was said is correct, it is just that the answerer was unaware that a open ticket was out there for this feature. Also as far as extant software, the poster is correct. Downvote is a bit harsh – Michael Papile Apr 14 '11 at 16:55