【发布时间】:2016-08-27 22:30:11
【问题描述】:
我是学习 MEAN 堆栈的初学者,我正在尝试建立一个基本论坛以熟悉此堆栈。到目前为止,除了 sub-sub-docs 之外,我已经完成了所有工作。我在主题内的帖子中对 cmets 执行 CRUD 时遇到问题。我已经做了很多搜索,但没有任何东西看起来完全符合我的需要。所以我的问题是,你将如何实现这一点?我知道可能有多种方法可以做到这一点,例如使用 refs 而不是子子文档,但是看到我已经为 CRUD 主题和使用子文档的主题中的 CRUD 帖子等内容编写了代码,我会如果我必须回去更改我的代码,而不是使用 refs。
var express = require('express');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var uri = "...";
mongoose.connect(uri);
var CommentSchema = new Schema({
id: ObjectId,
content: String,
author: UserSchema
});
var PostSchema = new Schema({
id: ObjectId,
title: String,
author: UserSchema,
comments: [CommentSchema]
});
var TopicSchema = new Schema({
id: ObjectId,
title: String,
moderator: UserSchema,
posts: [PostSchema]
});
var Topic = mongoose.model('Topic', TopicSchema);
var app = express();
app.delete('/topics/:topicId/posts/:postId/comments/:commentId', function(req, res) {
//What goes here?
});
app.put('/topics/:topicId/posts/:postId/comments/:commentId', function(req, res) {
//What goes here?
});
app.post('/topics/:topicId/posts/:postId/comments/:commentId', function(req, res) {
//What goes here?
});
app.get('/topics/:topicId/posts/:postId/comments/:commentId', function(req, res) {
//What goes here?
});
【问题讨论】:
标签: node.js mongodb mongoose mean-stack mongoose-schema