开发者

how to perform anding of oring in mongodb

开发者 https://www.devze.com 2023-02-25 10:16 出处:网络
my collection structure is as follows { name : \"xyz\" , priLoc :{ area : a , country : c } , secondLoc :

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 开发者_Go百科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


$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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消