Mongoose error: nesting schemes

I have a question about nesting a Mongoose schema.

Here is a simple code snippet

var aSchema = new Schema({bar: String});
var bSchema = new Schema({a: aSchema, foo: String});
var cSchema = new Schema({as: [aSchema], foo:String});

This will cast TypeErroron bSchema: TypeError: Undefined type at 's' Did you try nesting Schemas? You can only nest using refs or arrays.but works great for cSchema.

Just ask why it bSchemadoesn't work. Cannot find an explanation in the Mongoose document. Thank.

+4
source share
2 answers

MongoDB is not a relational database. This can be confusing for those who are used to the RDBS model (I still stumble sometimes ... but I'm really not a DB guy).

. Mongoose , .

, , type ref. : a: Number; Mongoose , :

a: {
   type: Number,
   required: true   
}

required: true , a.

, , Mongoose:

a: {
   type: Mongoose.Schema.ObjectId,
   ref: 'a'
}

Mongoose ObjectId ( Mongoose ) a a . ?

Mongoose : doc.a = myA. doc, Mongoose .

, , . , - .

+3

, MongoDB. , , , Mongoose.

.

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;

var CountrySchema = new Schema({
    name: { type: String, required: true },
    activeStatus: Boolean,
    createdOn: Date,
    updatedOn: Date
});

.

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;

var StateSchema = new Schema({
    name: { type: String, required: true },
    country: {type: ObjectId, ref: "Country"},
    activeStatus: Boolean,
    createdOn: Date,
    updatedOn: Date
});

ref.

0

All Articles