使用 nodejs 在 mongoose 中加入 table 的最佳方法
Best way to join a table in mongoose using nodejs
我正在为学校项目创建一个基本应用程序。我想将狗主人与他们拥有的狗相匹配。我设置了两种模型,一种用于狗,一种用于主人。狗主人可以养多只狗,所以我猜这是一对多。一个主人可以养多只狗...
我有以下 Dog 模型:
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 的过程)...
提前致谢!
我显示了要求复制此问题的答案。但我不认为它是重复的,所以我正在回答。
基本上是通过添加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' }]
});
我正在为学校项目创建一个基本应用程序。我想将狗主人与他们拥有的狗相匹配。我设置了两种模型,一种用于狗,一种用于主人。狗主人可以养多只狗,所以我猜这是一对多。一个主人可以养多只狗...
我有以下 Dog 模型:
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 的过程)...
提前致谢!
我显示了要求复制此问题的答案。但我不认为它是重复的,所以我正在回答。
基本上是通过添加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' }]
});