Skip to content

Documentation / @andrew_l/search-query-language / parseToMongo

Function: parseToMongo()

parseToMongo(input, options): Record<string, any>

Parses a query string and converts it into a MongoDB-compatible query object.

Parameters

input: string

The query string to be parsed.

options: ParseToMongoOptions = {}

Returns

Record<string, any>

  • The MongoDB query representation.

Examples

ts
const query = parseToMongo('age > 30');
console.log(query);
// { age: { $gt: 30 } }
ts
const query = parseToMongo('name = "Alice" AND age > 18');
console.log(query);
// { $and: [{ name: 'Alice' }, { age: { $gt: 18 } }] }
ts
const query = parseToMongo('_id = "67d737b73af3ff3e00a3bbf1"', {
  transform: {
    _id: [MONGO_TRANSFORM.OBJECT_ID, MONGO_TRANSFORM.NOT_NULLABLE]
  }
});
console.log(query);
// { $and: [{ name: 'Alice' }, { age: { $gt: 18 } }] }