如何在mongodb中执行oring的anding

发布于 2024-11-01 00:49:11 字数 1056 浏览 0 评论 0原文

我的集合结构如下

{ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡笑忘祈一世凡恋 2024-11-08 00:49:11

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

长亭外,古道边 2024-11-08 00:49:11

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文