Let's say I have a mongodb "schema" like this:
offers: Schema = {
name: String,
validityStart: Date,
validityEnd: Date,
customized: [
{
validityStart: Date,
validityEnd: Date,
user: String
}
]
}
By words: here we have a collection of offers, valid between the dates specified by validityStart and validityEnd. Each offer has a subarray which define that a user can have this offer "customized", i.e. having a longer time to use.
I can query all the offers available with
Offer.find({ $and: [{ validityStart: { $lte: today } }, { validityEnd: { $gte: today } }] })
but If "user" is provided, I need to create a query that show me all offers and show the "customized" ones by filtering via the user field.
An example to show what I want to achieve is this: imagine having some offers for December
[
{
name: "Soccer Game 2020",
validityStart: "2019-12-1",
validityEnd: "2019-12-31"
},
{
name: "Golf Equipment",
validityStart: "2019-12-1",
validityEnd: "2019-12-31"
}
]
but I let the user FOO to be an early bird of 1 month for the soccer game, I'll have:
[
{
name: "Soccer Game 2020",
validityStart: "2019-12-1",
validityEnd: "2019-12-31",
customized:[
{
validityStart: "2019-11-1",
validityEnd: "2019-11-30",
user: "FOO"
}
]
},
---
]
How to return for all the user the "parent" dates but for some user, specified by the customized array, their respective dates? Like:
[
{
name: "Soccer Game 2020",
validityStart: "2019-11-1",
validityEnd: "2019-11-30"
},
{
name: "Golf Equipment",
validityStart: "2019-12-1",
validityEnd: "2019-12-31"
}
]
I dunno how to create a query like this.
I'm available to change the schema, I'm in the early stage of developments so it's not a problem.
customizedfield for that particular offer and rest of the offers would have standard validity dates? Can you update the question with what would a result in above example for user "FOO" would look like for better visibility.