2017-12-21 5 views
0

MongoDBインタフェースを使用してAzureのCosmosDBを調べています。私は以下のテストを書いた(Jestを使って)。他のほとんどのテストでは、私はCosmosDB URLのMongoDB URLを入れ替えることができ、実行して渡します。このテストは、MongoDBので指されたときに渡し、CosmosDBのURLで失敗します。CosmosDB地理空間クエリは何も返しません

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の中で動作しますが、覚えておいてください。

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

そのクエリがCosmosDBに何も返さないしかし:少しの工夫で、私はMongoDBの中で動作し、CosmosDBにエラーが発生しないクエリを取得することができます。

結果を返すようにクエリを取得し、そのテストをCosmosDBに渡すには、何をする必要がありますか?

答えて

0

Azure Cosmos DBは、the docthis postに基づいて、すべてのフィールドに自動的にインデックスを作成します。したがって、Node.jsコードの場所にインデックスを作成する必要がなくなりました。

これは、あなたがすべてのインデックスを作成せずにコレクションを再作成する場合は動作します:

enter image description here

も参照してください:

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

関連する問題