如何在返回的文档数组中将项目顺序随机化?

时间:2018-10-05 10:39:52

标签: mongodb aggregation-framework

样品收集:

{
  model: Pants,
  colors: [green, blue]
},
{
  model: Trousers,
  colors: [red, blue, yellow]
},
...

我想使用mongodb聚合框架将字段colors的所有数组随机化。

可能的解决方案:

{
  model: Pants,
  colors: [blue, green]
},
{
  model: Trousers,
  colors: [yellow, red, blue]
},
...

我发现了$sample pipeline stage,但这似乎仅适用于查询本身,不适用于其文档的数组值。

有没有办法用mongodb做到这一点?

我正在使用Mongo 3.6

感谢您的帮助!

0 个答案:

没有答案