【问题标题】:CosmosDB Geospatial query returns nothingCosmosDB 地理空间查询不返回任何内容
【发布时间】:2017-12-21 00:38:14
【问题描述】:

我正在使用 MongoDB 界面探索 Azure 的 CosmosDB。我编写了以下测试(使用 Jest)。我的大多数其他测试都可以将 MongoDB url 换成 CosmosDB url,它们将运行并通过。此测试在指向 MongoDB 时通过,但因 CosmosDB url 而失败:

const { MongoClient } = require('mongodb')

let client, db, collection
const url = 'mongodb://localhost:27017'
const dbName = 'test'
describe('talking to mongodb', () => {
  beforeEach(async () => {
    client = await MongoClient.connect(url)
    db = client.db(dbName)
    collection = db.collection('cities')
    await collection.createIndex({ location: '2dsphere' })
  })

  afterEach(async () => {
    await collection.drop()
    client.close()
  })

  it('it can find geocoded things', async () => {
    await collection.insertMany([
      {
        city: 'Ottawa, Canada',
        location: {
          type: 'Point',
          coordinates: [-75.69719309999999, 45.4215296],
        },
        bbox: [-76.35391589999999, 44.962733, -75.2465979, 45.5375801],
      },
      {
        city: 'Moscow, Russia',
        location: { type: 'Point', coordinates: [37.6172999, 55.755826] },
        bbox: [37.3193289, 55.48992699999999, 37.9456611, 56.009657],
      },
    ])

    let cursor = await collection.find({
      location: {
        $near: { type: 'Point', coordinates: [-79.3831843, 43.653226] },
        $maxDistance: 500000,
      },
    })
    let [doc] = await cursor.toArray()

    expect(doc.city).toEqual('Ottawa, Canada')
  })
})

此测试失败并出现以下错误:

  ● talking to mongodb › it can find geocoded things

    MongoError: Request is malformed

      at node_modules/mongodb-core/lib/cursor.js:769:34
      at handleCallback (node_modules/mongodb-core/lib/cursor.js:178:5)
      at setCursorDeadAndNotified (node_modules/mongodb-core/lib/cursor.js:545:3)
      at nextFunction (node_modules/mongodb-core/lib/cursor.js:768:14)
      at node_modules/mongodb-core/lib/cursor.js:665:7
      at queryCallback (node_modules/mongodb-core/lib/cursor.js:263:5)
      at node_modules/mongodb-core/lib/connection/pool.js:542:18

请记住,该查询在 MongoDB 中有效。稍微修改一下,我可以得到一个在 MongoDB 中工作的查询,并且不会在 CosmosDB 中生成错误:

db.cities.find({"location": { $near:{ $geometry: { type: "Point", coordinates: [-79.3831843, 43.653226] }, $maxDistance: 500000 }}})

但是,该查询在 CosmosDB 上没有返回任何内容。

我需要做什么才能让该查询返回结果并让该测试在 CosmosDB 上通过?

【问题讨论】:

    标签: node.js mongodb azure azure-cosmosdb


    【解决方案1】:

    基于the docthis post,Azure Cosmos DB 自动为每个字段创建索引。因此,您不再需要在 Node.js 代码中创建位置索引。

    如果您在不创建任何索引的情况下重新创建集合,这将起作用:

    另请参阅:

    Geospatial index querying doesn't work on Azure CosmosDB (DocumentDB) using the Mongo API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 2021-04-24
      相关资源
      最近更新 更多