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

Nested projections do not function correctly #25

Closed
whitfin opened this issue Aug 20, 2016 · 4 comments
Closed

Nested projections do not function correctly #25

whitfin opened this issue Aug 20, 2016 · 4 comments
Labels

Comments

@whitfin
Copy link

whitfin commented Aug 20, 2016

If you have a nested projection, it fails to match:

var Mingo = require('mingo');

var db = [{ "name": "Steve", "age": 15, "features": { "hair": "brown", "eyes": "brown" } } ];

Mingo.find(db, {}, { "features.hair": 1 }).all(); // [ { } ]

In MongoDB, this would return [ { "_id" : ObjectId("57b86248bc6b3372ac511267"), "features" : { "hair" : "brown" } } ].

@kofrasa kofrasa added the bug label Aug 21, 2016
@whitfin
Copy link
Author

whitfin commented Aug 22, 2016

So I did some research on this and it seems that the issue is getValue.

var obj = { name: 'Steve',
  age: 15,
  features: { hair: 'brown', eyes: 'brown' } };

var key = 'features.hair';

_.result(obj, key); // undefined

@whitfin
Copy link
Author

whitfin commented Aug 22, 2016

I can hand roll a nested retrieval get/set but I filed another issue about replacing Underscore with Lodash (maybe in a v0.7.0) which would solve this issue.

@kofrasa
Copy link
Owner

kofrasa commented Aug 22, 2016

You are right about the issue being getValue. There is already an internal function which handles nested retrieval correctly called resolve. However, that alone is not sufficient to address the problem. The root cause is that we resolve to the exact value of the given selector and not to the full object graph leading to the value.

For instance given the object

var obj = { "name": "Steve", "age": 15, "features": { "hair": "brown", "eyes": "brown" } };

Currently:

getValue(obj, "features.hair"); // undefined
resolve(obj, "features.hair"); // "brown"

We desire the full object graph leading to the value so that.

resolveObj(obj, "features.hair"); // {"features": {"hair": "brown"}}

I have a working fix for this at the moment, but I still need to do some extra to also handle dropping nested keys which will also not be working correctly.

Concerning lodash however I don't think we gain much switching. Even with their _.get implementation, looking at the example in the docs shows that it is not consistent with MongoDB semantics as required by this project.

@kofrasa
Copy link
Owner

kofrasa commented Sep 23, 2016

Fixed in v0.7.0

@kofrasa kofrasa closed this as completed Sep 23, 2016
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

2 participants