从MongoDB查询结果集提取字段
如何从节点JS中的查询响应中提取JSON字段?
我有一个查询: -
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
我想提取totalCount
字段并存储其值如何?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设
totalCount
值在所有对象中都相同,则可以使用第一个索引提取第一个值Assuming
totalCount
value will be same in all the objects, you can extract the first value using first index如果仅从查询响应中获得
totalCount
字段,则可以在查询中添加.project()如下If you get only
totalCount
field from query response, you can add .project() in query as below