Hướng dẫn how do i get data from two different collections of mongodb using node js? - làm cách nào để lấy dữ liệu từ hai bộ sưu tập mongodb khác nhau bằng cách sử dụng nút js?

Mongoose là thư viện mô hình dữ liệu đối tượng (ODM) cho MongoDB. Nó xác định một Schema được kích thước mạnh mẽ, với các giá trị mặc định và xác nhận lược đồ sau này được ánh xạ tới một tài liệu MongoDB. is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed-schema, with default values and schema validations which are later mapped to a MongoDB document.

Để nhận dữ liệu từ một bộ sưu tập với Mongoose trong NodeJS, bạn phải có hai điều cần thiết:

  1. Lược đồ: Đây là cấu trúc tài liệu chứa thuộc tính với các loại của nó (giá trị mặc định, xác nhận, v.v. khi được yêu cầu) như một cặp giá trị khóa. It is a document structure that contains the property with its types (default value, validations, etc. when required) as a key-value pair.
  2. Mô hình: Nó là một lớp được tạo ra với sự trợ giúp của lược đồ được xác định và tài liệu MongoDB là một ví dụ của mô hình. Do đó, nó hoạt động như một giao diện cho cơ sở dữ liệu MongoDB để tạo, đọc, cập nhật và xóa tài liệu.: It is a class created with the help of defined Schema and a MongoDB document is an instance of Model. Therefore, it acts as an interface for the MongoDB database for creating, reading, updating, and deleting a document.

Sau khi có một mô hình, chúng ta có thể sử dụng phương thức find () trên mô hình của một bộ sưu tập cụ thể để lấy tài liệu của bộ sưu tập.find() on the model of a particular collection to get documents of the collection.

Syntax: 

.find(,)
  • : Nó là tùy chọn. Nó chỉ định một bộ lọc lựa chọn được sử dụng để lọc các tài liệu bằng các toán tử truy vấn MongoDB khác nhau. Nếu không được thông qua, tất cả các tài liệu được trả lại.: It is optional. It specifies a selection filter that is used to filter documents using various MongoDB query operators. If not passed, all the documents are returned.
  • : Nó là tùy chọn. Nó chứa các trường mà chúng tôi muốn được trả lại cho các tài liệu phù hợp với bộ lọc truy vấn. Nếu không được thông qua, tất cả các trường được trả lại.: It is optional. It contains fields that we want to be returned to the documents that match the query filter. If not passed, all the fields are returned.

Cài đặt Mongoose:

Bước 1: Bạn có thể truy cập liên kết Cài đặt Mongoose để cài đặt mô -đun Mongoose. Bạn có thể cài đặt gói này bằng cách sử dụng lệnh này. You can visit the link Install mongoose to install the mongoose module. You can install this package by using this command.

npm install mongoose

Bước 2: Bây giờ bạn có thể nhập mô -đun Mongoose trong tệp của mình bằng cách sử dụng: Now you can import the mongoose module in your file using:

const mongoose = require('mongoose');

Implementation:

Bước 1: Tạo một thư mục và thêm các tệp model.js và main.js vào đó. Create a folder and add model.js and main.js files into it.

  • Model.js: Nó chứa các lược đồ và mô hình cho tất cả các bộ sưu tập bạn muốn sử dụng, và sau đó chúng tôi đang xuất tất cả các mô hình được tạo để chúng có thể được nhập vào tệp mà chúng tôi sẽ lấy dữ liệu từ các bộ sưu tập khác nhau. It contains schemas and models for all the collections you want to use, and then we are exporting all the models created so that they can be imported into the file in which we will get data from different collections.
  • main.js: Đây là tệp máy chủ chính ở đây chúng tôi sẽ nhận dữ liệu từ hai bộ sưu tập khác nhau. It is the main server file here we will get data from two different collections.

Bước 2: Viết mã sau trong tệp model.js. Write down the following code in the model.js file.

model.js

const mongoose = require('mongoose'__

const courseSchema = new

npm install mongoose
0

npm install mongoose
1
npm install mongoose
2

npm install mongoose
1
npm install mongoose
4

npm install mongoose
1
npm install mongoose
6

npm install mongoose
7

npm install mongoose
8new
npm install mongoose
0

npm install mongoose
1
npm install mongoose
4

npm install mongoose
1
const mongoose = require('mongoose');
4

npm install mongoose
1
const mongoose = require('mongoose');
6

npm install mongoose
7

const mongoose = require('mongoose');
8
const mongoose = require('mongoose');
9
mongoose.connect('mongodb://localhost:27017/GFG',
{  
  useNewUrlParser: true,  
  useUnifiedTopology: true,  
  useFindAndModify: false
});
0

mongoose.connect('mongodb://localhost:27017/GFG',
{  
  useNewUrlParser: true,  
  useUnifiedTopology: true,  
  useFindAndModify: false
});
1
mongoose.connect('mongodb://localhost:27017/GFG',
{  
  useNewUrlParser: true,  
  useUnifiedTopology: true,  
  useFindAndModify: false
});
2
mongoose.connect('mongodb://localhost:27017/GFG',
{  
  useNewUrlParser: true,  
  useUnifiedTopology: true,  
  useFindAndModify: false
});
3

mongoose.connect('mongodb://localhost:27017/GFG',
{  
  useNewUrlParser: true,  
  useUnifiedTopology: true,  
  useFindAndModify: false
});
4

npm install mongoose
1
mongoose.connect('mongodb://localhost:27017/GFG',
{  
  useNewUrlParser: true,  
  useUnifiedTopology: true,  
  useFindAndModify: false
});
6

mongoose.connect('mongodb://localhost:27017/GFG',
{  
  useNewUrlParser: true,  
  useUnifiedTopology: true,  
  useFindAndModify: false
});
7

Cơ sở dữ liệu: Chúng tôi đã có tài liệu trong các khóa học và bộ sưu tập sinh viên mà chúng tôi sẽ nhận được dữ liệu như hình dưới đây:We already have documents in our Courses and Students collections from which we are going to get data as shown below:

Hướng dẫn how do i get data from two different collections of mongodb using node js? - làm cách nào để lấy dữ liệu từ hai bộ sưu tập mongodb khác nhau bằng cách sử dụng nút js?

Các khóa học bộ sưu tập và sinh viên trong cơ sở dữ liệu GFG

Bước 3: Kết nối cơ sở dữ liệu có thể dễ dàng thiết lập bằng cách sử dụng Mongoose như: Database connection can be easily established using mongoose like:

mongoose.connect('mongodb://localhost:27017/GFG',
{  
  useNewUrlParser: true,  
  useUnifiedTopology: true,  
  useFindAndModify: false
});

Bước 4: Viết mã sau trong tệp Main.js. Write down the following code in the main.js file.

main.js

const mongoose = require('mongoose');

node main.js
1
node main.js
2);

