Javascript 理解猫鼬子文档
我在学猫鼬。目前,我做了几件好事,但我真的不太明白Mongoose是如何管理模式之间的关系的 所以,简单的事情(我希望如此):我正在做一个经典的练习(我自己做,因为我找不到一个创建超过2个模式的好教程),其中有3个模式: 用户、帖子、评论Javascript 理解猫鼬子文档,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我在学猫鼬。目前,我做了几件好事,但我真的不太明白Mongoose是如何管理模式之间的关系的 所以,简单的事情(我希望如此):我正在做一个经典的练习(我自己做,因为我找不到一个创建超过2个模式的好教程),其中有3个模式: 用户、帖子、评论 用户可以创建多个帖子 用户可以创建许多评论 帖子属于用户 评论属于用户和帖子 我不认为这是件很难的事uhu 目前我可以很好地处理用户和帖子之间的关系。我的单元测试返回的正是我需要的,目前我正在使用,我不知道这是否是一个好主意 it('Use shoul
- 用户可以创建多个帖子李>
- 用户可以创建许多评论李>
- 帖子属于用户
- 评论属于用户和帖子
it('Use should create a Post', function(done) {
User.findOne({ email: 'test@email.com' }, function(err, user) {
var post = {
title: 'Post title',
message: 'Post message',
comments: []
};
user.posts.create(post, function(err, user, post) {
if (err) return done(err);
user.posts[0].should.equal(post._id);
post.author.should.equal(user._id);
// etc...
done();
});
});
});
现在的问题是创建注释。
我不能创建一个评论,引用文章和用户在一起
我做了类似的事情,但当我执行remove
时,它只会从帖子中删除,而不会从用户中删除
所以我认为我错过了一些东西,或者我仍然需要学习来提高它
it('User should add a Comment to a Post', function(done) {
User.findOne({ email: 'test@email.com' }, function(err, user) {
if (err) return done(err);
var comment = new Comment({
author: user._id,
message: 'Post comment'
});
Post.findOne({ title: 'Post title'}, function(err, post) {
if (err) return done(err);
post.comments.append(comment, function(err, comment) {
if (err) return done(err);
post.save(function(err) {
if (err) return done(err);
});
comment.author.should.equal(user._id);
post.comments.should.have.length(1);
// etc...
done();
});
});
});
});
正如您所看到的,代码不是很“好看”,但它在创建方面工作得很好
问题是当我删除一条评论时。好像有什么不对劲
以下是模型关系:
// User Schema
var userSchema = new mongoose.Schema({
// [...],
posts: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Post' }],
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
});
// Post Schema
var postSchema = new mongoose.Schema({
author: { type: mongoose.Schema.ObjectId, ref: 'User', refPath: 'posts' },
title: String,
message: String,
comments: [{ type: mongoose.Schema.ObjectId, ref: 'Comment' }]
});
// Comment Schema
var commentSchema = new mongoose.Schema({
author: { type: mongoose.Schema.ObjectId, ref: 'User', refPath: 'comments' },
post: { type: mongoose.Schema.ObjectId, ref: 'Post', refPath: 'comments' },
message: String
});
我真的希望在你的帮助下了解这一切
这将是一个很好的简单教程。我想你误解了子文档。按照模式设置的方式,您正在创建对其他集合中文档的引用 例如,如果在数据库中创建一篇文章,它将如下所示:
{
"author": ObjectId(123),
"title": "post title",
"message": "post message",
"comments": [ObjectId(456), ObjectId(789)]
}
请注意,“author”字段只包含创建它的作者的ID。它实际上并不包含文档本身
当您从数据库中读取文档时,您可以使用mongoose的“填充”功能来获取引用的文档
Ex(带填充):
Ex(无填充):
子文档:
实际上,您可以使用子文档在数据库中嵌套数据,模式看起来会略有不同:
var postSchema = new mongoose.Schema({
author: { userSchema },
title: String,
message: String,
comments: [commentSchema]
});
保存帖子时,用户文档将嵌套在帖子中:
{
"author": {
"name": "user name",
"email": "test@email.com"
...
},
"title": "post title",
"message": "post message",
"comments": [{
"message": "test",
...
}, {
"message": "test",
...
}]
}
子文档在mongo中可能很有用,但在这种情况下可能不行,因为您将复制每篇文章中的所有用户数据
删除文档
当您发出注释。删除(id)时,注释将被删除,但不会影响引用它的其他文档。因此,您将有一个帖子和一个用户,其注释ID不存在。您需要手动清除其他文档中的注释ID。您可以使用mongoose pre remove事件来执行此操作
你如何删除它?我知道它,我希望只有我做的ID,而不是复制整个用户文档。。。使用“填充”,我可以获得所需的所有属性。只需更新我的答案,并提供有关删除文档的详细信息。感谢您的更新,这很有意义,但如何从注释架构中获取用户ID和帖子ID?你能提供一个更深刻的例子吗?感谢更新了我的答案,其中有一些关于如何从删除的评论中获取用户和帖子ID的详细信息。谢谢,我非常感谢您的帮助。
var postSchema = new mongoose.Schema({
author: { userSchema },
title: String,
message: String,
comments: [commentSchema]
});
{
"author": {
"name": "user name",
"email": "test@email.com"
...
},
"title": "post title",
"message": "post message",
"comments": [{
"message": "test",
...
}, {
"message": "test",
...
}]
}
commentSchema.pre('remove', function (next) {
// this refers to the document being removed
var userId = this.author;
var postId = this.post;
User.findById(userId, function(err, user) {
// remove comment id from users.comments here;
Post.findById(postId, function(err, post) {
// remove comment id from post.comments;
next();
});
});
});