Docs HomeNode.js

Quick Reference快速参考

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

Command命令Syntax语法
Find a Document查找文档


API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
await coll.findOne({ title: 'Hamlet' });
Find Multiple Documents查找多个文档


API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
coll.find({ year: 2005 });
Insert a Document插入文档


API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
await coll.insertOne({ title: 'Jackie Robinson' });
Insert Multiple Documents插入多个文档


API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
await coll.insertMany([
{ title: 'Dangal', rating: 'Not Rated' },
{ title: 'The Boss Baby', rating: 'PG' }
]);
Update a Document更新文档


API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
await coll.updateOne(
{ title: 'Amadeus' },
{ $set: { 'imdb.rating': 9.5 } }
);
Update Multiple Documents更新多个文档


API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
await coll.updateMany(
{ year: 2001 },
{ $inc: { 'imdb.votes': 100 } }
);
Update Arrays in Documents更新文档中的数组


API DocumentationAPI文档
Fundamentals基本原理
await coll.updateOne(
{ title: 'Cosmos' },
{ $push: { genres: 'Educational' } }
):
Replace a Document替换文档


API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
await coll.replaceOne(
{ name: 'Deli Llama', address: '2 Nassau St' },
{ name: 'Lord of the Wings', zipcode: 10001 }
);
Delete a Document删除文档


API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
await coll.deleteOne({ title: 'Congo' });
Delete Multiple Documents删除多个文档


API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
await coll.deleteMany({ title: { $regex: /^Shark.*/ } });
Bulk Write大容量写入


API DocumentationAPI文档
Usage Example用法示例
await coll.bulkWrite([
{
insertOne: {
document: {
title: 'A New Movie',
year: 2022
}
}
},
{
deleteMany: {
filter: { year: { $lt: 1970 } }
}
}
]);
Watch for Changes监视更改


API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
coll.watch([ { $match: { year: { $gte: 2022 } } } ]);
Access Data from a Cursor Iteratively从游标迭代访问数据


Fundamentals基本原理
const cursor = coll.find();
for await (const doc of cursor) {
console.dir(doc);
}
Access Data from a Cursor as an Array以数组形式从游标访问数据


API DocumentationAPI文档
Fundamentals基本原理
const cursor = coll.find();
const results = await cursor.toArray();
Count Documents计数文档


API DocumentationAPI文档
Usage Example用法示例
await coll.countDocuments({ year: 2000 });
List the Distinct Documents or Field Values列出剔除重复的文档或字段值
API DocumentationAPI文档
Usage Example用法示例
Fundamentals基本原理
await coll.distinct('year');
Limit the Number of Documents Retrieved限制检索的文档数


API DocumentationAPI文档
Fundamentals基本原理
coll.find().limit(2);
Skip Retrieved Documents跳过检索到的文档


API DocumentationAPI文档
Fundamentals基本原理
coll.find({ title: { $regex: /^Rocky/} }, { skip: 2 });
Sort the Documents When Retrieving Them检索文档时对文档进行排序


API DocumentationAPI文档
Fundamentals基本原理
coll.find().sort({ year: 1});
Project Document Fields When Retrieving Them检索投影文档字段时


API DocumentationAPI文档
Fundamentals基本原理
coll.find().project({ _id: 0, year: 1, imdb: 1 });
Create an Index创建索引


API DocumentationAPI文档
Fundamentals基本原理
await coll.createIndex({ title: 1, year: -1 });
Search Text搜索文本


API DocumentationAPI文档
Fundamentals基本原理
// only searches fields with text indexes仅搜索具有文本索引的字段
coll.find({ $text: { $search: 'zissou' } });
Install the Driver Dependency安装驱动程序依赖项
package.json
"dependencies": {
"mongodb": "^5.7",
...
}