How to extract json field from query response in node js?
I have a query:-
const Allposts = await Post.aggregate([pipeline])
Allposts:-
{
_id: 1,
type: 'A',
source: 'B',
status: 'C',
totalCount: 7,
createdAt: 2022-04-13T17:12:28.097Z,
updatedAt: 2022-04-13T17:12:28.097Z,
__v: 0
},
{
_id: 2,
type: 'A',
source: 'B',
status: 'C',
totalCount: 7,
createdAt: 2022-04-13T17:12:28.097Z,
updatedAt: 2022-04-13T17:12:28.097Z,
__v: 0
},
{
_id: 3,
type: 'A',
source: 'B',
status: 'C',
totalCount: 7,
createdAt: 2022-04-13T17:12:28.097Z,
updatedAt: 2022-04-13T17:12:28.097Z,
__v: 0
}, and so on
I want to extract the totalCount
field and store its value how to do this?
2
Answers
Assuming
totalCount
value will be same in all the objects, you can extract the first value using first indexIf you get only
totalCount
field from query response, you can add .project() in query as below