Cách kết nối mongodb với nút js cục bộ

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ạo

Ví 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ước

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');
   } 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à
    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);
    }
    8 và chuyể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);
    }
    9 làm tham số
  • Bên trong hàm, tạo một đối tượng có tên là
    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 và lưu trữ thông tin của sinh viên trong đó
  • Sử dụ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();
       }
    }
    1, chuyển đối tượng vừa tạo làm tham số cho
    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

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 connectToCluster

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â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át

________số 8

Thự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ạ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);
}
8

Bạ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ệu

Tì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);
}
80

Sao 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();
   }
}
6

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

Gọ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ớ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);
}
7

Thự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);
}
8

Bạ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);
}
1

Gọ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ật

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

Thự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);
}
8

Bạ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);
}
88

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);
}
0

Gọ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);
}
1

Thự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);
}
8

Bạ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ỏ

MongoDB kết nối cục bộ như thế nào?

Bạn có thể kết nối với máy chủ MongoDB cục bộ đang chạy với cấu hình mặc định của nó bằng cách nhập. .
mongo. .
Phiên bản vỏ MongoDB v4. 4. 6. .
mongo --username --password. .. .
Phiên bản vỏ MongoDB v. 4. 4. 6. .
mongo "mongodb://:@127.0.0.1" .. .
mongo. .
hiển thị dbs. .
sử dụng quản trị viên

Bạn sẽ kết nối cơ sở dữ liệu MongoDB với nút js như thế nào?

Kết nối MongoDB . var MongoClient = yêu cầu('mongodb'). MongoClient; . MongoClient. connect("mongodb. //máy chủ cục bộ. 27017/MyDb", function (err, db) { if(err) throw err; // Viết cơ sở dữ liệu Chèn/Cập nhật/Mã truy vấn tại đây. });

MongoDB có thể được lưu trữ cục bộ không?

Cài đặt Phiên bản máy chủ cộng đồng MongoDB cho phép chúng tôi lưu trữ cục bộ cơ sở dữ liệu MongoDB không giống như Atlas là tùy chọn cơ sở dữ liệu được lưu trữ trên đám mây.

Chúng ta có thể sử dụng MongoDB với nút js không?

MongoDB là một nền tảng dữ liệu định hướng tài liệu đa năng, hiện đại đã được ghép nối rộng rãi với Node. js trong ngăn xếp công nghệ phổ biến như ngăn xếp MEAN (MongoDB, Express. js, AngularJS và Nút. js) và ngăn xếp MERN (MongoDB, Express.