In short
MongoDB doesn't use SQL, but it can still be injected. If a login route passes req.body straight into a query, an attacker can send an operator instead of a password and walk right in.
If you pass req.body straight into a MongoDB query, an attacker can send operators like { "$ne": null } to bypass checks. Validating types and sanitizing keys prevents this.

Think of it like this
The master key trick
A hostel warden asks, "Which room key do you need?" A normal student says "Room 12." A trickster says "Any room that is not empty," and a careless warden hands over a master key. Sending { "$ne": null } as a password is that trick: it asks MongoDB for any password that is not null.
Watch it happen
An injection attempt on a weak login
POST /login
1/4Body: { "email": "admin@shop.in", "password": { "$ne": null } }
The big ideas
What you need to know
Check types first
Email and password must be strings. Reject objects and arrays with a validator like zod or express-validator.
Build filters yourself
Use { email: String(email) } with named fields. Never pass req.body or req.query as a whole filter.
Never query by password
Find the user by email, then bcrypt.compare the typed password with the stored hash.
Mongoose sanitizeFilter
mongoose.set("sanitizeFilter", true) wraps nested $ operators in query filters with $eq. Use it as an extra layer, not the only one.
See it in code
Walk through the code
1import { z } from "zod";2import bcrypt from "bcrypt";3import User from "../models/User.js";4 5const loginSchema = z.object({6 email: z.string().email().max(254),7 password: z.string().min(8).max(128),8});9 10export async function login(req, res, next) {11 try {12 const parsed = loginSchema.safeParse(req.body);13 if (!parsed.success) {14 return res.status(400).json({ message: "Invalid email or password format" });15 }16 const { email, password } = parsed.data;17 18 const user = await User.findOne({ email: email.toLowerCase() }).select("+password");19 const ok = user && (await bcrypt.compare(password, user.password));20 if (!ok) return res.status(401).json({ message: "Wrong email or password" });21 22 sendAuthCookie(res, user);23 res.json({ id: user.id, name: user.name, role: user.role });24 } catch (err) {25 next(err);26 }27}1/4The schema demands strings. { "$ne": null } fails here with a 400.
A login route that injection cannot bypass: typed input, named filter, hashed comparison.
Save yourself the pain
Common mistakes
Oops
User.findOne({ email: req.body.email, password: req.body.password }).
Do this instead
Never store or query plain passwords. Find by email, then bcrypt.compare.
Oops
Passing req.query straight into Product.find(req.query) for filters.
Do this instead
Whitelist allowed fields and cast each value: { category: String(req.query.category) }.
Oops
Thinking MongoDB is safe because it has no SQL.
Do this instead
Operators like $ne, $gt and $where can be injected through JSON. Validate every input.
Your mission
Attack and fix your own login
Quick check
Which single change most directly stops the { "$ne": null } password trick?
Go deeper
- 1Try a login request with { "email": { "$ne": null } } against a careless route to see the risk.
- 2Ensure email and password are strings before querying.
- 3Use Mongoose's sanitizeFilter option or a sanitizing middleware.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.