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

Query and construct document with aliases with Model.translateAliases #5338

Merged
merged 4 commits into from
Jun 20, 2017

Conversation

rkt2spc
Copy link
Contributor

@rkt2spc rkt2spc commented Jun 8, 2017

Follow up from #5321
Usage:

var Character = mongoose.model('Character', new mongoose.Schema({
  name: { type: String, alias: '名' },
  bio: {
    age: { type: Number, alias: '年齢' }
  }
}));

// The condition for the 'find' query will become { name: 'Stark', bio: { age: 40 } }
Character
  .find(Charater.translateAliases({
    '名': 'Stark',
    '年齢': 40
  })
  .exec((err, characters) => {})

// Can be use similarily when constructing new document
var char = new Character(Character.translateAliases({
  '名': 'Stark',
  '年齢': 40
});

@rkt2spc rkt2spc closed this Jun 9, 2017
@rkt2spc rkt2spc reopened this Jun 9, 2017
@rkt2spc
Copy link
Contributor Author

rkt2spc commented Jun 10, 2017

Maybe I should put it under Schema instead of Model?

'年齢': 30
}),
// How translated aliases suppose to look like
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure separating out dotted paths is the right behavior here. If you're querying by '年齢', you'd want to query 'bio.age': 30 rather than bio: { age: 30 }, because the latter will only match if bio is an object that contains exactly 1 key, age. On the other hand, 'bio.age': 30 would match even if bio has other keys, for example birthday.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I was over thinking it
Do you prefer that I put it under schema instead of model?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I think schema is actually the better place to put the logic, but there should still be a helper function on model. But this code looks good, I'm gonna merge it into the 4.11 branch

@vkarpov15 vkarpov15 added this to the 4.11 milestone Jun 20, 2017
@vkarpov15 vkarpov15 changed the base branch from master to 4.11 June 20, 2017 20:51
@vkarpov15 vkarpov15 merged commit 49d6e89 into Automattic:4.11 Jun 20, 2017
@bencmbrook
Copy link

Any plans to have querying with aliases without additional query code?
e.g.

Person.findOne({ name: 'Ben' }, ...)

where

var personSchema = new Schema({
  n: {
    type: String,
    alias: 'name'
  }
});

Would be a great way to change schema without breaking existing query code

@rkt2spc
Copy link
Contributor Author

rkt2spc commented Jul 22, 2017

Pre-hooks seems like a good way to implement transparent aliased queries. I'll look on to it next weekend

@vkarpov15
Copy link
Collaborator

Should be easy to implement as a plugin, but haven't tried. I appreciate the suggestion, but please open up a new issue instead of commenting on a closed one 👍

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

Successfully merging this pull request may close these issues.

None yet

3 participants