Docs 菜单
Docs 主页
/ / /
Node.js 驱动程序

快速参考

本页显示了若干 MongoDB 命令的驱动程序语法,以及指向相关引用和 API 文档的链接。

您可以使用 Node.js 驱动程序连接到以下环境中托管的部署并对其执行命令:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

  • MongoDB Enterprise:基于订阅、自行管理的 MongoDB 版本

  • MongoDB Community:source-available、免费使用且可自行管理的 MongoDB 版本

要详细了解如何在 Atlas 用户界面中为 MongoDB Atlas 中托管的部署执行常见的 CRUD 操作,请参阅创建、查看、更新和删除文档

命令
语法
Find a Document

Usage Example
await coll.findOne({ title: 'Hamlet' });
{ title: 'Hamlet', type: 'movie', ... }
coll.find({ year: 2005 });
[
{ title: 'Christmas in Boston', year: 2005, ... },
{ title: 'Chicken Little', 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 } }
);
{ title: 'Amadeus', imdb: { rating: 9.5, ... } }
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

await coll.updateOne(
{ title: 'Cosmos' },
{ $push: { genres: 'Educational' } }
):
{ title: 'Cosmos', genres: [ 'Documentary', 'Educational' ] }
await coll.replaceOne(
{ name: 'Deli Llama', address: '2 Nassau St' },
{ name: 'Lord of the Wings', zipcode: 10001 }
);
{ 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 } }
}
}
]);
BulkWriteResult {
result: {
...
},
...
}
coll.watch([ { $match: { year: { $gte: 2022 } } } ]);
Access Data from a Cursor Iteratively

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

const cursor = coll.find();
const results = await cursor.toArray();
[
{ title: '2001: A Space Odyssey', ... },
{ title: 'The Sound of Music', ... },
...
]
await coll.countDocuments({ year: 2000 });
618
List the Distinct Documents or Field Values
await coll.distinct('year');
[ 1891, 1893, 1894, 1896, 1903, ... ]
Limit the Number of Documents Retrieved

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

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

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

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 }},
...
]
await coll.createIndex({ title: 1, year: -1 });
// 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": "^6.10",
...
}

后退

后续步骤