Retrieve Distinct Values of a Field检索字段的不同值¶
If you specify a callback method, 如果指定回调方法,distinct()
returns nothing. distinct()
将不返回任何内容。If you do not specify one, this method returns a 如果未指定,此方法将返回一个Promise
that resolves to the result object when it completes. Promise
,该Promise
在完成时解析为结果对象。See our guide on Promises and Callbacks for more information, or the API documentation for information on the result object.有关更多信息,请参阅我们的承诺和回调指南,或参阅API文档以了解有关结果对象的信息。
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()
方法,并将文档字段名称参数作为字符串,以生成一个列表,其中包含在指定文档字段中找到的每个不同值中的一个,如下所示:
const distinctValues = collection.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 = collection.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>
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 它使用一个查询文档来匹配包括芭芭拉·史翠珊(Barbara Streisand)作为导演的电影。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.用MongoDB部署的连接字符串替换uri字符串。
const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() {
try {
await client.connect();
// 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);
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.用MongoDB部署的连接字符串替换uri字符串。
const uri = "<connection string uri>";
const client = new MongoClient(uri);
interface Movie {
directors: string;
year: number;
}
async function run() {
try {
await client.connect();
// define a database and collection on which to run the method
const database = client.db("sample_mflix");
const movies = database.collection<Movie>("movies");
const distinctValues = await movies.distinct("year", {
directors: "Barbra Streisand",
});
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 ]