首页 > 解决方案 > 从 Nodejs 和 Mongoose 中的关注者加载帖子

问题描述

我有一个类似下面的帖子架构,但在从我的关注者那里获取帖子时遇到问题。我也尝试过使用,但都无济于事。请帮助我有一个如下所示的帖子架构,但在从我的关注者那里获取帖子时遇到问题。我也尝试过使用,但都无济于事。请帮助我有一个如下所示的帖子架构,但在从我的关注者那里获取帖子时遇到问题。我也尝试过使用,但都无济于事。请帮忙

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

const PostSchema =new Schema({
    user: {
        type: Schema.Types.ObjectId,
        ref:'users'
    },
    text:{
        type:String,
        required: true
    },
    name:{
        type:String
    },
    avatar:{
        type:String
    },
    likes:[
        {
            user:{
                type: Schema.Types.ObjectId,
                ref: 'users'
            }
        }
    ],
    comments:[
        {
            user:{
                type: Schema.Types.ObjectId,
                ref: 'users'
            },
            text:{
                type:String,
                required: true
            },
            name: {
                type: String
            },
            avatar: {
                type: String
            },
            date:{
                type:Date,
                default: Date.now
            },
            likes: [
                {
                    user: {
                        type: Schema.Types.ObjectId,
                        ref: 'users'
                    }
                }
            ],
        }
    ],
    reposter: [
        {
            user: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
            date: {
                type: Date,
                default: Date.now
            }
        }
    ],

    numberOfRepost: { type: Number, default: 0 },

    date: {
        type: Date,
        default: Date.now
    }
});

module.exports = Post = mongoose.model('post', PostSchema);

标签: node.jsmongoose

解决方案


首先,你应该重新考虑一下 mongo-collection 的设计,这里有一些建议最好考虑一下。

  • 使用Upper Camel case 来声明 mongoose 模型对象。(帖子,用户,...)
  • 始终将_放在任何引用变量之前。(帖子模型中的 _user )
  • 分离您的集合并尽可能避免冗余属性。
  • 始终对集合使用复数名称。(帖子与帖子)
  • 不要忘记将createdupdated属性添加到每个集合。(这个 hack 可以帮助你记录和调查你的模型)

现在,让我们看看我们的新设计:

  1. name 和 avatar 是 Post 模型中的冗余数据。您可以稍后填充它们。
  2. 将 Like、Comment、RePoster 与 Post 模型分开。

这是改进的 Post 模型对象。

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

const PostSchema = new Schema(
  {
    _user: { type: Schema.Types.ObjectId, ref: 'User' },

    text:{ type:String, required: true },

    rePostsNum: { type: Number, default: 0 },

    // any other meta data which you need

    creaetd: Date,
    updated: Date
  },
  {  
    collection: 'posts',
    strict: true,
    autoIndex: true
  }
);

PostSchema.pre('save', function (next) {
    if( this.isNew )
      this.created = new Date();

    this.updated = new Date();

    next();
});

module.exports = Post = mongoose.model('Post', PostSchema);

您也可以放入_comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }]Post 模型,但请考虑一下!如果可以在_comments 数组中存储数千条评论引用键,则不推荐,这就像技术债务一样。

其他型号:

评论:

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

const CommentSchema = new Schema(
  {
    _user: { type: Schema.Types.ObjectId, ref: 'User' },
    _post: { type: Schema.Types.ObjectId, ref: 'Post' },

    text:{ type:String, required: true },

    likesNum: { type: Number, default: 0 },

    creaetd: Date,
    updated: Date
  },
  {  
    collection: 'posts',
    strict: true,
    autoIndex: true
  }
);

CommentSchema.pre('save', function (next) {
    if( this.isNew )
      this.created = new Date();

    this.updated = new Date();

    next();
});

module.exports = Comment = mongoose.model('Comment', CommentSchema);

点赞帖子

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

const LikePostSchema = new Schema(
  {
    _user: { type: Schema.Types.ObjectId, ref: 'User' },    
    _post: { type: Schema.Types.ObjectId, ref: 'Post' },

    creaetd: Date,
    updated: Date
  },
  {  
    collection: 'likePosts',
    strict: true,
    autoIndex: true
  }
);

LikePostSchema.pre('save', function (next) {
    if( this.isNew )
      this.created = new Date();

    this.updated = new Date();

    next();
});

module.exports = LikePost = mongoose.model('LikePost', LikePostSchema);

喜欢评论

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

    const LikeCommentSchema = new Schema(
      {
        _user: { type: Schema.Types.ObjectId, ref: 'User' },    
        _comment: { type: Schema.Types.ObjectId, ref: 'Comment' },

        creaetd: Date,
        updated: Date
      },
      {  
        collection: 'likeComments',
        strict: true,
        autoIndex: true
      }
    );

    LikeCommentSchema.pre('save', function (next) {
        if( this.isNew )
          this.created = new Date();

        this.updated = new Date();

        next();
    });

    module.exports = LikeComment = mongoose.model('LikeComment', LikeCommentSchema);

用户

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

const UserSchema = new Schema(
  {
    name:{ type:String, required: true },
    avatar:{ type:String, required: true },

    // any other meta data which you need

    creaetd: Date,
    updated: Date
  },
  {  
    collection: 'users',
    strict: true,
    autoIndex: true
  }
);

UserSchema.pre('save', function (next) {
    if( this.isNew )
      this.created = new Date();

    this.updated = new Date();

    next();
});

module.exports = User = mongoose.model('User', UserSchema);

重新发布

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

const RePostSchema = new Schema(
  {
    _user: { type: Schema.Types.ObjectId, ref: 'User' },    
    _post: { type: Schema.Types.ObjectId, ref: 'Post' },

    creaetd: Date,
    updated: Date
  },
  {  
    collection: 'rePosts',
    strict: true,
    autoIndex: true
  }
);

RePostSchema.pre('save', function (next) {
    if( this.isNew )
      this.created = new Date();

    this.updated = new Date();

    next();
});

module.exports = RePost = mongoose.model('RePost', RePostSchema);

欢迎回来!现在我们的新设计是完全可扩展的,并指导您编写干净和健壮的代码。最后,我们可以查询和填充数据,这是两个很酷的示例代码:

加载特定用户的帖子

var mongoose    = require('mongoose');
var User        = mongoose.model('User');
var Post        = mongoose.model('Post');
var Comment     = mongoose.model('Comment');
var LikePost    = mongoose.model('LikePost');
var LikeComment = mongoose.model('LikeComment');
var RePost      = mongoose.model('RePost');


Post
.find({ _user: userId })
.select('_id _user text ...')
.populate({
  path: '_user',
  select: '_id name avatar ...'
})
.exec(function (err, poats) {

加载特定帖子的评论

Comment
.find({ _post: postId })
.select('_id _post _user text ...')
.populate({
  path: '_user',
  select: '_id name avatar ...'
})
.exec(function (err, comments) {

推荐阅读