I've created an API. I've a problem here. I want to set the limits on the Fields
of each document to return.
API:
const Pipeline =
[{
'$search': {
'text': {
'query': `${text}`,
'path': 'title',
}
}
},
{
'$limit': 5
}]
var result = await collection.aggregate(pipeline).toArray();
response.status(200).send(result);
My response from server:
{
"_id": "039x39sd939hlf",
"name": "Lorem ipsum dolor sit amet, consectetur adipiscing eli",
"brand": "Pogoplug",
"type": "Online data entry",
"price": 69,
"description": "giat nulla pariatur. Excepteur sint occa"
},
{
"_id": "039x39sd939hlf",
"name": "Lorem ipsum dolor sit amet, consectetur adipiscing eli",
"brand": "Pogoplug",
"type": "Online data backup",
"price": 69,
"description": "giat nulla pariatur. Excepteur sint occa"
}
Now, I've to limit it like this: Expected Result:
{
"name": "Lorem ipsum dolor sit amet, consectetur adipiscing eli",
"brand": "Peppa Pig",
},
{
"name": "Lorem ipsum dolor sit amet, consectetur adipiscing eli",
"brand": "Pogoplug",
},
Which aggregation should I use which is fast and make this possible??
You can add a $project
stage to return only fields you want, like this:
const Pipeline =
[
{
/*your current pipeline*/
},
{
"$project": {
"name": 1,
"brand": 1
}
}
]
Example here