首页 > 解决方案 > 猫鼬模式可以存储相同模式的引用吗?在社交媒体应用程序中存储用户的关注者所需的帮助

问题描述

在一个社交媒体应用程序中,我制作了一个用户模型。在其模式中,我想存储用户的关注者(关注者也是如此),为此,我需要存储关注我的用户的 objectIds。但是我可以存储同一个模型的objectId吗?我想不会。我怎样才能做到这一点?我需要为它制作一个单独的模型吗?

用户.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// const User = require('./user');
const userSchema = new Schema({
    username: {
        type: String,
        required: true,
        unique: true,
        min: 5,
        max: 20
    },
    email: {
        type: String,
        required: true,
        unique:true,
    },
    password: {
        type: String,
        required: true,
        unique:true,
        min: 6
    },
    profilePicture: {
        type: String,
    },
    followers: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    following: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    }
})

标签: node.jsmongodbmongoose

解决方案


我认为您应该将类​​型following和用户更改follower为 用户数组ObjectId, 关于您的问题,下面的链接可能很有用 How to nest the same schema in mongoose js


推荐阅读