Running multiple queries in mongo`

杀马特。学长 韩版系。学妹 提交于 2019-12-13 09:16:33

问题


I have a collection and want to get a set of results that met a set of conditions. I understand the Mongo doesn't let you use joins so I would need to run separate queries and join the results into a single response.

But is it possible to join the results of separate queries together to get the intended output.

Are there any basic examples I could see query results joined together.

Thanks

For example could I join these two queries so I get the results of both queries:

coll.find({"coordinates.type" : "Point"},{"coordinates" :1}, tailable = True, timeout = False)

and:

coll.find({"place.bounding_box.type" : "Polygon"},{"place.bounding_box.coordinates" : 1}, tailable = True, timeout = False)

回答1:


In your specific example, you do not need to run those queries separately. You can join the results like so:

coll.find(
  { $or : [ 
      { "coordinates.type" : "Point" }, 
      { "place.bounding_box.type" : "Polygon" } 
    ] 
  },
  {"coordinates" :1, "place.bounding_box.coordinates" : 1}
)

You can also use $and / $elementMatch instead of an $or



来源:https://stackoverflow.com/questions/13385986/running-multiple-queries-in-mongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!