在猫鼬中为朋友模式建模? [英] Modelling for friends schema in mongoose?
问题描述
当我位于其他用户个人资料上时,如何对猫鼬模式进行建模以获取这三个按钮?
How do I model my mongoose schema to get these three buttons when I am on the other users profile?
- 添加朋友
- 已请求
- 朋友
我的用户架构
const schema = new Mongoose.Schema({
firstName: { type: String, default: '', trim: true },
lastName: { type: String, default: '', trim: true },
}, { timestamps: true })
我无法为此找到正确的建模...并且还请在建模后建议汇总...
I could not find the correct modelling for this... And also please suggest the aggregation after modelling...
推荐答案
所以最后我做到了,我认为这可能是用mongodb和mongoose做到的最好方法
So Finally I made it and I think it is probably the best way to do it with mongodb and mongoose
1..为用户创建模型.
1. Create a model for users.
var Schema = mongoose.Schema
const usersSchema = new Schema({
firstName: { type: String, required: true },
lastName: { type: String, required: true },
friends: [{ type: Schema.Types.ObjectId, ref: 'Friends'}]
}, {timestamps: true})
module.exports = mongoose.model('Users', usersSchema)
2..为拥有枚举值的朋友创建一个模型,该枚举用于接受,拒绝,待处理和请求.
2. Create a model for friends having enums for accepted, rejected, pending and requested.
const friendsSchema = new Schema({
requester: { type: Schema.Types.ObjectId, ref: 'Users'},
recipient: { type: Schema.Types.ObjectId, ref: 'Users'},
status: {
type: Number,
enums: [
0, //'add friend',
1, //'requested',
2, //'pending',
3, //'friends'
]
}
}, {timestamps: true})
module.exports = mongoose.model('Friends', friendsSchema)
3.现在api调用->假设我们有两个用户UserA和UserB ...因此,当UserA当时要求UserB成为朋友时,我们会创建两个 文档,以便UserA可以看到请求的内容,而UserB可以看到待处理的内容 同时我们将这些文档的_id推送到用户的 朋友
3. Now api calls --> Lets say we have two users UserA and UserB... So when UserA requestes UserB to be a friends at that time we make two documents so that UserA can see requested and UserB can see pending and at the same time we push the _id of these documents in user's friends
const docA = await Friend.findOneAndUpdate(
{ requester: UserA, recipient: UserB },
{ $set: { status: 1 }},
{ upsert: true, new: true }
)
const docB = await Friend.findOneAndUpdate(
{ recipient: UserA, requester: UserB },
{ $set: { status: 2 }},
{ upsert: true, new: true }
)
const updateUserA = await User.findOneAndUpdate(
{ _id: UserA },
{ $push: { friends: docA._id }}
)
const updateUserB = await User.findOneAndUpdate(
{ _id: UserB },
{ $push: { friends: docB._id }}
)
4..如果UserB接受了请求
4. If UserB acceptes the request
Friend.findOneAndUpdate(
{ requester: UserA, recipient: UserB },
{ $set: { status: 3 }}
)
Friend.findOneAndUpdate(
{ recipient: UserA requester: UserB },
{ $set: { status: 3 }}
)
5..如果UserB拒绝了请求
5. If UserB rejectes the request
const docA = await Friend.findOneAndRemove(
{ requester: UserA, recipient: UserB }
)
const docB = await Friend.findOneAndRemove(
{ recipient: UserA, requester: UserB }
)
const updateUserA = await User.findOneAndUpdate(
{ _id: UserA },
{ $pull: { friends: docA._id }}
)
const updateUserB = await User.findOneAndUpdate(
{ _id: UserB },
{ $pull: { friends: docB._id }}
)
6..获取所有朋友,并检查登录用户是否为该用户的朋友
6. Get all friends and check whether the logged in user is friend of that user or not
User.aggregate([
{ "$lookup": {
"from": Friend.collection.name,
"let": { "friends": "$friends" },
"pipeline": [
{ "$match": {
"recipient": mongoose.Types.ObjectId("5afaab572c4ec049aeb0bcba"),
"$expr": { "$in": [ "$_id", "$$friends" ] }
}},
{ "$project": { "status": 1 } }
],
"as": "friends"
}},
{ "$addFields": {
"friendsStatus": {
"$ifNull": [ { "$min": "$friends.status" }, 0 ]
}
}}
])
这篇关于在猫鼬中为朋友模式建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!