2013-04-24 87 views
-1

我想收集1,000個文檔,我通過向單個find()查詢的$ in操作符傳遞1,000個唯一_id鍵來指定這些文檔。

這比我運行1,000 find({_ id:})查詢要快多少?

+5

你太多,要快得多。接近1000倍更快。你爲什麼不嘗試並測量它? – 2013-04-24 06:04:00

+0

爲了讓其他NoSQL新手在未來對同一個問題感興趣,我犧牲了低價,並不是每個人都需要時間來獲得答案。嘿。 – 2013-04-24 17:39:10

回答

1

像這樣的東西應該幫助

//create some data (100k documents) 
for(i=0; i<=100000; i++){ 
    db.foo.insert({"_id":i}); 
} 

//generate some ids to search for 
var ids = []; 
for(i=0; i<=1000; i++){ 
    ids.push(i); 
} 

//now let's try the two queries 

//#1 - 1000 separate find queries 

var timer1Start = new Date().getTime(); //start the stopwatch 

ids.forEach(function(i){ 
    var result = db.foo.find({"_id":i}); 
}); 

var timer1End = new Date().getTime(); //stop the stopwatch 

//#2 $in query 
var timer2Start = new Date().getTime(); //start the stopwatch 
var result = db.foo.find({"_id": {$in: ids}}); 
var timer2End = new Date().getTime(); //stop the stopwatch 

print("Function #1 = " + (timer1Start - timer1End)); 
print("Function #2 = " + (timer2Start - timer2End)); 
+1

結果,$ in要快得多 – iwat 2014-12-03 09:12:11