gpt4 book ai didi

mongodb - 在 MongoDB 中查找一组不同的字段

转载 作者:可可西里 更新时间:2023-11-01 09:12:11 25 4
gpt4 key购买 nike

我在 MongoDB 中有以下 JSON 集合。

{
"_id": ObjectId("57529381551673386c9150a6"),
"team_code": 3,
"team_id": 2
},
{
"_id": ObjectId("57529381551673386c91514a"),
"team_code": 4,
"team_id": 5
},
{
"_id": ObjectId("57529381551673386c91514b"),
"team_code": 3,
"team_id": 2
},
{
"_id": ObjectId("57529381551673386c91514c"),
"team_code": 4,
"team_id": 5,
}

从数据中可以看出,有 2 条记录,每条记录为 (team_code=3, team_id =2) 和 (team_code =4, team_id=5 ).是否有可能获得一组不同的团队代码和团队 ID。有些东西,比如,

{
"team_code" : 3, "team_id" : 2
},
{
"team_code" : 4, "team_id" : 5,
}

最佳答案

您可以使用以下聚合管道执行此操作:

var distinctIdCode = { $group: { _id: { team_code: "$team_code", team_id: "$team_id" } } }
db.foo.aggregate([distinctIdCode])

这会给你:

{ "_id" : { "team_code" : 4, "team_id" : 5 } }
{ "_id" : { "team_code" : 3, "team_id" : 2 } }

此查询返回从您集合中的文档创建的新文档。返回的文档有一个 _id,它是在 team_codeteam_id 字段上对集合文档进行分组的结果。

关于mongodb - 在 MongoDB 中查找一组不同的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37722919/

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