-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Ability to add object-arrays to schema #2376
Comments
I agree with this as it would save a lot of code duplication when repeating the same structure for different keys. |
Is there any workaround on this now? |
Not quite, and no real ETA for one unfortunately. Right now the best way to do this would be an array and the |
Not sure how to upvote this as a "yes please" feature, but, 'yes please'. I'm wanting to store custom form fields in one document and their values in another, and being able to look up values : {
[key] : { ... schema for value validation ... }
} instead. Similar to OP's request, but for speed of look up of dynamic content in the DB, rather than to avoid copy pasting schema blocks, which I -can't- do in current situation |
@rcuddy if you're worried about performance, multi-key index and $elemMatch would work about as well performance-wise. Otherwise, you can just wait for #2689, which will fix this. |
@vkarpov15 hi, I need something similar to what @grabbou requested (object-arrays) and I'm probably going to try adding it as a plugin but I want to make sure it's doable before starting (of course unless you planned to add this in the near future :) ):
Thank! |
In theory you could use Re: point 2, Re: point 3, don't really know what the difficulties are off the top of my head. I haven't done any work on this yet though. Let me know how your attempts at a plugin go :) |
I think I'm going to try this approach: slightly modify the
|
A custom type is an interesting avenue that I had not really considered, good idea :) |
My idea was using Mixed with a custom validator but I'm wondering of 2015-04-16 15:36 GMT+02:00, Valeri Karpov notifications@github.com:
Matteo Pagliazzi - paglias.net |
If you have a thousand subdocs you should really watch out for the 16MB doc size limit - just because it isn't an array doesn't mean that it's safe to have a document size that grows without bound :) |
Hi @vkarpov15, sorry for the tag but this is such an old issue that it has fallen out of view :) We have an application where users can create custom fields and then attach them to existing documents, right now we are using const CustomFieldSchema = new Schema({
name: { type: String },
options: { ... },
})
const OtherSchema = new Schema({
...
customFields: {
[Schema.Keys.String]: { // that's just a supposition
customField: { type: ObjectId, ref: 'CustomField' },
value: { type: String },
},
},
}) We tried the array way but it gets really messy when using const CustomFieldSchema = new Schema({
name: { type: String },
options: { ... },
})
const OtherSchema = new Schema({
...
customFields: [{
customField: { type: ObjectId, ref: 'CustomField' },
value: { type: String },
}]
}) The first example is what we would like to have, but since we miss this "generic key" feature we end up with: const CustomFieldSchema = new Schema({
name: { type: String },
options: { ... },
})
const OtherSchema = new Schema({
...
customFields: { type: Mixed },
}) It works, but having mongoose magic on those subdocuments would be awesome! As always, thanks for your time |
@andreafdaf I think you're looking for maps, here's some more docs on maps. |
Oh my god that's amazing, thank you so much! I guess I need to read release notes more often, that's a great feature that I never noticed But now there is another question, how can I populate a map since the keys can be anything? const OtherSchema = new Schema({
...
customFields: [{
customField: { type: ObjectId, ref: 'CustomField' },
value: { type: String },
}]
}) We can simply: OtherModel.find(query).populate('customFields.customField') But with: const OtherToCustomFieldSchema = new Schema({
customField: { type: ObjectId, ref: 'CustomField' },
value: { type: String },
}, { _id: false })
const OtherSchema = new Schema({
...
customFields: { type: Map: of: OtherToCustomFieldSchema },
}) How do we: OtherModel.find(query).populate('customFields.(???).customField') |
|
It'd be useful to have the ability to force specific values and keys on all properties inside your object.
In my use-case, I have
provider
property attached to my main schema that contains multiple providers in it (Facebook, google+ and other passport goodies). The problem is that I don't want to use arrays as dot noted access matters here.Here's the current snippet of code:
It'd be super handy to do something like:
Kind of similar to #681, but that issue is only speaking about validation for keys, not about ability to have multiple properties. Validation of the key is actually not that important for me actually, can be anything. Should be faster to develop then.
What you think?
The text was updated successfully, but these errors were encountered: