Registry / database / mongo-query-to-postgres-jsonb

mongo-query-to-postgres-jsonb

JSON →
library0.2.19jsnpmunverified

Converts MongoDB query documents to PostgreSQL WHERE clauses for JSONB fields. Supports a wide range of MongoDB operators ($or, $and, $gt, $lt, $in, $regex, etc.), projections, updates ($set, $inc, $unset, $push, $pull, etc.), and sort. Current version 0.2.19 is stable and used by pgmongo. Differentiator: provides a drop-in-like experience for MongoDB developers querying JSONB columns in Postgres.

npm install mongo-query-to-postgres-jsonb
INSTALL
IMPORT
SIG · MONGO-QUERY-TO-POS
M
mongo-query-to-postgres-jsonb
databasejavascriptv0.2.19
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import mToPsql from 'mongo-query-to-postgres-jsonb';
const mToPsql = require('mongo-query-to-postgres-jsonb');
ESM import works; the library also supports CommonJS require().
convertSelect
import { convertSelect } from 'mongo-query-to-postgres-jsonb';
const convertSelect = require('mongo-query-to-postgres-jsonb').convertSelect;
Named export available for tree-shaking.
convertUpdate
import { convertUpdate } from 'mongo-query-to-postgres-jsonb';
const convertUpdate = require('mongo-query-to-postgres-jsonb').convertUpdate;
Named export for update conversion.
convertSort
import { convertSort } from 'mongo-query-to-postgres-jsonb';
Available as named export.

Converts a MongoDB query with $gte, $or, and equality to a parameterized PostgreSQL WHERE clause for a JSONB column.

import mToPsql from 'mongo-query-to-postgres-jsonb'; const jsonbField = 'data'; const mongoQuery = { name: 'Alice', age: { $gte: 18 }, $or: [ { city: 'New York' }, { city: 'Los Angeles' } ] }; const { where, params } = mToPsql(jsonbField, mongoQuery); console.log('Where clause:', where); console.log('Parameters:', params); // Example output: // Where clause: (data->>'name' = $1) AND (data->'age' >= $2::jsonb) AND ((data->>'city' = $3) OR (data->>'city' = $4)) // Parameters: ['Alice', '18', 'New York', 'Los Angeles']
Debug
Known issues
gotchaArray fields must be explicitly passed in arrayFields parameter; otherwise operators like $elemMatch may produce incorrect results.
fix
Provide arrayFields option with dot-separated paths to array fields.
affects: >=0.1.0
gotchaUnsupported MongoDB operators (e.g., $nearSphere, $geoWithin) throw errors. Not all query operators are implemented.
fix
Check the list of supported operators in the documentation; avoid geo operators.
affects: >=0.1.0
deprecatedThe old API returning a string without parameters is deprecated. Always use the object form to get parameterized queries.
fix
Use the object return value: const { where, params } = mToPsql(...).
affects: >=0.2.0
gotchaRegex queries with flags (e.g., /pattern/i) must be passed as objects { $regex: 'pattern', $options: 'i' } instead of RegExp objects.
fix
Use the explicit $regex and $options syntax.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: mToPsql(...) is not a function
Incorrect import: using destructured named import when default is needed.
fix
Use 'import mToPsql from "mongo-query-to-postgres-jsonb"' (default import).
Cannot find module 'mongo-query-to-postgres-jsonb'
Package not installed or typo in package name.
fix
Run 'npm install mongo-query-to-postgres-jsonb' or check spelling.
Unsupported operator: $nearSphere
MongoDB geo operators are not implemented in this library.
fix
Remove unsupported operators or implement custom handling manually.
Upgrade
Version history
0.2.19latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
mongo-query-to-postgres-jsonb — npm install mongo-query-to-postgres-jsonb · libregistry