我需要一个Show Schema,我想在节目中只获得可用的门票。我需要找到所有可用的文件> 0并只使用show._id和show.title获取此子文档。我怎么能这样做?
// Show's Model
const mongoose = require('../../database/mongoose')
const ShowSchema = mongoose.Schema({
title: { type: String, required: true },
description: { type: String, required: true },
startsAt: { type: Date, required: true },
endsAt: { type: Date, required: true },
openToBuyAt: { type: Date, required: true },
closedToBuyAt: { type: Date, required:true },
status: { type: Boolean, required: true },
sectors: [{
name: { type: String, required: true },
price: { type: Number, required: true },
availableTickets: { type: Number, required: true }
}],
convenienceFee: { type: Number, required: true },
images: [
{ type: String }
]
})
module.exports = mongoose.model('Show', ShowSchema)
预期结果
{
"status": "success",
"tickets": [
{
"show._id": "ddadadas12a",
"show.title": "Crazy Show",
"sectors": [
{
"name": "VIP"
"price": 50.25,
"availableTickets": 12
},
{
"name": "General Sector"
"price": 20.00,
"availableTickets": 5
}
]
}
]
}
答案 0 :(得分:0)
不幸的是你有错误的架构。
你没有"可用的门票"领域。如果你有任何字段,比如说 availableTickets
你可以这样轻松地做到这一点:
此查询中有两个主要部分:
n-1