gpt4 book ai didi

mongodb - 跨索引获取数组字段的不同聚合

转载 作者:IT老高 更新时间:2023-10-28 13:07:59 26 4
gpt4 key购买 nike

我正在尝试学习 MongoDB 以及它对我的分析有何用处。我只是在玩他们网站上的 JavaScript 控制台,并创建了以下项目:

{"title": "Cool", "_id": {"$oid": "503e4dc0cc93742e0d0ccad3"}, "tags": ["twenty", "sixty"]}
{"title": "Other", "_id": {"$oid": "503e4e5bcc93742e0d0ccad4"}, "tags": ["ten", "thirty"]}
{"title": "Ouch", "_id": {"$oid": "503e4e72cc93742e0d0ccad5"}, "tags": ["twenty", "seventy"]}
{"title": "Final", "_id": {"$oid": "503e4e72cc93742e0d0ccad6"}, "tags": ["sixty", "seventy"]}

我想做的是查询,所以我得到了所有这些对象的唯一标签列表。结果应如下所示:

["ten", "twenty", "thirty", "sixty", "seventy"]

我该如何查询?我正在尝试 distinct() 它,但调用总是失败,甚至没有查询。

最佳答案

他们网站上失败的代码在实际的 MongoDB 实例上运行:

> db.posts.insert({title: "Hello", tags: ["one", "five"]});
> db.posts.insert({title: "World", tags: ["one", "three"]});
> db.posts.distinct("tags");
[ "one", "three", "five"]

很奇怪。

关于mongodb - 跨索引获取数组字段的不同聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12183444/

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