AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

クイック リファレンス

このページでは、いくつかの MongoDB コマンドのドライバー構文を示し、それらに関連するリファレンスと API ドキュメントにリンクします。

Node.js ドライバーを使用して、次の環境でホストされている配置に接続し、コマンドを実行できます。

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

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

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

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

コマンド
構文
await coll.findOne({ title: 'Hamlet' });
coll.find({ year: 2005 });
await coll.insertOne({ title: 'Jackie Robinson' });
await coll.insertMany([ { title: 'Dangal', rating: 'Not Rated' }, { title: 'The Boss Baby', rating: 'PG' } ]);
await coll.updateOne( { title: 'Amadeus' }, { $set: { 'imdb.rating': 9.5 } });
await coll.updateMany( { year: 2001 }, { $inc: { 'imdb.votes': 100 } });
await coll.updateOne( { title: 'Cosmos' }, { $push: { genres: 'Educational' } }):
await coll.replaceOne( { name: 'Deli Llama', address: '2 Nassau St' }, { name: 'Lord of the Wings', zipcode: 10001 });
await coll.deleteOne({ title: 'Congo' });
await coll.deleteMany({ title: { $regex: /^Shark.*/ } });
await coll.bulkWrite([ { insertOne: { document: { title: 'A New Movie', year: 2022 } } }, { deleteMany: { filter: { year: { $lt: 1970 } } } }]);
coll.watch([ { $match: { year: { $gte: 2022 } } } ]);

カーソルから反復的にデータにアクセスする

カーソルからデータにアクセスするガイド

const cursor = coll.find();for await (const doc of cursor) { console.dir(doc);}

カーソルから配列としてカーソルにアクセス

API ドキュメント
カーソルからデータにアクセスするガイド

const cursor = coll.find();const results = await cursor.toArray();
await coll.countDocuments({ year: 2000 });

個別のドキュメントまたはフィールド値を一覧表示する
API ドキュメント
Retrieve Distinct Values Guide

await coll.distinct('year');

取得されるドキュメント数の制限

API ドキュメント
ドキュメント参照の制限

coll.find().limit(2);

検索されたドキュメントをスキップする

API ドキュメント
ドキュメントの参照をスキップ

coll.find({ title: { $regex: /^Rocky/} }, { skip: 2 });

ドキュメントを取得するときにソートする

API ドキュメント
ドキュメントのソートに関する参照

coll.find().sort({ year: 1});

プロジェクト ドキュメント フィールドを取得する際

API ドキュメント
返すフィールドを指定する ガイド

coll.find().project({ _id: 0, year: 1, imdb: 1 });

インデックスの作成

API ドキュメント
Indexes Guide

await coll.createIndex({ title: 1, year: -1 });
// only searches fields with text indexescoll.find({ $text: { $search: 'zissou' } });

ドライバーの依存関係のインストール

package.json

"dependencies": { "mongodb": "^6.22", ...}
このページを評価