我正在尝试分别创建一个文档和一个子文档。 架构:
const mongoose = require('mongoose');
require('./streets');
const PlaceSchema = new mongoose.Schema({
place1: String,
streets: [{type: mongoose.Schema.Types.ObjectId, ref: 'streetSchema'}]
});
module.exports = mongoose.model('place', PlaceSchema);
SubSchema:
const mongoose = require('mongoose');
const StreetSchema = new mongoose.Schema(
{
trafficLights: Number,
shops: Number,
busStops: Number,
},
{ strict: false }
);
module.exports = StreetSchema;
然后我尝试自行实例化街道:
const place = // ...get a certain place from DB;
const street = new Street({
trafficLights: 1,
shops: 2,
busStops: 3,
});
place.streets.push(street)
我正在得到
Street is not a constructor
如何创建实例化的子架构并将其推入“ streets”数组?
PS-我还尝试将两个模式放在同一文件中,但是在没有实例化整个“地方”模式的情况下无法到达嵌套数组(街道)。
谢谢。
答案 0 :(得分:0)
您应该创建一个模型,请在模式文件末尾添加此行
mongoose.model('street', StreetSchema);
并在需要的地方将其导入
const Street = mongoose.model('street');