GithubHelp home page GithubHelp logo

mrflox / mongo-query-to-postgres-jsonb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thomas4019/mongo-query-to-postgres-jsonb

0.0 0.0 0.0 136 KB

Converts MongoDB queries to postgresql queries for jsonb fields.

License: MIT License

Shell 0.87% JavaScript 97.60% Dockerfile 1.53%

mongo-query-to-postgres-jsonb's Introduction

Mongo-Postgres Query Converter

MongoDB query documents are quite powerful. This brings that usefulness to PostgreSQL by letting you query in a similar way. This tool converts a Mongo query to a PostgreSQL where clause for data stored in a jsonb field. It also has additional converters for Mongo projections which are like select clauses and for update queries.

This tool is used by pgmongo which intends to provide a drop-in replacement for MongoDB.

Installation

npm install mongo-query-to-postgres-jsonb

Simple Usage

var mToPsql = require('mongo-query-to-postgres-jsonb')
var query = { field: 'value' }
var sqlQuery = mToPsql('data', query)

API

var mToPsql = require('mongo-query-to-postgres-jsonb')

mToPsql(sqlField, mongoQuery, [arrayFields])

sqlField

This is the name of your jsonb column in your postgres table which holds all the data.

mongoQuery

An object containing MongoDB query operators.

arrayFields

This tool doesn't know which fields are arrays so you can optionally specify a list of dotted paths which should be treated as an array.

mToPsql.convertSelect(sqlField, projectionQuery, [arrayFields])

projectionQuery

Object specifying which a subset of documents to return. Note: advanced projection fields are not yet supported.

mToPsql.convertUpdate(sqlField, updateQuery, [upsert])

updateQuery

Object containing MongoDB operations to apply to the documents.

upsert

Indicate that the query is being used for upserting. This will create a safer query that works if the original document doesn't already exist.

mToPsql.convertSort(sqlField, sortQuery, [forceNumericSort])

sortQuery

Object containing desired ordering

forceNumericSort

Cast strings to number when sorting.

Examples

Languages MongoDB Postgres
Where { 'names.0': 'thomas' } (data->'names'->>0 = 'thomas')
Where { 'address.city': 'provo' } data @> { "address": '{ "city": "provo" }' }
Where { $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ] } ((data->'qty'>'100'::jsonb) OR (data->'price'<'9.95'::jsonb))
Projection { field: 1 } jsonb_build_object('field', data->'field', '_id', data->'_id')'
Update { $set: { active: true } } jsonb_set(data,'{active}','true'::jsonb)
Update { $inc: { purchases: 2 } } jsonb_set(data,'{purchases}',to_jsonb(Cast(data->>'purchases' as numeric)+2))
Sort { age: -1, 'first.name': 1} data->'age' DESC, data->'first'->'name' ASC

Advanced Select: Match a Field Without Specifying Array Index

With MongoDB, you can search a document with a subarray of objects that you want to match when any one of the elements in the array matches. This tool implements it in SQL using a subquery, so it will likely not be the efficient on large datasets.

To enable subfield matching, you can pass a third parameter which is either an array of dotted paths that will be assumed to potentially be arrays or true if you want it to assume any field can be an array.

Example document:

{
  "courses": [{
      "distance": "5K"
    }, {
      "distance": "10K"
    }]
]

Example queries to match:

mongoToPostgres('data', { 'courses.distance': '5K' }, ['courses'])
mongoToPostgres('data', { 'courses.distance': '5K' }, true)

This then creates a PostgreSQL query like the following:

(data->'courses'->>'distance'='5K'
OR EXISTS (SELECT * FROM jsonb_array_elements(data->'courses')
           WHERE jsonb_typeof(data->'courses')='array' AND value->>'distance'='5K'))

Note: nested paths are not yet supported, so passing ['courses', 'courses.distance'] won't support checking both. The first matching path is the one that will be used.

Supported Features

Todo

Cannot Support

See also

mongo-query-to-postgres-jsonb's People

Contributors

dannyzaken avatar jeniawhite avatar kane-mason avatar mrflox avatar romayalon avatar thomas4019 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.