这是我的表结构
需要:-我需要使用其活动成员(状态= true)的组,因为需要用户集合中的用户名
当前代码:-
Group.aggregate([
{
$lookup: {
from: "groupmembers",
localField: "_id",
foreignField: "group",
as: "membersList"
}
},
{
$lookup : {
from: "users",
localField: "membersList.user",
foreignField: "_id",
as: "userss"
}
},
{
$match:{"_id" : mongoose.Types.ObjectId("5abe70eb7ede1b695e9342d6")}
},
{
$project:{"_id":1,"name":"$name","member":{"membersList":"$membersList","userss":"$userss"}}
}
])
获得响应但态度不好->
[
{
"_id": "5abe70eb7ede1b695e9342d6",
"name": "Welcome To Hobnobbin",
"member": {
"membersList":["Got memebrsLiset from GroupMembers"],
"userss": ["Got users from groupMembers FK "]
}
}
]
我怎么能得到
[
{
"_id": "5abe70eb7ede1b695e9342d6",
"name": "Welcome To Hobnobbin",
"member": {
"membersList":["Got memebrsLiset from GroupMembers",user: "Userobject here"],
}
}
]
收藏:->
var GroupSchema = new Schema({
name: {
type: String,
required: 'Please fill Group name',
trim: true
},
photo:{
type:String
},
type: {
type: [{
type: String,
enum: ['child', 'adult']
}],
required: 'Please select at least one type'
},
description:{
type:String,
default:''
},
pinnedTweet:{
type:String,
default:''
},
/*members: [{
type: Schema.ObjectId,
ref: 'User'
}],*/
jointype: {
type: String,
enum: ['openToJoin', 'openToRequest', 'inviteOnly'],
default: 'openToJoin',
required: 'Please select at least one behavious of group'
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
createByHobnob: {
type: Boolean,
default: false
},
isWelcomeGroup: {
type: Boolean,
default: false
},
allowPrivateChat: {
type: Boolean,
default: false
}
});
var GroupMemberSchema = new Schema({
user : {
type: Schema.ObjectId,
ref: 'User'
},
group : {
type: Schema.ObjectId,
ref: 'Group'
},
role: {
type: String,
enum: ['member', 'ban', 'moderator', 'admin'],
default: 'member',
required: 'Please select at least one role'
},
is_active: {
type: Boolean,
default: false
},
is_invited: {
type: Boolean,
default: false
},
created: {
type: Date,
default: Date.now
},
allowChat: {
type: Boolean,
default: true
}
});
var UserSchema = new Schema({
firstName: {
type: String,
trim: true,
default: ''
},
lastName: {
type: String,
trim: true,
default: ''
},
displayName: {
type: String,
trim: true
},
email: {
type: String,
unique: true,
lowercase: true,
trim: true,
default: '',
validate: [validateLocalStrategyEmail, 'Please fill a valid email address']
}
});