I have Booking model with field [status(type:String)] and can only have four values NEW,ALLOTED,CANCELLED,COMPLETED . And also I have Customer model ,which has field [bookings(as array of Booking ("ref"))]
Actually I need four arrays for four values of status (for , NEW,ALLOTED,CANCELLED,COMPLETED),from Customer model (customer _id is given) via bookings array (also populated),
can deal with aggregate in mongoose?!
For that I first have to get all documents and then I have to iterate over all and then ,by (if-else ladder) I can push documents according to values. Or by doing four queries through aggregate. But actually I have large number of documents ,so I think it will not be efficient
Is there any other way to do this in javascript or in mongoose,mongodb(via queries). I am beginner .Please Help ,Thank You! (For nodejs)
const bookingSchema = new Schema({
category: {
type: String,
default: null
},
status: {
type: String,
enum: {
values: ["NEW", "ALLOTED", "COMPLETED", "CANCELLED"],
message: "bad status"
},
default: "NEW",
required: true
}
})
const customerSchema= new Schema({
email: {
type: String,
required:true
},
bookings: [
{
type: Schema.Types.ObjectId,
ref: 'Booking'
}
],
})