如何在mongodb中执行oring的anding
我的集合结构如下
{ name : "xyz" , priLoc :{ area : a , country : c } , secondLoc :
[ {area : b ,country : d},{area : b ,country : d} ]}
我想查询 priLoc 和 secondaryLoc 区域 以及 priLoc 和 secondaryLoc 两个国家。
我已经进行了如下查询
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)
,但是当此代码运行 areaObjList 时,countryObjList 会重播 因此,有时查询对象中仅存在一个或对象,
我想进行查询,其中两个或对象意味着areaObjList和 国家对象列表。
如何实现这个。或者我做错了什么,
如果有人知道请回复。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$and 是 mongo 中请求的功能,即将推出 https://jira.mongodb.org/browse /SERVER-1089 。默认情况下,运算符是与在一起的,但在像这样的情况下,如果您将 $or 与 and 混合,则需要 $and 运算符。
$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 不提供您可能期望 SQL 提供的通用查询语言。基本上所有查询表达式都使用 AND(隐式)进行组合。您可以使用 $or 运算符组合任意数量的表达式。但是没有通用的查询,例如
(a和b)或(c和d)或不是(x和y)等...
http://www.mongodb.org/display/DOCS/Advanced+Queries
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