我正在为学校项目创建一个基本应用程序。我希望将狗主人与他们拥有的狗匹配。我为狗设置了两个模型,为主人设置了一个模型。狗主人可以有多只狗,所以我猜它是一对多。一个主人可以有多只狗...
我有以下狗的模型:
var mongoose = require(' mongoose'); var Schema = mongoose.Schema;
var DogSchema = new Schema({
name: String,
age: Number,
location: String,
breed: String,
sex: String
});
module.exports = mongoose.model('Dog', DogSchema);
和所有者:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var OwnerSchema = new Schema({
firstName: String,
lastName: String,
age: Number,
location: String,
favorite: String,
numberOfBreeds: Number,
numberOfDogs: String,
username: String,
password: String
});
module.exports = mongoose.model('Owner', OwnerSchema);
我怎么能加入这两个?我不确定我是否理解加入mongo(或sql)的过程......
提前致谢!
答案 0 :(得分:1)
我会显示要求复制此问题的答案。但我不认为它重复,所以我回答。
基本上通过添加one Schema to another schema
之间的关系var DogSchema = new Schema({
name: String,
age: Number,
location: String,
breed: String,
sex: String,
owner : [{ type: Schema.Types.ObjectId, ref: 'Owner' }]
});