Data ရှာဖွေခြင်း (Query)
Firestore query ကိုသုံးရင် condition နဲ့ကိုက်တဲ့ documents တွေကို ဖတ်နိုင်ပါတယ်။ ဥပမာ login user ပိုင်တဲ့ notes တွေပဲဖတ်ခြင်း၊ pinned notes တွေပဲပြခြင်း၊ newest first စီခြင်းတို့ကို query နဲ့လုပ်ပါတယ်။
Code Example
javascript
import { getFirestore, collection, query, where, orderBy, limit, getDocs } from 'firebase/firestore';
const db = getFirestore();
async function getPinnedNotes(userId) {
const q = query(
collection(db, 'notes'),
where('ownerId', '==', userId),
where('isPinned', '==', true),
orderBy('createdAt', 'desc'),
limit(10)
);
const snapshot = await getDocs(q);
return snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data()
}));
}ဒီ code က ဘာလုပ်တာလဲ?
where နဲ့ condition စစ်ပြီး orderBy နဲ့ စီပါတယ်။ limit က result အရေအတွက်ကိုကန့်သတ်ပေးလို့ performance ပိုကောင်းပါတယ်။
Index လိုနိုင်သည်
Condition များပြီး sort ပါလာတဲ့ query တွေမှာ Firestore index လိုအပ်နိုင်ပါတယ်။ Console error ထဲမှာ index တည်ဆောက်ရန် link ပေးတတ်လို့ အဲ့ဒီ link ကနေတည်ဆောက်နိုင်ပါတယ်။