Here is a mongoose schema that I have created:
var MySchema = new Schema({
name: String,
attributes: [ {name: String, value: Schema.Types.Mixed} ]
});
The challenge is that the clients will determine what the attributes will be, so it's unpredictable from the start.
I am working on developing a generic search function to filter results based on attribute.name='x', and potentially attribute.value='y' (although my example excludes values). Do you have any advice on how to approach writing such a method?
let filter = ['name', 'attributes.name=height', 'attributes.name=superpower'];
function search(filter) {
//This needs to be fixed
const mongoReply = await myModel.find({}, 'attributes.name');
}
UPDATE:
This code appears to be functioning correctly for me. I intend to conduct further tests to confirm its behavior as expected.
const mongoReply = await cuk.aggregate([
{
$match: {$and: [{'attributes.name' : 'color'}, {'attributes.name' : 'superpower'}]}
},
{
$project: {
attributes: {
$filter: {
input: '$attributes',
as: 'attribute',
cond: {
$or : [ { $eq: [ '$$attribute.name', 'name'] },
{ $eq: [ '$$attribute.name', 'crypto_length'] }
]
}
}
}
}
}
])