문서 메뉴
문서 홈
/ / /
Node.js

빠른 참조

이 페이지에서는 여러 MongoDB 명령의 드라이버 구문과 관련 참조 및 API 문서에 대한 링크를 보여줍니다.

다음 환경에서 호스팅되는 배포에 Node.js 드라이버를 연결해 명령을 실행할 수 있습니다.

  • MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스

  • MongoDB Enterprise: 구독 기반의 자체 관리형 MongoDB 버전입니다.

  • MongoDB Community: 소스 사용 가능하고, 무료로 사용할 수 있는 자체 관리형 MongoDB 버전

MongoDB Atlas에서 호스팅되는 배포를 위해 Atlas UI에서 일반적인 CRUD 작업을 수행하는 방법에 대해 자세히 알아보려면 문서 생성, 보기, 업데이트 및 삭제를 참조하세요.

명령
구문
Find a Document

Usage Example
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 } }
);
Update Arrays in Documents

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 } } } ]);
Access Data from a Cursor Iteratively

const cursor = coll.find();
for await (const doc of cursor) {
console.dir(doc);
}
Access Data from a Cursor as an Array

const cursor = coll.find();
const results = await cursor.toArray();
await coll.countDocuments({ year: 2000 });
List the Distinct Documents or Field Values
await coll.distinct('year');
Limit the Number of Documents Retrieved

coll.find().limit(2);
Skip Retrieved Documents

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

coll.find().sort({ year: 1});
Project Document Fields When Retrieving Them

coll.find().project({ _id: 0, year: 1, imdb: 1 });
await coll.createIndex({ title: 1, year: -1 });
// only searches fields with text indexes
coll.find({ $text: { $search: 'zissou' } });
Install the Driver Dependency
package.json
"dependencies": {
"mongodb": "^5.7",
...
}

돌아가기

다음 단계