我从mongoose 开始,我想知道如何进行这种配置:

enter image description here

食谱有不同的成分.

我有两个模型:

配料和配方:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var IngredientSchema = new Schema({
    name: String
});

module.exports = mongoose.model('Ingredient', IngredientSchema);
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var RecipeSchema = new Schema({
    name: String
});

module.exports = mongoose.model('Recipe', RecipeSchema);

推荐答案

判断以下更新的代码,尤其是本部分:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var IngredientSchema = new Schema({
    name: String
});

module.exports = mongoose.model('Ingredient', IngredientSchema);
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var RecipeSchema = new Schema({
    name: String,
    ingredients:[
      {type: Schema.Types.ObjectId, ref: 'Ingredient'}
    ]
});

module.exports = mongoose.model('Recipe', RecipeSchema);

To Save:

var r = new Recipe();

r.name = 'Blah';
r.ingredients.push('mongo id of ingredient');

r.save();

Node.js相关问答推荐

聚合发布/订阅消息

Firebase-admin筛选器.或只考虑第一个WHERE子句

使用ReadableStream.管道时NodeJS Crypto Hash不正确

对于具有重叠列的组合键,在冲突&q;上没有唯一或排除约束匹配错误

使用NodeJS的DynamoDB中的BatchGetItem出现MultipleValidationError

获取驱动器文件夹的直接子文件夹时出现问题

即使卷已设置,Docker Nodemon 也不会热重载

尽管 tsconfig 中提供了正确的路径,但仍出现找不到模块错误

Postgressql的BIGSERIAL自增序列,即使由于唯一约束错误没有创建行,也会自动增加

在 Nest 项目上运行 Jest 测试时,我的文件无法找到一个在测试之外没有任何问题的模块

当try 将 formData 转换为 express js 时,服务器无法识别 multipart/form-data

为什么 $or 在带有正则表达式的mongoose 中不能正常工作

如何为单个 mongo nodejs 驱动程序找到最佳连接池大小

为什么我的react 表单不能正常工作?

突然 React 无法执行create-react-app命令.为什么会发生这种情况,我该如何解决?

node/express:使用 Forever 连续运行脚本时设置 NODE_ENV

如何从 npm 注册表中删除 npm 包?

在 Node 中连接和缩小 JS 文件

如何在express 中设置默认路径(路由前缀)?

deno vs ts-node:有什么区别