Docs Home / Node.js Driver

Quick Reference快速参考

This page shows the driver syntax for several MongoDB commands and links to their related reference and API documentation.本页显示了几个MongoDB命令的驱动程序语法,并链接到它们的相关参考和API文档。

Compatibility兼容性

You can use the Node.js driver to connect and execute commands for deployments hosted in the following environments:您可以使用Node.js驱动程序连接并执行以下环境中托管的部署的命令:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

To learn more about performing common CRUD operations in the Atlas UI for deployments hosted in MongoDB Atlas, see Create, View, Update, and Delete Documents.

CommandSyntax
Find a Document


API Documentation
Find Documents Guide
await coll.findOne({ title: 'Hamlet' });
{ title: 'Hamlet', type: 'movie', ... }
Find Multiple Documents


API Documentation
Find Documents Guide
coll.find({ year: 2005 });
[
{ title: 'Christmas in Boston', year: 2005, ... },
{ title: 'Chicken Little', year: 2005, ... },
...
]
Insert a Document


API Documentation
Insert Documents Guide
await coll.insertOne({ title: 'Jackie Robinson' });
Insert Multiple Documents


API Documentation
Insert Documents Guide
await coll.insertMany([
{ title: 'Dangal', rating: 'Not Rated' },
{ title: 'The Boss Baby', rating: 'PG' }
]);
Update a Document


API Documentation
Update Documents Guide
await coll.updateOne(
{ title: 'Amadeus' },
{ $set: { 'imdb.rating': 9.5 } }
);
{ title: 'Amadeus', imdb: { rating: 9.5, ... } }
Update Multiple Documents


API Documentation
Update Documents Guide
await coll.updateMany(
{ year: 2001 },
{ $inc: { 'imdb.votes': 100 } }
);
[
{ title: 'A Beautiful Mind', year: 2001, imdb: { votes: 826257, ... },
{ title: 'Shaolin Soccer', year: 2001, imdb: { votes: 65442, ... },
...
]
Update Arrays in Documents


API Documentation
Update Arrays in a Document Guide
await coll.updateOne(
{ title: 'Cosmos' },
{ $push: { genres: 'Educational' } }
):
{ title: 'Cosmos', genres: [ 'Documentary', 'Educational' ] }
Replace a Document


API Documentation
Replace Documents Guide
await coll.replaceOne(
{ name: 'Deli Llama', address: '2 Nassau St' },
{ name: 'Lord of the Wings', zipcode: 10001 }
);
{ name: 'Lord of the Wings', zipcode: 10001 }
Delete a Document


API Documentation
Delete Documents Guide
await coll.deleteOne({ title: 'Congo' });
Delete Multiple Documents


API Documentation
Delete Documents Guide
await coll.deleteMany({ title: { $regex: /^Shark.*/ } });
Bulk Write


API Documentation
Bulk Operations Guide
await coll.bulkWrite([
{
insertOne: {
document: {
title: 'A New Movie',
year: 2022
}
}
},
{
deleteMany: {
filter: { year: { $lt: 1970 } }
}
}
]);
BulkWriteResult {
result: {
...
},
...
}
Watch for Changes


API Documentation
Monitor Data with Change Streams Guide
coll.watch([ { $match: { year: { $gte: 2022 } } } ]);
Access Data from a Cursor Iteratively


Access Data from a Cursor Guide
const cursor = coll.find();
for await (const doc of cursor) {
console.dir(doc);
}
[
{ title: '2001: A Space Odyssey', ... },
{ title: 'The Sound of Music', ... },
...
]
Access Data from a Cursor as an Array


API Documentation
Access Data from a Cursor Guide
const cursor = coll.find();
const results = await cursor.toArray();
[
{ title: '2001: A Space Odyssey', ... },
{ title: 'The Sound of Music', ... },
...
]
Count Documents


API Documentation
Count Documents Guide
await coll.countDocuments({ year: 2000 });
618
List the Distinct Documents or Field Values
API Documentation
Retrieve Distinct Values Guide
await coll.distinct('year');
[ 1891, 1893, 1894, 1896, 1903, ... ]
Limit the Number of Documents Retrieved


API Documentation
Limit Documents Reference
coll.find().limit(2);
[
{ title: 'My Neighbor Totoro', ... },
{ title: 'Amélie', ... }
]
Skip Retrieved Documents


API Documentation
Skip Documents Reference
coll.find({ title: { $regex: /^Rocky/} }, { skip: 2 });
[
{ title: 'Rocky III', ... },
{ title: 'Rocky IV', ... },
{ title: 'Rocky V'}, ... }
]
Sort the Documents When Retrieving Them


API Documentation
Sort Documents Reference
coll.find().sort({ year: 1});
[
{ title: 'Newark Athlete', year: 1891, ... },
{ title: 'Blacksmith Scene', year: 1893, ...},
{ title: 'Dickson Experimental Sound Film', year: 1894},
...
]
Project Document Fields When Retrieving Them


API Documentation
Specify Which Fields to Return Guide
coll.find().project({ _id: 0, year: 1, imdb: 1 });
[
{ year: 2012, imdb: { rating: 5.8, votes: 230, id: 8256 }},
{ year: 1985, imdb: { rating: 7.0, votes: 447, id: 1654 }},
...
]
Create an Index


API Documentation
Indexes Guide
await coll.createIndex({ title: 1, year: -1 });
Query Text


API Documentation
Query Text Guide
// only searches fields with text indexes仅搜索具有文本索引的字段
coll.find({ $text: { $search: 'zissou' } });
[
{ title: 'The Life Aquatic with Steve Zissou', ... }
]
Install the Driver Dependency安装驱动程序依赖项package.json
"dependencies": {
"mongodb": "^7.0",
...
}