Turistforeningen / node-mongo-querystring

Query builder for accepting URL query parameters into your MongoDB queries. Safe and feature rich. Supports most of MongoDB's query operators such as $eq, $gt, $lt, $ne, $in, $nin, $exists, $regex, geospatial queries such as bbox and near, as well as your own custom query business logic!
MIT License
100 stars 31 forks source link

Want to encapsulate the query string into a single URL parameter #69

Open mozrat opened 7 years ago

mozrat commented 7 years ago

Hello - great project.

My requirements are slightly different to the default behaviour of node-mongo-querystring.

Given a URL like /?mongoquery=name=simon^location=London&page=2&rows=10

I would like to access my Mongo database query just from the mongoquery URL parameter.

I considered using node-mongo-querystring and base64 encoding the query so that it could be encapsulated, but that kills URL readability. Is there a good solution you can think of?

Starefossen commented 7 years ago

Hi @mozrat and thanks for the kind words.

I think this should be possible to achieve but you need to select some other query separator (like $) in order for this to work. Lets consider the following example.

?mongoquery=name=simon$location=London&page=2&rows=10
const qs = require('querystring');

const params = querystring.parse(req.querystring, '&');
console.log(params); // { mongoquery: "name=simon$location=London", page: 2, rows: 10 }

const queries = querystring.parse(params.mongoquery, '$');
console.log(queries); // { name: "simon", location: "London" }