在 mongodb 中返回唯一值-ag捕鱼王app官网

在 mongodb 中返回唯一值

作者:迹忆客 最近更新:2023/05/07 浏览次数:

在本文中,我们将解决如何使用 mongodb distinct() 方法返回唯一值。 此外,还讨论了返回数组和字段中的唯一值。

在 mongodb 中,有时您可能希望呈现或返回唯一值,您可以使用 distinct() 方法来实现。

distinct() 方法为单个集合或视图中的所有集合或视图的给定字段生成不同值的数组,并帮助您为 mongo shell 中指定的字段返回唯一值。


mongodb 中的 distinct() 函数

语法:

db.collection.distinct(field, query, options)

在 mongodb 中,该函数必须有两个必要的参数,称为字段和查询运算符。 field 参数是您希望 distinct() 方法在其中获取唯一值的字段。

查询参数指定应从中获取不同值的文档。 您可以向该函数发送抵押选项参数。

请注意 ,如果指定字段的值是一个数组,则 distinct() 会将每个数组元素视为一个单独的值。 例如,如果字段的值为 [10, [10], 10],distinct() 会将 10、[10] 和 10 视为单独的值。


使用 distinct() 函数返回唯一值

让我们使用 distinct() 函数返回 mongodb 中的唯一值。 下面的集合将用于第一个示例。

db.movies.find().pretty()
{
        "_id" : objectid("60322d3501cd70079c48cb65"),
        "title" : "enchanted",
        "year" : 2006,
        "score" : 10,
        "rating" : "pg",
        "__v" : 0
}
{
        "_id" : objectid("60322d3501cd70079c48cb67"),
        "title" : "final destination ii",
        "year" : 2015,
        "score" : 10,
        "rating" : "pg-13",
        "__v" : 0
}
{
        "_id" : objectid("6190189ef5c8903629012fe1"),
        "title" : "fifty shades of grey",
        "year" : 2015,
        "score" : 10,
        "rating" : "nc-17",
        "__v" : 0
}

为 mongodb 中的字段返回唯一或不同的值

上面的电影集合返回此示例中指定字段的唯一值。 假设您想要评级字段中的唯一值。

查询:

db.movies.distinct( "rating" )
[ null, "", "nc-17", "pg", "pg-13"]

使用上述查询后,distinct() 函数已成功帮助您在 mongodb 中为集合返回唯一值。


为 mongodb 中的嵌入式字段返回唯一值

在此示例中,显示了为嵌入字段返回唯一值。 但是,上一个示例中使用的集合不包含嵌入字段。

因此,将使用一个新的集合,如下所示。

db.drones.find().pretty()
{
        "_id" : objectid("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "natural resource exploration",
                "remote sensing",
                "real estate and construction",
                "recreation",
                "delivery"
        ],
        "onsale" : false,
        "name" : "nimbari gryphon medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionaldetails" : {
                "material" : "carbon fiber",
                "moreuses" : [
                        "precision agriculture",
                        "land inspection",
                        "water inspection",
                        "cinematography"
                ]
        }
}
{
        "_id" : objectid("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "natural resource exploration",
                "remote sensing",
                "real estate and construction",
                "recreation",
                "delivery"
        ],
        "onsale" : false,
        "name" : "x-strimmer eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionaldetails" : {
                "material" : "glass fiber",
                "moreuses" : [
                        "precision agriculture",
                        "cinematography"
                ]
        }
}
{
        "_id" : objectid("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "natural resource exploration",
                "remote sensing",
                "real estate and construction",
                "recreation",
                "delivery"
        ],
        "onsale" : false,
        "name" : "khai balemosh shefqa trx",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionaldetails" : {
                "material" : "aluminum",
                "moreuses" : [
                        "precision agriculture",
                        "land inspection"
                ]
        }
}

以下查询可以从上述数据库集合中的所有嵌入字段返回唯一值。

查询:

db.drones.distinct( "additionaldetails.material" )
[ "aluminum", "carbon fiber", "glass fiber"]

返回嵌入式数组字段的唯一值

使用 distinct() 函数,您可以使用上面的相同集合,使用 distinct() 函数从嵌入式数组字段返回唯一值。

查询:

db.drones.distinct( "additionaldetails.moreuses" )
["cinematography","land inspection"]

在这篇 mongodb 教程文章中,您将详细了解带有集合的不同函数。 返回数组和字段或嵌入字段中的唯一值也用代码段进行了解释。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 mongodb shell 中列出所有数据库

发布时间:2023/05/11 浏览次数:180 分类:mongodb

交互式 mongo shell 提供了多个用于获取数据的选项。 本文介绍了在 mongo shell 中列出数据库的几种不同方法。

mongodb 中检查字段包含的字符串

发布时间:2023/05/11 浏览次数:1024 分类:mongodb

这篇文章解决了如何在 mongodb 中使用正则表达式来确定字段是否包含字符串。在 mongodb 中使用正则表达式 正则表达式 (regex) 是定义搜索模式的文本字符串。

在 mongodb 中 upsert 更新插入

发布时间:2023/05/11 浏览次数:214 分类:mongodb

在 mongodb 中,upsert 结合了更新和插入命令。 它可以在 update() 和 findandmodify() 操作中使用。mongodb 中的 upsert 查询 upsert 采用单个布尔参数。

如何卸载 mongodb

发布时间:2023/05/11 浏览次数:745 分类:mongodb

要从您的计算机中卸载 mongodb,您必须先删除 mongodb 服务、数据库和日志文件。使用这篇 mongodb 文章,您将能够从 ubuntu linux、mac 和 windows 卸载 mongodb。 请务必保留数据备份,因为一旦卸载,便

在 mongodb 中存储日期和时间

发布时间:2023/05/11 浏览次数:762 分类:mongodb

本 mongodb 教程解释了 date() 对象是什么以及如何使用 date() 方法对集合进行排序。 这也将帮助您找到在 mongodb 中显示和存储日期/时间的最佳方法。

mongodb 按 id 查找

发布时间:2023/05/11 浏览次数:1856 分类:mongodb

mongodb 中的 find by id() 函数用于获取与用户提供的 id 相匹配的文档。 如果找不到与指定 id 匹配的文档,则返回空值。

检查 mongodb 服务器是否正在运行

发布时间:2023/05/11 浏览次数:247 分类:mongodb

这篇 mongodb 教程将告诉您如何检查是否安装了 mongodb 以及安装的 mongodb 服务器的版本。 它在 windows、ubuntu 和 mac 等不同的操作系统中实现。

mongodb 中的分页

发布时间:2023/05/11 浏览次数:174 分类:mongodb

这篇文章将介绍什么是 mongodb 中的分页。 为什么在 mongodb 中需要分页以及在 mongodb 中完成分页的不同方法或方式是什么。

mongodb 从查询开始

发布时间:2023/05/11 浏览次数:186 分类:mongodb

在这篇 mongodb 文章中,用户将学习如何使用 $regex 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图