npm install mongoose
1
node main.js
5

node main.js
6
node main.js
7
node main.js
8
node main.js
9

node main.js
6const mongoose = require(1
node main.js
8
node main.js
9

node main.js
6const mongoose = require(5const mongoose = require(6

npm install mongoose
1
npm install mongoose
7

const mongoose = require(9 'mongoose'0

'mongoose'1'mongoose'2 'mongoose'3

npm install mongoose
1'mongoose'5

node main.js
6'mongoose'7'mongoose'8'mongoose'9

node main.js
6);1

node main.js
6);3

);4);5

node main.js
6'mongoose'3

node main.js
6);9const courseSchema = 0const courseSchema = 1

);4'mongoose'5

const courseSchema = 4'mongoose'7const courseSchema = 6'mongoose'9

const courseSchema = 4);1

);4'mongoose'3

);4new3new4new5

const courseSchema = 4new7

);4'mongoose'3

npm install mongoose
1'mongoose'3

npm install mongoose
1new3new4new5

node main.js
6new7

npm install mongoose
1'mongoose'3

Bước 5: Chạy tệp Main.js bằng lệnh bên dưới: Run main.js file using the below command:

node main.js

Giải thích: Trong mã trên, trong tệp Main.js, chúng tôi sẽ nhận được tất cả các tài liệu của tập hợp khóa học trong danh mục là cơ sở dữ liệu sau đó lưu trữ _ID của mỗi khóa học trong mảng dbcof của cơ sở dữ liệu danh mục.In the above code, in the file main.js, we are getting all the documents of Course collectionwhose category is Database then storing _id of each course in dbcourse array then getting all the documents from the Student collection whose is enrolled in any course of category Database.

Đầu ra: Chúng tôi đang nhận dữ liệu từ hai khóa học bộ sưu tập khác nhau và sinh viên trong bảng điều khiển được hiển thị dưới đây: We are getting data from two different collections Courses and Students in the console shown below:

Hướng dẫn how do i get data from two different collections of mongodb using node js? - làm cách nào để lấy dữ liệu từ hai bộ sưu tập mongodb khác nhau bằng cách sử dụng nút js?

Đầu ra sau khi thực thi main.js


Làm thế nào để bạn truy xuất dữ liệu từ nhiều bộ sưu tập trong MongoDB Node JS?

Sau khi có một mô hình, chúng ta có thể sử dụng phương thức find () trên mô hình của một bộ sưu tập cụ thể để lấy tài liệu của bộ sưu tập. : Nó là tùy chọn. Nó chỉ định một bộ lọc lựa chọn được sử dụng để lọc các tài liệu bằng các toán tử truy vấn MongoDB khác nhau. Nếu không được thông qua, tất cả các tài liệu được trả lại.use method find() on the model of a particular collection to get documents of the collection. : It is optional. It specifies a selection filter that is used to filter documents using various MongoDB query operators. If not passed, all the documents are returned.

Làm thế nào để bạn nhận được dữ liệu từ hai bộ sưu tập trong MongoDB?

Để thực hiện MongoDB tham gia hai bộ sưu tập, bạn phải sử dụng toán tử tra cứu $.Nó được định nghĩa là một giai đoạn thực hiện tham gia bên ngoài bên trái với một bộ sưu tập khác và hỗ trợ lọc dữ liệu từ các tài liệu được nối.Ví dụ: nếu người dùng yêu cầu tất cả các lớp từ tất cả học sinh, thì truy vấn dưới đây có thể được viết: sinh viên.use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents. For example, if a user requires all grades from all students, then the below query can be written: Students.

Làm cách nào để kết nối hai cơ sở dữ liệu MongoDB với Node JS?

Làm cách nào để kết nối hai cơ sở dữ liệu MongoDB với Node JS ?..
Tạo DB.sơ đẳng.Tệp JS ..
Tạo DB.sơ trung.Tệp JS ..
Tạo chỉ mục.Tệp JS.Tệp này sẽ xuất tất cả các kết nối cơ sở dữ liệu ..
Cấu trúc thư mục của bạn sẽ trông như thế này ..
Khởi tạo kết nối cơ sở dữ liệu trong máy chủ.Tệp JS như bạn sẽ làm với kết nối mặc định ..

Chúng ta có thể có nhiều bộ sưu tập trong MongoDB không?

Có, bạn có thể có nhiều bộ sưu tập trong cơ sở dữ liệu bằng MongoDB..