我试图让用户向其他用户发送类似于 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) 我的节点应用程序中有一个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类型。谢谢!