2017-09-13 47 views
1

有人偷找到一種方法來檢查,如果一個指數具有呼叫_ensureIndex/createIndex之後被創建時沒有使用蒙戈殼牌但在流星服務器的代碼?流星檢查,如果指數一直沒有蒙戈殼牌創建

我正在編寫一個包測試,我想斷言,在某些包代碼執行過程中已經創建了索引。

+0

[rawCollection()](https://docs.meteor.com/api /collections.html#Mongo-Collection-rawCollection)從驅動程序返回underying ['Collection'](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html)實例。因此,列出或測試索引存在的方法有很多種。 –

+0

但是如何?調用'rawCollection()。getIndexes()'不會返回任何東西[不再支持有線Tiger](https://docs.mongodb.com/manual/release-notes/3.0-compatibility/#compatibility-drivers-wired -虎)。 – Jankapunkt

+1

可能因爲實際上沒有這樣的方法。停止閱讀殼牌文件。 NodeJS驅動程序文檔正是我給你的鏈接。你想['indexInformation()'](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#indexInformation)或['listIndexes()'](http:// mongodb .github.io/node-mongodb-native/2.2/api/Collection.html#listIndexes)或甚至['indexes()'](http://mongodb.github.io/node-mongodb-native/2.2/api /Collection.html#indexes)等等等等。當你真正看,還有更多。 –

回答

2

我使用這個代碼來擴展集合原型爲獲得指標同步:

getIndexes.js

const Future = Npm.require('fibers/future'); 

Mongo.Collection.prototype.getIndexes = function() { 
    const raw = this.rawCollection(); 
    const future = new Future(); 

    raw.indexes(function(err, res) { 
    if(err) { 
     future.throw(err); 
    } 
    future.return(indexes); 
    }); 
    return future.wait(); 
}; 
+0

謝謝!如果尼爾沒有發表他的評論作爲答案,我會接受這一個。順便說一句,你是否打算把它作爲流星包發佈?可以結合'createIndex'使用。 – Jankapunkt

+1

@Jankapunkt很高興幫助。不,我不認爲這樣簡單的代碼將被用作包:) – Styx