I have a system that needs to perform many different kinds of searches, including pagination, so we’re using aggregation queries to get counts and limit results. The same processes are used for single queries and multi-layer queries.
EG:
I have simple-query A which may or may not have a geolocation limitation
I have simple-query B which may or may not have a geolocation limitation
I have a multi-layer query that can contain query A, query B, or both. It also has it’s own geolocation limitation, and a couple of other default criteria.
We use a 2dsphere index for the geolocation limitation because it dramatically improves performance, however, when my multi-layer query contains two geolocation limits (one within the other) and no other non-geolocation criteria, it throws an error: “Intersection requires two index intervals”
The same query works perfectly without the index.
aggregation query:
[
{
$match:
/**
* query: The query in MQL.
*/
{
$and: [
{
/* GLOBAL criteria */
$and: [
{
location: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[-85, 27],
[-85, 30],
[-89, 30],
[-89, 27],
[-85, 27],
],
],
crs: {
type: "name",
properties: {
name: "urn:x-mongodb:crs:strictwinding:EPSG:4326",
},
},
},
},
},
},
{
deleted: false,
},
{
deleted: {
$ne: null,
},
},
],
},
{
$or: [
{
/* layer 1 */
$and: [
{
location: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[-86, 27],
[-86, 28],
[-87, 28],
[-87, 27],
[-86, 27],
],
],
crs: {
type: "name",
properties: {
name: "urn:x-mongodb:crs:strictwinding:EPSG:4326",
},
},
},
},
},
},
{
carType: "TAXI",
},
{
onACall: true,
},
{
onACall: {
$ne: null,
},
},
],
},
],
},
],
},
},
]