Does Firebase Work With Mongoose?
You can use Mongoose with Firebase, but they solve different database problems—Mongoose needs MongoDB while Firebase has its own database, so you're typically choosing one or the other, not both.
Quick Facts
How Firebase Works With Mongoose
Firebase and Mongoose aren't designed to work together because Firebase provides its own real-time database (Firestore or Realtime Database) as a complete backend solution, while Mongoose is an ODM (Object Document Mapper) for MongoDB. However, you can use them in the same project if you architect it correctly: Firebase handles authentication and real-time features, while a separate MongoDB instance with Mongoose manages your structured data. This makes sense when you need Firebase's auth/hosting but prefer MongoDB's schema flexibility and Mongoose's validation layer. The developer experience requires managing two separate database connections—Firebase SDK for auth and real-time syncing, Mongoose for transactional data. You'll typically use Firebase for user management, presence, and notifications, while offloading complex queries and relationships to MongoDB through Mongoose. This adds operational complexity since you're maintaining two databases, but gives you best-of-both-worlds functionality for certain architectures.
Best Use Cases
Quick Setup
npm install firebase mongoose dotenvimport admin from 'firebase-admin';
import mongoose from 'mongoose';
// Initialize Firebase Admin
admin.initializeApp();
// Connect to MongoDB
mongoose.connect(process.env.MONGODB_URI);
// Define Mongoose schema
const userSchema = new mongoose.Schema({
firebaseUid: { type: String, required: true, unique: true },
email: String,
profile: { name: String, avatar: String },
createdAt: { type: Date, default: Date.now }
});
const User = mongoose.model('User', userSchema);
// Middleware: verify Firebase token and sync to MongoDB
export async function syncFirebaseUser(req: any, res: any, next: any) {
const token = req.headers.authorization?.split(' ')[1];
if (!token) return res.status(401).send('Unauthorized');
const decodedToken = await admin.auth().verifyIdToken(token);
const { uid, email } = decodedToken;
// Upsert user in MongoDB
await User.findOneAndUpdate(
{ firebaseUid: uid },
{ firebaseUid: uid, email },
{ upsert: true }
);
req.firebaseUid = uid;
next();
}
// Usage in route
export async function getUser(req: any, res: any) {
const user = await User.findOne({ firebaseUid: req.firebaseUid });
res.json(user);
}Known Issues & Gotchas
Data synchronization conflicts between Firebase and MongoDB
Fix: Establish clear ownership: use Firebase for real-time ephemeral data, Mongoose for canonical persistent data. Implement event-driven sync (Firebase triggers write to MongoDB via Cloud Functions)
Firebase Auth tokens don't automatically validate in MongoDB queries
Fix: Verify Firebase ID tokens in your Node.js middleware before Mongoose operations. Extract uid from token and use it as a filter in queries
Increased operational overhead and cost from dual databases
Fix: Evaluate if a single database (Firestore or MongoDB alone) meets your needs before committing to both
Mongoose schemas add validation latency not present in Firebase's flexible model
Fix: Use Mongoose lean() queries and async validation to minimize performance impact
Alternatives
- •Firestore + Node.js (Firebase's native solution): Eliminates dual-database complexity, has Firebase's ecosystem, but less schema flexibility than Mongoose
- •MongoDB Atlas + Custom Auth (e.g., JWT): Full MongoDB control with Mongoose, but you lose Firebase's managed authentication and real-time features
- •Supabase + Mongoose: PostgreSQL with real-time subscriptions + MongoDB, offers middle ground but adds more operational complexity
Resources
Related Compatibility Guides
Explore more compatibility guides