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

How to set a fields to be inside a key of nested array. #3

Open
deremer opened this issue May 29, 2014 · 3 comments
Open

How to set a fields to be inside a key of nested array. #3

deremer opened this issue May 29, 2014 · 3 comments

Comments

@deremer
Copy link

deremer commented May 29, 2014

Great plugin! Loving it. This not an issue, unless it's not possible, in which case it's a feature request. Had a quick question about updating embedded arrays...

Say you've got a standard User model, and the following:

var VoteSchema = mongoose.Schema({
  vote              : { type: Number }
});

var ItemSchema = mongoose.Schema({
  text              : { type: String, },
  votes             : [VoteSchema]
});

VoteSchema.plugin(filler, {
  ref  : 'User',
  dest : 'Claim',
  pos  : 'votes.$.',
  select : 'firstName lastName'
});

This works wonderfully (thanks again!), but it returns the user fields at the same level of 'vote'. So you get this...

votes = [
 { firstName: 'blah', lastName: 'blah', vote: 1 }
]

Is it possible to make it sync the user fields behind a 'user' key, like this:

votes = [
 { user: { firstName: 'blah', lastName: 'blah'}, vote: 1 }
]

Thanks for your help.

@pgherveou
Copy link
Owner

Did you try this?

VoteSchema.plugin(filler, {
  ref  : 'User',
  dest : 'Claim',
  pos  : 'votes.$.user',
  select : 'firstName lastName'
});

@deremer
Copy link
Author

deremer commented May 29, 2014

I tried that first before I entered the issue (should’ve mentioned it). Unfortunately it doesn’t work, unless I’m missing a key part of the process.

If I just add “ pos  : 'votes.$.user’ “ then actually it doesn’t bring any of the user fields.

If I add “ user : {} “ or “ user : mongoose.Schema.Types.Mixed “ to the VoteSchema, then when I save the user _id, it is saved, but none of the user fields are synced...

Perhaps I’m doing something wrong?

Thanks for your help.

On May 29, 2014 at 5:15:50 PM, PG Herveou ([email protected]) wrote:

Did you try this?

VoteSchema.plugin(filler, {
ref : 'User',
dest : 'Claim',
pos : 'votes.$.user',
select : 'firstName lastName'
});

Reply to this email directly or view it on GitHub.

@pgherveou
Copy link
Owner

if you copy a working script here, I can check it out
you can also enable debugging on mongoose to see what's going on

 require('mongoose').set('debug', true)

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

No branches or pull requests

2 participants