从MongoDB查询结果集提取字段

发布于 2025-01-22 18:08:30 字数 768 浏览 0 评论 0原文

如何从节点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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所谓啦 2025-01-29 18:08:30

假设totalCount值在所有对象中都相同,则可以使用第一个索引提取第一个值

let tc = Allposts[0].totalCount;

let data = [{
    _id: 1,
    type: 'A',
    source: 'B',
    status: 'C',
    totalCount: 7,
    createdAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    updatedAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    __v: 0
  },
  {
    _id: 2,
    type: 'A',
    source: 'B',
    status: 'C',
    totalCount: 7,
    createdAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    updatedAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    __v: 0
  },
  {
    _id: 3,
    type: 'A',
    source: 'B',
    status: 'C',
    totalCount: 7,
    createdAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    updatedAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    __v: 0
  }
]

let tc = data[0].totalCount;

console.log(tc);

Assuming totalCount value will be same in all the objects, you can extract the first value using first index

let tc = Allposts[0].totalCount;

let data = [{
    _id: 1,
    type: 'A',
    source: 'B',
    status: 'C',
    totalCount: 7,
    createdAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    updatedAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    __v: 0
  },
  {
    _id: 2,
    type: 'A',
    source: 'B',
    status: 'C',
    totalCount: 7,
    createdAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    updatedAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    __v: 0
  },
  {
    _id: 3,
    type: 'A',
    source: 'B',
    status: 'C',
    totalCount: 7,
    createdAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    updatedAt: '2022 - 04 - 13 T17: 12: 28.097 Z',
    __v: 0
  }
]

let tc = data[0].totalCount;

console.log(tc);

江城子 2025-01-29 18:08:30

如果仅从查询响应中获得totalCount字段,则可以在查询中添加.project()如下

const Allposts = await Post.aggregate().project("totalCount -_id");

If you get only totalCount field from query response, you can add .project() in query as below

const Allposts = await Post.aggregate().project("totalCount -_id");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文