Reduce MongoDB selector query complexity using the commutative, associative and distributive laws by de-separating nested clauses. The output can often times help MongoDB's query planner to chose the best index.
npm install --save @candis/mongo-selector-simplifier
or
yarn add @candis/mongo-selector-simplifier
import { simplify } from "@candis/mongo-selector-simplifier";
const selector = {
$and: [
{ name: { $in: ["Rey", "Leia Organa", "Padmé Amidala", "Jyn Erso"] } },
{ name: "Rey" }
],
gender: "Female"
};
const simplifiedSelector = simplify(selector); // outputs { gender: 'Female', 'name': 'Rey' }
Original work by @gsuess.
mongo-selector-simplifier
is MIT licensed.