-
-
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
docs embedded in object w/ arbitrary keys? #681
Comments
there is not. |
What about |
@aheckmann - that's what I feared, do you know if this is on the roadmap or if this is just something that isn't widely needed? @TooTallNate - right, that's the default. Problem is with mixed you lose all the validation/schema stuff that you get with supported (named keys & arrays) embedded docs. |
@Ordrin no one has needed it before |
OK, didn't realize I was doing something so esoteric! Damn. |
I have a use case for this, at least I believe it is relevant. I am making an ACL system. My tasks have _ids that are Strings. I want to embed those ids into my member's Member records (an array of tasks they can do). While I can of course just put an array of strings in their "tasks" property, I would, ideally, like to use DBrefs in this scenario so I can pull in metadata. |
@Ordrin I just re-read this. Have you looked at using Mixed? I may have misunderstood b/c i think of "embedded docs" differently than how MongoDB refers to them. Here's generally what you can do with Mixed. var myschema = new mongoose.Schema({
field : mongoose.Schema.Types.Mixed
}); This is pretty close to what you asked for. In this case "field' can be assigned to any value whatsoever. You could just assign it an object literal and add a custom validator to ensure the values are strings. myschema.path('field').validate(yourCustomValidator, 'your custom vaildator failed msg');
function yourCustomValidator (value) {
// check that value is an object etc
return true|false
}
var M = mongoose.model('M', myschema);
var m = new M;
m.field = { might: "work", for: "you" }
m.save(); |
I just wanted to add that I was looking for functionality similar to this. I wanted to have dynamic keys with strongly typed schema values. It would be fantastic if we could avoid having to handroll the validation. new mongoose.Schema({
dynamicKey: {foo: Number, isAwesome: Boolean}
});
or
new mongoose.Schema({
dynamicKey: value is always only a Number
}); |
+1 |
1 similar comment
+1 |
+1. I posted a SO post regarding this issue. If anyone knows of a good way to validate a property with dynamic keys feel free to post. If it was built in I might layout a schema like new Schema({
myDynamicKeyObj: {
type: Dynamic,
key: {type: String, match: /[a-z]*/i},
val: [String]
}
}) |
+1 It would be great to have dynamic keys. If you are to write a json-schema you can do something like for example to match an integer key: "^[0-9]+$": {
"$ref": "#/definitions/mydefinition"
} |
any solution having dynamic keys or still not? |
Same problem. I want do analytics like explained in mongodb docs. http://docs.mongodb.org/ecosystem/use-cases/pre-aggregated-reports/ |
+1 |
2 similar comments
👍 |
+1 |
+1 |
1 similar comment
+1 |
+1 |
6 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
@qqilihq, do implement plx, then we stop +1, kthxbye |
"no one has needed it before" |
Since this is the oldest issue I can find related to this one, I'll re-open this and close #1522 so as many people as possible can track for updates. |
This feature is a MUST. |
How did this feature get released so quietly that I wasn't aware of it?! 💯 |
@adamreisnz lol thanks. Where do you look for Mongoose feature updates? Changelog? Twitter? |
Changelog usually, but must have missed this one! |
Is there a way to use embedded docs within a object that doesn't have predetermined keys? i.e. Rather than having a situation like this:
Being able to say something like:
Makes it a little more array like in that way - but the idea is that I don't know the keys in advance, but I'd still like to be able to have an embedded doc.
The text was updated successfully, but these errors were encountered: