I am working with Mongoose and creating a view on a collection.
NewSchema.createCollection({
viewOn: originalModel.collection.collectionName,
pipeline: [
{
$project: keep.reduce((a, v) => ({ ...a, [v]: 1 }), {}),
},
],
});
This process involves generating a new schema that only displays specific fields defined as keep
.
The resulting model includes the following pipeline:
{
'$project': {
uuid: 1,
name: 1,
description: 1,
image_url: 1,
price: 1,
avg_rating: 1
}
}
However, when executing queries on this new schema like:
const res = await NewSchema.find({name: {$regex: keywords, $options: 'i' }}).sort({ 'price': -1 })
The search results still include all data from the collection. Filtering works correctly when querying the base collection. Is there a way to apply filters to a query in Mongoose on a model that is a view of another schema?