Mongoose: docs embedded in object w/ arbitrary keys?

Created on 12 Jan 2012  路  40Comments  路  Source: Automattic/mongoose

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.

enhancement

Most helpful comment

This feature is a MUST.

All 40 comments

there is not.

What about Mixed type?

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

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

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

+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?
Example of 2 objects:
{ 123: { firstname: "firstA", lastname: "lastA" } }
{ 124: { firstname: "firstB", lastname: "lastB" } }

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

+1

:+1:

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

I have needed this a well

+1

Stop those meaningless +1 , which end up in watching peoples' inboxes.

There are "Subscribe" and "Reaction" buttons for a reason.

+1

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

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

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!

Was this page helpful?
0 / 5 - 0 ratings