有两个表,一个用户表,一个订单表,现在想要查询所有 appId 为 1 的用户的订单应该怎么查询呢
const orderSchema = new mongoose.Schema({
user: {
type: mongoose.SchemaTypes.ObjectId,
ref: "User",
required: true
},
orderNum: {
type: String,
required: true
},
})
const UserSchema = new mongoose.Schema({
nickname: String,
avatarUrl: String,
phone: String,
appId: {
type: String,
}
});
1
dreasky 2022-10-11 15:05:00 +08:00
aggregate $lookup
|
2
leopod1995 2022-10-11 18:45:19 +08:00
最简单的
1. 先查所有 appId 为 1 的用户 id -> userIds = user.find({ appId: 1 }).select('_id') 2. order.find({ user: { $in: userIds }}) |