Bây giờ, bạn có thể chọn một cơ sở dữ liệu bằng cách sử dụng phương pháp
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
3. Nếu cơ sở dữ liệu chưa tồn tại, nó sẽ được tạoVí dụ của chúng ta sẽ tạo một cơ sở dữ liệu có tên là “
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
4”, cơ sở dữ liệu này sẽ có một bộ sưu tập “export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
5”Cập nhật hàm
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
6 từ phần trướcexport async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
} finally {
await mongoClient.close[];
}
}
Tạo ra
Bây giờ bạn đã thiết lập cơ sở dữ liệu và bộ sưu tập, bạn có thể tạo tài liệu mới bằng cách sử dụng
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
7. Để tạo một tài liệu- Tạo một hàm không đồng bộ có tên là
8 và chuyểnexport async function createStudentDocument[collection] { const studentDocument = { name: 'John Smith', birthdate: new Date[2000, 11, 20], address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' }, }; await collection.insertOne[studentDocument]; }
9 làm tham sốexport async function createStudentDocument[collection] { const studentDocument = { name: 'John Smith', birthdate: new Date[2000, 11, 20], address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' }, }; await collection.insertOne[studentDocument]; }
- Bên trong hàm, tạo một đối tượng có tên là
0 và lưu trữ thông tin của sinh viên trong đóexport async function executeStudentCrudOperations[] { const uri = process.env.DB_URI; let mongoClient; try { mongoClient = await connectToCluster[uri]; const db = mongoClient.db['school']; const collection = db.collection['students']; console.log['CREATE Student']; await createStudentDocument[collection]; } finally { await mongoClient.close[]; } }
- Sử dụng
1, chuyển đối tượng vừa tạo làm tham số choexport async function executeStudentCrudOperations[] { const uri = process.env.DB_URI; let mongoClient; try { mongoClient = await connectToCluster[uri]; const db = mongoClient.db['school']; const collection = db.collection['students']; console.log['CREATE Student']; await createStudentDocument[collection]; } finally { await mongoClient.close[]; } }
7export async function createStudentDocument[collection] { const studentDocument = { name: 'John Smith', birthdate: new Date[2000, 11, 20], address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' }, }; await collection.insertOne[studentDocument]; }
Sao chép và dán hàm sau vào tệp “
export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
3” của bạn, ngay sau khi xuất hàm connectToClusterexport async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
Bây giờ bạn đã viết xong hàm, bạn phải gọi nó. Bên trong khối
export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
4 trong export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
6, sau khi bạn đã xác định bộ sưu tập, hãy gọi export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
6. Đảm bảo sử dụng await để tập lệnh chờ thực thi chức năng trước khi thoátThực thi tập lệnh “
export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
7” trong thiết bị đầu cuối của bạnexport async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
8Bạn sẽ thấy thông báo “CREATE Student” được đăng nhập trên thiết bị đầu cuối của bạn. Để xác minh rằng tài liệu đã được tạo, hãy điều hướng đến phiên bản MongoDB Atlas của bạn và chọn Duyệt bộ sưu tập. Sau khi được chọn, bạn sẽ thấy cơ sở dữ liệu, bộ sưu tập và tài liệu mới được tạo của mình
Mặc dù bạn chưa cung cấp trường
export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
8, hãy lưu ý rằng có một trường id cho tài liệu của bạn. Điều này là do nếu bạn không cung cấp trường id, cơ sở dữ liệu sẽ tự động tạo một id duy nhất cho tài liệuTìm thấy
Bạn có thể sử dụng phương thức find[] để lấy tài liệu từ cơ sở dữ liệu. Điều này yêu cầu bạn chuyển một đối tượng truy vấn với các thuộc tính tùy chọn để lọc cơ sở dữ liệu. Tương tự như hàm
export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
6, bạn sẽ tạo hàm export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
80Sao chép chức năng sau và thêm nó sau chức năng
export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
6export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
3Gọi hàm này bên trong khối
export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
4 và duy trì tài liệu đã truy xuất thành một biến mới. Sử dụng export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
83 để in biến. Đảm bảo nhận xét cuộc gọi đến hàm export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
6. Nếu không, tập lệnh sẽ tạo một tài liệu mới có cùng thông tin và id mớiexport async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
7Thực thi tập lệnh trong thiết bị đầu cuối của bạn
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
8Bạn sẽ thấy đối tượng
export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
0 đã đăng nhập trên thiết bị đầu cuối của mìnhĐể thử, hãy thử cập nhật tên thành John và để ý điều gì sẽ xảy ra
Sử dụng phương pháp
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
86 để cập nhật tài liệu. Phương thức này có hai tham số bắt buộc- Đối tượng bộ chọn truy vấn cho biết tài liệu nào cần được cập nhật
- Một đối tượng cho biết những thay đổi nào cần được áp dụng
Hãy tạo một hàm cập nhật các trường được chỉ định của tài liệu với một tên được chỉ định
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
1Gọi chức năng này và sau đó
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
80 một lần nữa để xác minh rằng tài liệu đã được cập nhậtexport async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
3Thực hiện kịch bản một lần nữa
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
8Bạn có thể so sánh các tài liệu đã ghi — trước và sau khi cập nhật. Nếu bạn làm mới trang Bản đồ MongoDB của mình, bạn cũng sẽ thấy tài liệu được cập nhật
Xóa bỏ
Để xóa tài liệu khỏi cơ sở dữ liệu, hãy sử dụng phương thức deleteMany[]. Phương thức này lấy một đối tượng chỉ định tài liệu nào cần xóa làm tham số đầu tiên của nó
Hãy tạo một hàm xóa tất cả các tài liệu có tên được chỉ định ngay sau hàm
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
88export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
0Gọi hàm bên trong khối
export async function executeStudentCrudOperations[] {
const uri = process.env.DB_URI;
let mongoClient;
try {
mongoClient = await connectToCluster[uri];
const db = mongoClient.db['school'];
const collection = db.collection['students'];
console.log['CREATE Student'];
await createStudentDocument[collection];
} finally {
await mongoClient.close[];
}
}
4. Bạn cũng có thể bỏ ghi chú các lệnh gọi hàm trước đóexport async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
1Thực thi tập lệnh lần cuối
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
8Bạn sẽ thấy rằng không tìm thấy tài liệu nào có tên đã chỉ định sau khi hàm
export async function createStudentDocument[collection] {
const studentDocument = {
name: 'John Smith',
birthdate: new Date[2000, 11, 20],
address: { street: 'Pike Lane', city: 'Los Angeles', state: 'CA' },
};
await collection.insertOne[studentDocument];
}
30 được thực thi. Làm mới trang MongoDB Atlas. Bạn sẽ thấy các tài liệu đã được gỡ bỏ