如何在parsing中使用includeKey查询“Or”?

所以,我在Parse and Place模型中有Event模型。 每个模型都有地方。 另外我有用户,每个事件都有主人。 所以,我需要在我的位置周围10英里的地方获得我的活动或事件,以获得我使用的活动

let query = Event.query() query.whereKey("author", containedIn: [PFUser.currentUser()!]) query.includeKey("place") 

它的工作,但现在我需要添加OR操作,并find我使用的10英里的事件

  let placeQuery = PFQuery(className: "Place") placeQuery.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude), withinMiles: 20.0) 

而我怎么需要主查询使用这两个? 我努力了

  var resultQuery:PFQuery = PFQuery.orQueryWithSubqueries([query, placeQuery]) 

但它给了我一个错误,orQueryWithSubqueries需要使用相同的类

目前你有一个查询返回一个事件列表,然后返回一个地方列表的查询。

这就是为什么你得到错误。

他们都需要返回相同的types。 然后你可以把它们“或”在一起。

喜欢这个…

 let authorQuery = Event.query() authorQuery.whereKey("author", containedIn: [PFUser.currentUser()!]) // note I'm using the "place.location" path to refer to the location key of the place key. let placeQuery = Event.query() placeQuery.whereKey("place.location", nearGeoPoint: geoPoint, withinMiles: 20.0) 

只有这样才能在复合查询中包含密钥。 包含键在子查询上使用时没有效果。

 let resultQuery:PFQuery = PFQuery.orQueryWithSubqueries([authorQuery, placeQuery]) resultQuery.includeKey("place") 

这将返回每个对象中填充Place键的事件列表。

编辑

进一步阅读parsing文档显示复合查询不支持各种各样的东西…

在这里输入图像说明

请注意,我们不支持复合查询的子查询中的GeoPoint或非过滤约束(例如nearGeoPoint,withinGeoBox …:,limit,skip,orderBy …:,includeKey :)。

看起来你将不得不为此创build一个云function。

使用云function,您可以传递位置并运行两个单独的查询,然后在返回之前将它们合并到现在的数组中。

你必须使用云代码的东西写在Javascript中。

编辑2

事实上,你可以试试这个…

 let authorQuery = Event.query() authorQuery.whereKey("author", containedIn: [PFUser.currentUser()!]) // note I'm using the "place.location" path to refer to the location key of the place key. let placeQuery = Place.query() placeQuery.whereKey("location", nearGeoPoint: geoPoint, withinMiles: 20.0) let eventPlaceQuery = Event.query() eventPlaceQuery.whereKey("place", matchesQuery: placeQuery) let resultQuery:PFQuery = PFQuery.orQueryWithSubqueries([authorQuery, eventPlaceQuery]) resultQuery.includeKey("place") 

这可能有相同的限制,不允许你创build它,但是值得一试。 :d

Interesting Posts