lucasrochagit / nest-mongo-query-parser

A MongoDB query string parser to be used in applications developed with NestJS.
Apache License 2.0
12 stars 8 forks source link

search not working with me #6

Closed hatemragab closed 1 year ago

hatemragab commented 1 year ago
{{baseUrl}}/channel/{{rId}}/message?mT=_dsff

it generate

MongoQueryModel {
  limit: 100,
  skip: 0,
  select: {},
  sort: {},
  populate: [],
  filter: { mT: '_dsff' }
}
lucasrochagit commented 1 year ago

Hello, thanks for your collab.

I don't understand the reason why the search doesn't works for you. As I wrote in the lib documentation, on rules topic:

Anything other than limit, skip, page, sort, select and populate will be considered a filter;

So if you type a query where the key is mT and the value is _dsff, the lib will consider this search as a filter, so in the field filter you will get the filter as { mT: '_dsff' }. You can use it for get some data in the database, using the prop filter from the MongoQueryModel. For example:

@Injectable()
export class MessageService {
  constructor(
    @InjectModel(Message.name)
    private readonly _model: Model<MessageDocument>,
  ) {}

  async find(query: MongoQueryModel) {
    return this._model
      .find(query.filter) // in this case, .find ({ mT: '_dsff' })
      .limit(query.limit) // in this case, .limit(100)
      .skip(query.skip) // in this case, .skip(0)
      .sort(query.sort) // in this case, .sort({})
      .select(query.select) // in this case, .select({})
      .exec();
  }
}