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

docs embedded in object w/ arbitrary keys? #681

Closed
ghost opened this issue Jan 12, 2012 · 40 comments
Closed

docs embedded in object w/ arbitrary keys? #681

ghost opened this issue Jan 12, 2012 · 40 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@ghost
Copy link

ghost commented Jan 12, 2012

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:

var myschema = new mongoose.Schema({
   field : { foo: String, bar: String }
});

Being able to say something like:

var myschema = new mongoose.Schema({
   field : { *: String }
});

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.

@aheckmann
Copy link
Collaborator

there is not.

@TooTallNate
Copy link
Contributor

What about Mixed type?

@ghost
Copy link
Author

ghost commented Jan 12, 2012

@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.

@aheckmann
Copy link
Collaborator

@Ordrin no one has needed it before

@ghost
Copy link
Author

ghost commented Jan 12, 2012

OK, didn't realize I was doing something so esoteric! Damn.

@bingomanatee
Copy link

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.

@aheckmann
Copy link
Collaborator

@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();

http://mongoosejs.com/docs/schematypes.html

@Akkuma
Copy link

Akkuma commented Oct 11, 2012

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
});

@Rob-ot
Copy link

Rob-ot commented Nov 15, 2013

+1

1 similar comment
@chopachom
Copy link
Contributor

+1

@RyanQuackenbush
Copy link

+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] 
  } 
})

@fdelbos
Copy link

fdelbos commented Mar 29, 2014

+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"
}

@dutu
Copy link

dutu commented Apr 7, 2014

any solution having dynamic keys or still not?
Example of 2 objects:
{ 123: { firstname: "firstA", lastname: "lastA" } }
{ 124: { firstname: "firstB", lastname: "lastB" } }

@vinz243
Copy link

vinz243 commented Apr 22, 2014

Same problem. I want do analytics like explained in mongodb docs. http://docs.mongodb.org/ecosystem/use-cases/pre-aggregated-reports/

@ashaffer
Copy link
Contributor

+1

2 similar comments
@TedAvery
Copy link

TedAvery commented Jul 2, 2014

👍

@eXon
Copy link

eXon commented Jul 26, 2014

+1

@adam8810
Copy link

+1

1 similar comment
@rapee
Copy link

rapee commented Oct 13, 2014

+1

@coty-crg
Copy link

coty-crg commented Jul 6, 2015

+1

6 similar comments
@tieblumer
Copy link

+1

@rubenstolk
Copy link

+1

@rohithn
Copy link

rohithn commented Jul 5, 2016

+1

@Elements-
Copy link

+1

@maxskorr
Copy link

maxskorr commented Jun 3, 2017

+1

@voodee
Copy link

voodee commented Jul 25, 2017

+1

@coty-crg
Copy link

+1

@raub
Copy link

raub commented Sep 27, 2017

@qqilihq, do implement plx, then we stop +1, kthxbye

@Flicksie
Copy link

Flicksie commented Nov 2, 2017

"no one has needed it before"
5 years later, a ton of +1s later, still no updates on this?

@vkarpov15
Copy link
Collaborator

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.

@doneumark
Copy link

doneumark commented Mar 10, 2018

This feature is a MUST.

vkarpov15 added a commit to mongoosejs/mpath that referenced this issue Mar 26, 2018
vkarpov15 added a commit that referenced this issue Mar 27, 2018
vkarpov15 added a commit that referenced this issue Mar 30, 2018
vkarpov15 added a commit that referenced this issue Apr 1, 2018
vkarpov15 added a commit that referenced this issue Apr 12, 2018
vkarpov15 added a commit that referenced this issue Apr 13, 2018
vkarpov15 added a commit that referenced this issue Apr 16, 2018
vkarpov15 added a commit that referenced this issue Apr 17, 2018
@adamreisnz
Copy link
Contributor

How did this feature get released so quietly that I wasn't aware of it?! 💯

@vkarpov15
Copy link
Collaborator

@adamreisnz lol thanks. Where do you look for Mongoose feature updates? Changelog? Twitter?

@adamreisnz
Copy link
Contributor

Changelog usually, but must have missed this one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests