1

I started to work with MongoDB API, beacause we are using Azure Cosmos DB. Tried examples using $near in MongoDB with the basic structure {key:"A": localtion:{type:"Point", coordinates:[1,2]}}, and works well. The ploblem is when i need to use an array of locations.

I'm triying to execute this query without result. What i'm doing bad?

db.places.insert( {
    id:1,
    name: "AAAAAAAAAAA",
    locals:[
        {
        location: { type: "Point", coordinates: [-73.9928, 40.7191 ] },
        }
    ],
   category: "Parks"
} );   
db.places.insert( {
    id:2,
    name: "BBBBBBBBBBB",
    locals:[
        {
        location: { type: "Point", coordinates: [-73.9928, 40.7193 ] },
        }
    ],
   category: "Parks"
} );
db.places.insert( {
    id:3,
    name: "CCCCCCCCCCCCC",
    locals:[
        {
        location: { type: "Point", coordinates: [  -73.9375, 40.8303 ] },
        }
    ],
   category: "Parks"
} );
//Create index
db.places.createIndex({ "locals.location" : "2dsphere" })
//Query without result
db.places.find(
   {
     "locals.location":
       { $near:
          {
            $geometry: { type: "Point",  coordinates: [ -73.9375, 40.8303 ] },
            $minDistance: 1000,
            $maxDistance: 5000
          }
       }
   }
)

The places has many locals, that's why I can have many objects inside.

I hope some one can help me.

Cheers.

  • 2
    You are using CosmosDB? If so then I don't see this working. There is a supported way for MongoDB with the aggregation framework, but this is not compatible with the "compatibility layer" of CosmosDB. These are two completely different products, despite the marketing hype claiming otherwise. – Neil Lunn Nov 13 '18 at 22:03
  • 1
    See also [How to query $near in CosmosDB via mongoDB protocol](https://stackoverflow.com/q/45631169/2313887). As for "an array of sub-documents with location", I would not recommend it. Your example is trivial ( for MongoDB at least ) but even the unsupported `$geoNear` can only identify at most **one** match from the sub-documents. See [$geoNear matching nearest array](https://stackoverflow.com/a/44281352/2313887) for that one. – Neil Lunn Nov 13 '18 at 22:19
  • Hi,does my answer helps you? – Jay Gong Nov 15 '18 at 01:17
  • Thx @NeilLunn you are right. – Paul Escarcena Nov 15 '18 at 14:01

1 Answers1

0

Based on my test with your sample data, also got empty result as same as you. Per my knowledge, CosmosDB just supports a subset of the MongoDB API and translates requests into the CosmosDB SQL equivalent. CosmosDB has some different behaviours and results. But the onus is on CosmosDB to improve their emulation of MongoDB.

Certainly, you could add feedback here to submit your requirements or consider using MongoDB Atlas on Azure if you'd like full MongoDB feature support.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • Hi, thx for you answer, I´m learning more about CosmosDB and sending an email to azure feedback. I need to find an alternative using any CosmosDB api. Chreers. – Paul Escarcena Nov 15 '18 at 14:00
  • @PaulEscarcenaSantana How about cosmos db sql api? You could use stored procedure or udf to implement your requirements. – Jay Gong Nov 16 '18 at 01:26
  • @PaulEscarcena Sure,you could mark if for answer ,thanks ! – Jay Gong Nov 17 '18 at 06:47