moleculerjs / moleculer-db

:battery: Database access service mixins for Moleculer
https://moleculer.services/
MIT License
152 stars 122 forks source link

mongo adapter - can't sort result with text search #123

Open hawkins48b opened 5 years ago

hawkins48b commented 5 years ago

Prerequisites

Create a collection with basic data

broker.call('users.create', {
  username: "user01",
  name: "awesome user",
  zip: "54321"
}).then(res => console.log(res));

broker.call('users.create', {
  username: "user02",
  name: "awesome person",
  zip: "12345"
}).then(res => console.log(res));

Current Behavior

Perform a textual search with method 'find' or 'list'

broker.call('users.list', {
  search: "awesome",
  sort: 'zip'
}).then(res => console.log(res));

Does not sort results at all.

[
  {
    _id='kB4WHa8EFg3qsKYScXtXPyPP',
    username: "user01",
    name: "awesome user",
    zip: "54321"
  },
  {
    _id='cvWFC4MSWqFBxSYd4mwuBSVw',
    username: "user02",
    name: "awesome person",
    zip: "12345"
  }
]

Expected Behavior

We should be able to sort data with a text search.

As stated by the documentation, list should be able to perform a textual search and sort : https://moleculer.services/docs/0.13/moleculer-db.html#find https://moleculer.services/docs/0.13/moleculer-db.html#list

Again, I found out that the base adapter (memory) works. It is related to mongo. As a workaround, I use the 'find' method and sort results myself.

Steps to reproduce

Use the official example as a base : packages/moleculer-db-adapter-mongo/examples/search/index.js

Context

hawkins48b commented 5 years ago

Issue is a follow up of https://github.com/moleculerjs/moleculer-db/issues/122

ChangJoo-Park commented 4 years ago

same here.

I use moleculer-db with mongodb adapter. sort not working well :(

2019-12-11T02:18:57.521Z 2019-12-10T12:05:47.189Z 2019-12-10T10:21:48.368Z 2019-12-10T10:18:47.660Z 2019-12-10T10:25:25.689Z 2019-12-10T10:22:25.935Z