Retrieve Distinct Values of a Field检索字段的剔除重复值
You can retrieve a list of distinct values for a field across a collection by using the collection.distinct()通过使用 method.
collection.distinct()
方法,可以检索集合中某个字段的剔除重复值列表。
Call the 对distinct()
method on a Collection
object with a document field name parameter as a String
to produce a list that contains one of each of the different values found in the specified document field as shown below:Collection
对象调用distinct()
方法,并将文档字段名参数作为String
,以生成一个列表,该列表包含指定文档字段中的每个不同值之一,如下所示:
const distinctValues = myColl.distinct("countries", query);
You can specify a document field within an embedded document using dot notation. 可以使用点表示法在嵌入文档中指定文档字段。If you call 如果对包含数组的文档字段调用distinct()
on an document field that contains an array, the method treats each element as a separate value. distinct()
,则该方法将每个元素视为一个单独的值。See the following example of a method call to the 请参阅以下对wins
field in the awards
subdocument:awards
子文档中的wins
字段进行方法调用的示例:
const distinctValues = myColl.distinct("awards.wins", query);
You can specify additional query options using the 您可以使用作为options
object passed as the third parameter to the distinct()
method. distinct()
方法的第三个参数传递的options
对象来指定其他查询选项。For details on the query parameters, see the distinct() method in the API documentation.有关查询参数的详细信息,请参阅API文档中的distinct()
方法。
If you specify a value for the document field name that is not of type 如果为文档字段名指定的值不是String
such as a Document
, Array
, Number
, or null
, the method does not execute and returns a TypeMismatch
error with a message that resembles the following:String
类型,例如Document
、Array
、Number
或null
,则该方法不会执行,并返回TypeMismatch
错误,并显示类似以下消息:
"key" had the wrong type. Expected string, found <non-string type>
Visit Retrieve Distinct Values for more information about the 有关distinct()
method.Distinct()
方法的详细信息,请访问检索剔除重复值。
Example实例
The following snippet retrieves a list of distinct values for the 以下代码段从year
document field from the movies
collection. movies
集合中检索year
文档字段的剔除重复值列表。It uses a query document to match movies that include "Barbara Streisand" as a 它使用一个查询文档来匹配包括“芭芭拉·史翠珊”作为导演的电影。director
.
You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. 您可以使用此示例连接到MongoDB的实例,并与包含示例数据的数据库进行交互。To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.要了解有关连接到MongoDB实例和加载示例数据集的更多信息,请参阅用法实例指南。
import { MongoClient } from "mongodb";
//Replace the uri string with your MongoDB deployment's connection string.将uri字符串替换为MongoDB部署的连接字符串。
const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() {
try {
//define a database and collection on which to run the method定义要在其上运行方法的数据库和集合
const database = client.db("sample_mflix");
const movies = database.collection("movies");
//specify the document field指定文档字段
const fieldName = "year";
//specify an optional query document指定可选的查询文档
const query = { directors: "Barbra Streisand" };
const distinctValues = await movies.distinct(fieldName, query);
console.log(distinctValues);
} finally {
await client.close();
}
}
run().catch(console.dir);
If you run the preceding example, you should see the following output:如果运行前面的示例,您应该会看到以下输出:
[ 1983, 1991, 1996 ]