Docs Menu
Docs Home
/ / /
Node.js ドライバー
/ /

ドキュメントの検索

collection.findOne()メソッドを使用して、コレクション内の単一のドキュメントをクエリできます。 findOne()メソッドは、指定したクエリ ドキュメントを使用して、クエリに一致するコレクション内のドキュメントのサブセットのみを照合します。 クエリ ドキュメントを指定しない場合、または空のドキュメントを指定した場合、MongoDB はコレクション内のすべてのドキュメントを照合します。 findOne()操作では、最初に照合したドキュメントのみが返されます。 MongoDB のクエリの詳細については、 クエリ ドキュメントに関するドキュメントを参照してください。

返されるドキュメントを設定するには、ソートプロジェクションなどの追加のクエリ オプションも定義できます。findOne メソッドの 2 番目のパラメーターとして渡される options オブジェクトで、さらに多くのオプションを指定できます。詳細な参照ドキュメントについては、collection.findOne() を参照してください。

Node.js ドライバーを使用して接続し、次の環境でホストされている配置にfindOne()メソッドを使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる、MongoDB の自己管理型バージョン

MongoDB Atlas でホストされている配置の Atlas UI でドキュメントを検索する方法の詳細については、「ドキュメントの作成、表示、更新、および削除」を参照してください。

次のスニペットは、movies コレクションから 1 つのドキュメントを検索します。次のパラメーターを使用します。

  • 'The Room' というテキストと完全に一致するタイトルの映画のみを返すようにクエリを構成するクエリドキュメント

  • 一致したドキュメントを評価の降順で並べ替えるソート。これにより、クエリが複数のドキュメントに一致した場合、返されるドキュメントは評価が最も高いドキュメントになります。

  • 返されるドキュメントから _id フィールドを明示的に除外し、titleimdb オブジェクト(およびその埋め込みフィールド)のみを明示的に含むプロジェクション

注意

この例を使って MongoDB のインスタンスに接続し、サンプルデータを含むデータベースとやり取りできます。MongoDB インスタンスへの接続とサンプル データセットの読み込みの詳細については、使用例ガイドを参照してください。

import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.
const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() {
try {
// Get the database and collection on which to run the operation
const database = client.db("sample_mflix");
const movies = database.collection("movies");
// Query for a movie that has the title 'The Room'
const query = { title: "The Room" };
const options = {
// Sort matched documents in descending order by rating
sort: { "imdb.rating": -1 },
// Include only the `title` and `imdb` fields in the returned document
projection: { _id: 0, title: 1, imdb: 1 },
};
// Execute query
const movie = await movies.findOne(query, options);
// Print the document returned by findOne()
console.log(movie);
} finally {
await client.close();
}
}
run().catch(console.dir);
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.
const uri = "<connection string uri>";
const client = new MongoClient(uri);
interface IMDB {
rating: number;
votes: number;
id: number;
}
export interface Movie {
title: string;
year: number;
released: Date;
plot: string;
type: "movie" | "series";
imdb: IMDB;
}
type MovieSummary = Pick<Movie, "title" | "imdb">;
async function run(): Promise<void> {
try {
const database = client.db("sample_mflix");
// Specifying a Schema is always optional, but it enables type hinting on
// finds and inserts
const movies = database.collection<Movie>("movies");
const movie = await movies.findOne<MovieSummary>(
{ title: "The Room" },
{
sort: { rating: -1 },
projection: { _id: 0, title: 1, imdb: 1 },
}
);
console.log(movie);
} finally {
await client.close();
}
}
run().catch(console.dir);

前の例を実行すると、次の出力が表示されます。

{ title: 'The Room', imdb: { rating: 3.5, votes: 25673, id: 368226 } }

戻る

検索操作