2017-08-06 91 views
0

我對mongodb中數據的索引工作方式感到有點困惑。貓鼬查詢索引數據

例如,我有所謂的經驗與各地的15000 productfeeds集合:

{ "_id" : ObjectId("597df357c6622963cc3d834e"), "userImg" : false, "category" : "Test" } 

現在我用MongoDB的索引像這樣的類別:

db.experiences.createIndex({ category: 1 }) 

然後我查詢的索引收集是這樣的:

var query = {}; 
query.category = categoryVal; 
query.price = 100; 
Experiences.find(query).lean().exec(function (err, rec) { 
       if(err) 
        console.log('Error at records: '+err); 
       else { 
        console.log(rec); 
} 

這裏我不確定這是否工作或不?我正在獲取檢索的數據,但性能與以前相同。它出什麼問題了?

+0

你可以看到這個答案https://stackoverflow.com/questions/44519353/whats-the-strategy-for-creating-index-for-this-query/44520558#44520558 –

回答

0

要驗證您的查詢是否使用索引,請使用來自mongo shell的查詢解釋。

explain計劃會給你看到後面發生的事情得到你的數據。

+0

好吧,可以結合瘦()和explain()。它有什麼不同? –

+0

您可能需要檢查自己在貓鼬的API中 – Astro