从Nodejs和Mongoose中的关注者加载帖子

时间:2019-02-28 08:34:04

标签: node.js 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);

2 个答案:

答案 0 :(得分:1)

首先,您应该重新考虑mongo-collection设计,这里有一些提示,最好考虑一下。

  • 使用 Upper Camel 案例声明猫鼬模型对象。 (帖子,用户,...)
  • 始终将 _ 放在任何参考变量之前。 (_post模型中的_user)
  • 分离集合,并尽可能避免使用冗余属性。
  • 始终对集合使用多个名称。 (帖子与帖子)
  • 不要忘记向每个集合添加创建的已更新属性。 (此黑客可帮助您记录和调查模型)

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

  1. 名称和头像是Post模型中的冗余数据。您可以稍后填充它们。
  2. 将“赞”,“评论”和“重新发布”与“发布”模型分开。

这是精致的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);

LikePost

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);

LikeComment

    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) {

答案 1 :(得分:0)

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

const UserSchema = new Schema({
    name: {
        type: String,
        required: true,
    },
    username: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    avatar: {
        type: String
    },
    phonenumber: {
        type: String,
        required: true
    },
    email: {
        type: String
    },
    resetPasswordToken: {
        type: String
    },
    lastlogin: {
        type: Date, default: Date.now
    },
    passwordUpdated: {
        type: Date, default: Date.now
    },
    resetPasswordExpires: {
        type: Date
    },
    dateofbirth: {
        type: Date
    },
    dateJoined: {
        type: Date,
        default: Date.now
    },
    sentRequest: [{
        username: { type: String, default: '' },
        date: {
            type: Date,
            default: Date.now
        }
    }],
    request: [{
        userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
        username: { type: String, default: '' },
        name: { type: String, default: '' },
        phone: { type: String, default: '' },
        date: {
            type: Date,
            default: Date.now
        }
    }],
    friendsList: [{
        friendId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
        friendName: { type: String, default: '' },
        friendUsername: { type: String, default: '' },
        friendPhone: { type: String, default: '' },
        date: {
            type: Date,
            default: Date.now
        }
    }],
    followers: [{
        userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
        date: {
            type: Date,
            default: Date.now
        }
    }],
    blockedContacts: [
        {
            userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
            date: {
                type: Date,
                default: Date.now
            }
        }],

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

    repost: [
        {
            postId: { type: mongoose.Schema.Types.ObjectId, ref: "Post" },
            date: {
                type: Date,
                default: Date.now
            }
        }
    ],
    accolades: [
        {
            user: {
                type: mongoose.Schema.Types.ObjectId, ref: 'User'

            },
            remarks: {
                type: String
            },
            date: {
                type: Date,
                default: Date.now
            }
        }
    ],

    followers: [{
        user: {
            type: mongoose.Schema.Types.ObjectId, ref: 'User'
             },
        date: {
            type: Date,
            default: Date.now
        }
    }],
    following: [{
        user: {
            type: mongoose.Schema.Types.ObjectId, ref: 'User'
        },
        date: {
            type: Date,
            default: Date.now
        }
    }],

})

UserSchema.pre('save', function (next) {
    var user = this;

    //check if password is modified, else no need to do anything
    if (!user.isModified('pass')) {
        return next()
    }

    user.pass = bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
    next()
})
module.exports = User = mongoose.model('Users', UserSchema);

这是用户架构