Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$select doesn't work in find? #71

Closed
beeplin opened this issue Mar 22, 2016 · 11 comments
Closed

$select doesn't work in find? #71

beeplin opened this issue Mar 22, 2016 · 11 comments
Labels

Comments

@beeplin
Copy link

beeplin commented Mar 22, 2016

$select doesn't work in find operation. I found that the source code for $select is not the same as that in other packages like feathers-mongodb or feathers-nedb. Normally the code would be like this:

    // $select uses a specific find syntax, so it has to come first.
    if (filters.$select) {
      query = this.Model.find(params.query, getSelect(filters.$select));
    }

but in feathers-mongoose (via github) it is like:

    // $select uses a specific find syntax, so it has to come first.
    if (filters.$select && filters.$select.length) {
      let fields = {};

      for (let key of filters.$select) {
        fields[key] = 1;
      }

      query.select(fields);
    }

and furthermore, in the actually feathers-mongoose npm package (v3.3.6) the source code is:

      // $select uses a specific find syntax, so it has to come first.
      if (filters.$select && filters.$select.length) {
        var fields = {};

        var _iteratorNormalCompletion = true;
        var _didIteratorError = false;
        var _iteratorError = undefined;

        try {
          for (var _iterator = filters.$select[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            var key = _step.value;

            fields[key] = 1;
          }
        } catch (err) {
          _didIteratorError = true;
          _iteratorError = err;
        } finally {
          try {
            if (!_iteratorNormalCompletion && _iterator.return) {
              _iterator.return();
            }
          } finally {
            if (_didIteratorError) {
              throw _iteratorError;
            }
          }
        }

        query.select(fields);
      }
@ekryski
Copy link
Member

ekryski commented Mar 22, 2016

@beeplin how are you passing it in your code? We have tests that cover this that are passing. If needs to be under params.query.$select and you need to make sure your object is structured properly. See this section.

@beeplin
Copy link
Author

beeplin commented Mar 22, 2016

I just cloned feathers-chat project, changed it's service from nedb to mongoose, and modify /public/react-chat/app.jsx L128 to:

    // Find the last 10 messages
    messageService.find({
      query: {
        $select: {
          _id: 0,
          'text': 0
        },
        $sort: { createdAt: -1 },
        $limit: this.props.limit || 10
      }
    }).then(page => this.setState({ messages: page.data.reverse() }));

other stuffs work perfectly including $sort, &skit and #limit, but $select seems to be ignored.

@ekryski
Copy link
Member

ekryski commented Mar 22, 2016

Hmm. ok we'll have to look into it. Unfortunately, it won't happen today.

@ekryski ekryski changed the title [bug?] $select doesn't work in find $select doesn't work in find? Mar 22, 2016
@ekryski ekryski added the bug label Mar 22, 2016
@beeplin
Copy link
Author

beeplin commented Mar 22, 2016

take ur time. :) if this got solved I d like to transform my old project from mongodb to mongoose and enjoy feathers~~

@jamesjnadeau
Copy link
Contributor

I'm not sure if others have noticed this, and it has to do with query parsing in your client, but I found that depending on how your request handler parses the query, and creates the subsequent url, you might have issues that are hard to debug without digging deeper.

@beeplin can you please try the following and see if you get what you are looking for:

// Find the last 10 messages
    messageService.find({
      query: {
        '$select[_id]': 0,
        '$select[text]': 0,
        $sort: { createdAt: -1 },
        $limit: this.props.limit || 10
      }
    }).then(page => this.setState({ messages: page.data.reverse() }));

you're probably going to have to do the same to the $sort variable, or pass it in via a string like:

$sort: '-createdAt'

@daffl
Copy link
Member

daffl commented Mar 23, 2016

I think the docs are wrong. The service tests use an array. Can you try

    messageService.find({
      query: {
        $select: ['_id', 'text' ],
        $sort: { createdAt: -1 },
        $limit: this.props.limit || 10
      }
    }).then(page => this.setState({ messages: page.data.reverse() }));

@daffl
Copy link
Member

daffl commented Mar 23, 2016

If that is the case, removing fields can probably be done best using the remove hook instead.

@daffl
Copy link
Member

daffl commented Mar 23, 2016

This was indeed a mistake in the documentation, sorry about that. It is fixed now and works as I described above.

@beeplin
Copy link
Author

beeplin commented Mar 23, 2016

so this array syntax only works for mongoose or for all other db adapters too?

@marshallswain
Copy link
Member

@beeplin This is part of our shared tests for all adapters: https://github.com/feathersjs/feathers-service-tests/blob/master/src/common-tests.js#L166

So all of them should support it.

@beeplin
Copy link
Author

beeplin commented Mar 23, 2016

got it. Thanks~

来自 魅族 MX5

-------- 原始邮件 --------
发件人:Marshall Thompson notifications@github.com
时间:周三 3月23日 16:10
收件人:feathersjs/feathers-mongoose feathers-mongoose@noreply.github.com
抄送:Beep LIN beeplin@gmail.com
主题:Re: [feathers-mongoose] $select doesn't work in find? (#71)

@beeplin This is part of our shared tests for all adapters: https://github.com/feathersjs/feathers-service-tests/blob/master/src/common-tests.js#L166 So all of them should support it. —You are receiving this because you were mentioned.Reply to this email directly or view it on GitHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants