mongodb使用跳过,在查询中根据数组中的值在查询中截然不同

发布于 2025-02-13 08:11:37 字数 1593 浏览 0 评论 0原文

因此,我拥有这样的结构文件

_id: ObjectId('62bbe17d8fececa06b91873d')
clubName: 'test'
staff:[
 '62bbe47f8fececa06b9187d8'
 '624f4b56ab4f5170570cdba3' //IDS of staff members
]

,可以将单个员工分配给多个俱乐部,我跟随此解决方案>可以在一个查询上使用,但它只是返回了这一点:

[
  { _id: [ '624f5054ab4f5170570cdd16', '624f5054ab4f5170570cdd16' ] }  //staff from club 1,
  { _id: [ '624f5054ab4f5170570cdd16', '624f9194ab4f5170570cded1' ] } //staff from club 2,
  { _id: [ '624f4b56ab4f5170570cdba3' ]} //staff from club 3
]

我所期望的结果就是这样:

[ _id : ['624f5054ab4f5170570cdd16', '624f9194ab4f5170570cded1', '624f4b56ab4f5170570cdba3'] ]

这是我的查询:

const query = this.clubModel.aggregate(
      [{ $group: { _id: '$staff' } }, { $skip: 0}, { $limit: 10}],
      (err, results) => {
        console.log(results);
      },
    );

返回的值根本没有区别,是否有一个可以评估数组内值并使它们与众不同的操作?

这是我在文档结构中添加“创建”字段后的新查询:

const query = this.clubModel.aggregate([
      { $sort: { createdAt: -1 } },
      {
        $unwind: '$drivers',
      },
      {
        $project: {
          isActive: true,
        },
      },
      {
        $group: {
          _id: 'null',
          ids: {
            $addToSet: '$drivers',
          },
        },
      },
      {
        $project: {
          _id: 0,
        },
      },
      {
        $skip: skip,
      },
      {
        $limit: limit,
      },
    ]);

So I have document that is structure like this

_id: ObjectId('62bbe17d8fececa06b91873d')
clubName: 'test'
staff:[
 '62bbe47f8fececa06b9187d8'
 '624f4b56ab4f5170570cdba3' //IDS of staff members
]

A single staff can be assigned to multiple clubs so what I'm trying to achieve is to get all staff that has been assigned to at least one club and display them on a table on the front end, I followed this solution since distinct and skip can't be used on a single query but it just returned this:

[
  { _id: [ '624f5054ab4f5170570cdd16', '624f5054ab4f5170570cdd16' ] }  //staff from club 1,
  { _id: [ '624f5054ab4f5170570cdd16', '624f9194ab4f5170570cded1' ] } //staff from club 2,
  { _id: [ '624f4b56ab4f5170570cdba3' ]} //staff from club 3
]

my desired outcome would be like this:

[ _id : ['624f5054ab4f5170570cdd16', '624f9194ab4f5170570cded1', '624f4b56ab4f5170570cdba3'] ]

here's my query:

const query = this.clubModel.aggregate(
      [{ $group: { _id: '$staff' } }, { $skip: 0}, { $limit: 10}],
      (err, results) => {
        console.log(results);
      },
    );

the values returned are not distinct at all, is there an operation that can evaluate the value inside an array and make them distinct?

Here's my new query after adding the 'createdAt' field in my document structure:

const query = this.clubModel.aggregate([
      { $sort: { createdAt: -1 } },
      {
        $unwind: '$drivers',
      },
      {
        $project: {
          isActive: true,
        },
      },
      {
        $group: {
          _id: 'null',
          ids: {
            $addToSet: '$drivers',
          },
        },
      },
      {
        $project: {
          _id: 0,
        },
      },
      {
        $skip: skip,
      },
      {
        $limit: limit,
      },
    ]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

帅的被狗咬 2025-02-20 08:11:37

这对您有效吗,第一个Undind员工数组,然后在“ _id”上分组为null ,然后使用添加 starts 值$ addtoset :

db.collection.aggregate([
  {
    "$unwind": "$staff"
  },
  {
    "$group": {
      "_id": "null",
      "ids": {
        "$addToSet": "$staff"
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      
    }
  },
  {
    $skip: 0
  },
  {
    $limit: 10
  }
])

这是工作链接

Does this works for you, first UNWIND the staff array, and then group on "_id" as null and add staff values using $addToSet:

db.collection.aggregate([
  {
    "$unwind": "$staff"
  },
  {
    "$group": {
      "_id": "null",
      "ids": {
        "$addToSet": "$staff"
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      
    }
  },
  {
    $skip: 0
  },
  {
    $limit: 10
  }
])

Here's the working link.

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