In the collection I'm working on, a document looks like this:
{
name: 'Myname',
other: 'other',
stuff: [
['something', 12, 4, 'somethingelse'],
['morestuff', 2, 4, 8],
['finally', 12, 'again', 58],
]
}
I wrote this Mongoose schema to access it:
var MyDocSchema = new Schema({
name: String,
other: String,
stuff: [],
});
When I query a doc, everything works well, the output shown in the console is right. But when, I try to do console.log(myDoc.stuff), I got the following:
['something', 12, 4, 'somethingelse', 'morestuff', 2, 4, 8, 'finally', 12, 'again', 58]
instead of
[
['something', 12, 4, 'somethingelse'],
['morestuff', 2, 4, 8],
['finally', 12, 'again', 58],
]
What am I doing wrong? Thank you for your help!!
http://mongoosejs.com/docs/schematypes.html: Scroll down to the Array section:
Note: specifying an empty array is equivalent to
[Mixed]
. The following all create arrays ofMixed
.
Details on what that means is in the Mixed section right above the Array section.
Define a schema for the embedded documents:
var Stuff = new Schema({
name: String,
value1: Number,
...
});
Use that instead of an empty array []
:
var MyDocSchema = new Schema({
name: String,
other: String,
stuff: [Stuff],
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With