【发布时间】:2020-05-31 14:12:22
【问题描述】:
我有两张桌子。 1) 房间列表 2) 房间用户。这里的左表将是 roomList。即使连接的表结果为空,是否可以获取所有结果?
我目前正在使用聚合:$lookup、$unwind、$match。
房间列表
const roomInfo = new mongoose.Schema({
roomName: {
type: String,
trim: true,
required: true
},
currentMembers: {
type: Number,
required: true
},
createdBy: {
type: mongoose.Schema.ObjectId,
required: true,
ref: "User"
},
creationDate: {
type: Date,
trim: true
},
status: {
type: Number,
required: true,
default: 1
}
});
房间用户
const roomUser = new mongoose.Schema({
roomId: {
type: mongoose.Schema.ObjectId,
required: true,
ref: "RoomInfo"
},
userId: {
type: mongoose.Schema.ObjectId,
required: true,
ref: "User"
},
joiningDate: {
type: Date,
trim: true
},
leavingDate: {
type: Date,
trim: true
},
requestDate:{
type:Date,
trim:true
},
status: {
type: Number,
default: 0
}
我想要类似于这个 mysql 查询的结果。
SELECT * FROM roomList LEFT JOIN roomUser ON roomUser.roomId = roomList.id WHERE roomUser.userId = 123 AND roomList.status = 1。
【问题讨论】:
-
我终于明白了,使用所谓的 JOIN 来解决这个问题是如此困难(砰砰砰)。我使用猫鼬子文档做到了。现在我的收藏看起来像这样: