2017-10-13 37 views
0

我需要知道一個對象是否存在於我正在使用的db中。我知道我可以使用query.count來測試結果是否爲零,否則它不存在。但有人告訴我,計數很貴,所以我認爲query.first可能更有效。這是真的,測試db中對象存在的最好方法是什麼?使用find,count或first in parse來測試是否存在

回答

0

查詢首先將不計入你的結果,你可以使用查詢發現,然後如果使用array.length測試數組的長度大於0或不

+0

我需要測試存在只有我不需要結果數 –

0

這將是有益的,如果你表現出一定的代碼,但是這是太好玩了一個問題就過去了。

測試代碼在下面,但我的非科學答案是:只要查詢索引良好,它可能無所謂你使用哪種方法。當你計算大量的對象時,計數是很昂貴的,但如果你只是想找到一個並且查詢被索引,我認爲它不會很昂貴。

好的,現在有些代碼。我在我的parse-server repo中編譯了一些單元測試,以測試我能想到的三種測試存在的方式。在所有三種情況下,我使用的可能是特殊的objectId,因此您可以輕鬆調整它。我也只做了一次每個測試,而要做到科學性,我們想每次做100次,平均每次測試。但是之後我們需要考慮查詢緩存,所以我只是保持簡單,並且可以使用相同的模式對自己的數據進行真實測試。

好了,現在我已經給了我所有的注意事項:

describe('way to find if an object exists....', function() { 

    beforeEach(function (done) { 
    new Parse.Object('Test') 
     .save() 
     .then((testObj) => { 
     this.testObj = testObj; 
     done(); 
     }) 
     .catch(done.fail); 
    }); 

    it('should use get if we know object id', function(done) { 
    this.start = new Date(); 
    new Parse.Query('Test').get(this.testObj.id) 
     .then(result => { 
     console.log('get time exists', new Date() - this.start); 
     return expect(result.id).toBe(this.testObj.id); 
     }) 
     .then(() => { 
     this.start = new Date(); 
     return new Parse.Query('Test').get('not the id'); 
     }) 
     .then(
     () => done.fail('we shouldn\'t get here, cause the obj doesn\'t exist'), 
     // so if the get is rejected you know it doesn't exist. 
     e => { 
      console.log('get time does not exist', new Date() - this.start); 
      expect(e).toEqual(new Parse.Error(101, 'Object not found.')); 
     }) 
     .then(done) 
     .catch(done.fail); 
    }); 

    it('should also work with find', function (done) { 
    this.start = new Date(); 
    new Parse.Query('Test') 
     .equalTo('objectId', this.testObj.id) 
     .find() 
     .then(result => { 
     console.log('find time exists', new Date() - this.start); 
     return expect(result.length).toBe(1); 
     }) 
     .then(() => { 
     this.start = new Date(); 
     return new Parse.Query('Test') 
      .equalTo('objectId', 'not the id') 
      .find(); 
     }) 
     .then((result) => { 
     console.log('find time does not exist', new Date() - this.start); 
     expect(result.length).toEqual(0); 
     }) 
     .then(done) 
     .catch(done.fail); 
    }); 

    it('should also work with count', function (done) { 
    this.start = new Date(); 
    new Parse.Query('Test') 
     .equalTo('objectId', this.testObj.id) 
     .count() 
     .then(result => { 
     console.log('count time exists', new Date() - this.start); 
     return expect(result).toBe(1); 
     }) 
     .then(() => { 
     this.start = new Date(); 
     return new Parse.Query('Test') 
      .equalTo('objectId', 'not the id') 
      .count(); 
     }) 
     .then((result) => { 
     console.log('count time does not exist', new Date() - this.start); 
     expect(result).toEqual(0); 
     }) 
     .then(done) 
     .catch(done.fail); 
    }); 
}); 

這將產生這樣的結果:

Jasmine started 

    way to find if an object exists.... 

    ✓ should use get if we know object id 
    get time exists 19 
    get time does not exist 8 

    ✓ should also work with find 
    find time exists 12 
    find time does not exist 9 

    ✓ should also work with count  
    count time exists 7 
    count time does not exist 6 

所以在我的漂亮的空,在內存蒙戈,其所有的超級速度極快(那些是毫秒),所以,假設你的查詢索引很好,選擇一個產生最可讀代碼的那個;)

相關問題