gpt4 book ai didi

MongoDB 聚合 $group queryquery

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

我有一个集合,里面的文档是这样的

{
"_id" : "api",
"titleAndTimeDetails" : [
{
"Title" : "api data",
"timeTaken" : NumberLong(8091446063)
},
{
"Title" : "class data",
"timeTaken" : NumberLong(519163710)
},
{
"Title" : "API or datasets for age world",
"timeTaken" : NumberLong(34245103)
},
{
"Title" : "What does a null result mean API?",
"timeTaken" : NumberLong(171605137)
}
]
}

/* 2 */
{
"_id" : "government",
"titleAndTimeDetails" : [
{
"Title" : "Is there an open standard for the CAFR (Comprehensive Annual Finance Report)?",
"timeTaken" : NumberLong(574587563)
},
{
"Title" : "College Scorecard full data base",
"timeTaken" : NumberLong(9422714)
},
{
"Title" : "List of German local politicians",
"timeTaken" : NumberLong(691311396)
},
{
"Title" : "Trying to extrapolate patient costs by physician from public Medicare pricing data",
"timeTaken" : NumberLong(9590779130)
},
{
"Title" : "Are there good examples of open read-write APIs in Federal government?",
"timeTaken" : NumberLong(1784634763)
}
]
}

查询语句是显示Title每个“_id”花费的时间最少。请问有人可以帮我吗?

最佳答案

你应该先$unwind titleAndTimeDetails 然后是 $sort通过 titleAndTimeDetails.timeTaken 最后 $group通过 _id 并获取 Title 的 $first。

db.collection.aggregate([
{ $unwind: "$titleAndTimeDetails" },
{ $sort: { "titleAndTimeDetails.timeTaken": 1 } },
{ $group: { _id: "$_id", title: { $first: "$titleAndTimeDetails.Title" } } }
])

关于MongoDB 聚合 $group queryquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46641817/

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