gpt4 book ai didi

mongodb聚合: group by _id

转载 作者:可可西里 更新时间:2023-11-01 10:03:56 26 4
gpt4 key购买 nike

聚合后我得到了这个结果。 _id 字段是每个类(class)的 ID,我应用了 {$group:{_id:"$_id", .....

[{ _id: 54c977314f5293b74ea54f96, subject: 'math' }, score: 73.5335 },
{ _id: 54c977314f5293b74ea54f96, subject: 'science' }, score: 56.2192 },
{ _id: 54c977314f5293b74ea54f96, subject: 'history' }, score: 82.8821 },
{ _id: 54c974bdff0d993b4ecf34ce, subject: 'math' }, score: 68.2598 },
{ _id: 54c974bdff0d993b4ecf34ce, subject: 'science' }, score: 77.8712 },
{ _id: 54c974bdff0d993b4ecf34ce, subject: 'history' }, score: 71.9218 },
... ]

问题是 _id 值不是字符串类型。所以当我向客户端发送 JSON 时,它显示格式错误的 JSON。

如何获取字符串类型_id?提前致谢。

最佳答案

不要自己形成 JSON 字符串。使用 JSON.stringify( output_from_aggregation )

但是,如果您不想那样做,那么就这样做。

  • 确保形成用双引号 (") 包裹的字符串,而不是单引号 (')。单引号中的字符串在 JSON 中无效。
  • 您需要将 _id 值包装在一个字符串中。这是一个无效的十六进制数,因为它太大而无法解析。

旧代码:

...
{ _id: 54c974bdff0d993b4ecf34ce, subject: 'science' }, score: 77.8712 },
...

新代码:

...
{ _id: "54c974bdff0d993b4ecf34ce", subject: "science" }, score: 77.8712 },
...

更多信息:

关于mongodb聚合: group by _id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com