簡體   English   中英

CosmosDB地理空間查詢不返回任何內容

[英]CosmosDB Geospatial query returns nothing

我正在使用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進行測試?

根據文檔本文 ,Azure Cosmos DB自動在每個字段上創建索引。 因此,您不再需要在Node.js代碼中的位置上創建索引。

如果您在不創建任何索引的情況下重新創建集合,這將起作用:

在此處輸入圖片說明

也可以看看:

使用Mongo API的地理空間索引查詢不適用於Azure CosmosDB(DocumentDB)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM