小编jbl*_*lew的帖子

带有 Express 和 MongoDB 的好友请求系统

我试图让用户向其他用户发送类似于 Facebook 和其他社交媒体平台的好友请求。我已经开始创建这个功能,但很快就卡住了,因为我是 mongoDB 和整个 Schema 的新手。

以下是我目前拥有的模型:

// User Schema
var UserSchema = new Schema({
    _id: {
        type: Number
    },
    name: {
        type: String
    },
    username: {
        type: String,
        index: true
    },
    password: {
        type: String,
        required: true
    },
    email: {
        type: String
    },  
    friends: [{
        friendName: {
            type: Schema.ObjectId,
            required: true,
            ref: 'User'
        },
        duration: {
            type: Number
        }
    }]
});


// Friendship Schema
var FriendshipSchema = new Schema({
    participants: [{
        type: Schema.Types.ObjectId, 
        ref: 'User'
    }],
    requestTo: { …
Run Code Online (Sandbox Code Playgroud)

schema mongodb node.js express

3
推荐指数
1
解决办法
5416
查看次数

MongoDB +猫鼬:唯一:true无法正常工作

我的节点应用程序中有一个user.js模型,我希望用户名和其他一些字段唯一。在我的模型文件中,我正确声明了unique如下所示的类型:

// User Schema
const UserSchema = new Schema({

    // PERSONAL USER INFO
    username: {
        type: String,
        required: true,
        index: true,
        unique: true,
    },
    email: {
        type: String,
        required: true,
        index: true,
        unique: true
    },
    password: {
        type: String,
        required: true,
    },
    ....
});
Run Code Online (Sandbox Code Playgroud)

但是,在重新启动服务器和mongo会话之后,我仍然可以使用相同的用户名和相同的电子邮件创建用户。我可以从mongo shell和前端用户注册页面上执行此操作。

我还缺少第二部分吗?我现在不确定如何正确执行该unique类型。谢谢!

unique mongoose mongodb node.js express

1
推荐指数
1
解决办法
2052
查看次数

标签 统计

express ×2

mongodb ×2

node.js ×2

mongoose ×1

schema ×1

unique ×1