Using JavaScript, What I can render so far are the entries from a Table using a filter to display on client side what I need to show.
What I would like to is build a mongoose query to have the entries from all Tables using the same filter (if possible) otherwise just a filter for a single property/Table that I can exclude x key/values from.
Currently, using JS I return the key value pairs using Object.entries()
and manipulate the returned query string before rendering client side. I'm not sure if this is even close to best practice or not but I'm open to suggestions to help me improve.
I do a lot of research before asking questions because I like to learn but I think this obstacle is original and I hope that my question helps others new to coding, like myself.
// Mongoose Model
const demoSchema = new mongoose.Schema({
Table_1: [
{
Sub_Area: String,
Demography_Universe_Oct_20: String,
Demography_Universe_Oct_21: String,
},
],
Table_2: [
{
Sub_Area: String,
Demography_Universe_Oct_20: String,
Demography_Universe_Oct_21: String,
},
],
})
I tried to build a mongoose query but I can't access the objects
I am using find()
for my query, if I use findById()
it returns null
[
{
_id: new ObjectId("62d53b95ed429d306dbe13b0"),
Table_1: [
[Object], [Object], [Object],
],
__v: 0
}
]
So I used JavaScript instead gaining access to each property with index of 0, data[0].
Currently unable to duplicate this behavior in Mongoose
I'd like to iterate each array without using its property name
e.Table_1
// JS Query
data = regional
.flatMap((e) => e.Table_1) // I'd like to iterate each array without using its property name.
.filter((e) => e.Center == "VA")
data = { ...data }
dataObj = data[0]
// Omitted additional filtering portion of the code
// Current query returns the following result:
// Result
Table_1: [
{
Sub_Area: 'CMA-div1',
Center: 'VA'
Demography_Universe_Oct_20: '10,312',
Demography_Universe_Oct_21: '10,294',
_id: new ObjectId("62d53b95ed429d306dbe1823")
},
],
Table_2: [
{
Sub_Area: 'CMA-div2',
Center: 'VA'
Demography_Universe_Oct_20: '35,435',
Demography_Universe_Oct_21: '35,880',
_id: new ObjectId("62d53b95ed429d306dbe1822")
}
]