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. Show Để 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:
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:
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.
Bước 2: Viết mã sau trong tệp model.js. Write down the following code in the model.js file. model.js
npm install mongoose0 npm install mongoose1 npm install mongoose2 npm install mongoose1 npm install mongoose4 npm install mongoose1 npm install mongoose6 npm install mongoose7 npm install mongoose8 new npm install mongoose0 npm install mongoose1 npm install mongoose4 npm install mongoose1 const mongoose = require('mongoose');4 npm install mongoose1 const mongoose = require('mongoose');6 npm install mongoose7 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 mongoose1 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: 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
node main.js1 node main.js2 ); npm install mongoose1 node main.js5 node main.js6 node main.js7 node main.js8 node main.js9 node main.js6 const mongoose = require( 1node main.js8 node main.js9 node main.js6 const mongoose = require( 5const mongoose = require( 6npm install mongoose1 npm install mongoose7
npm install mongoose1 'mongoose' 5node main.js6 'mongoose' 7'mongoose' 8'mongoose' 9node main.js6 ); 1node main.js6 ); 3
node main.js6 'mongoose' 3node main.js6 ); 9const courseSchema = 0const courseSchema = 1
npm install mongoose1 'mongoose' 3npm install mongoose1 new 3new 4new 5node main.js6 new 7npm install mongoose1 'mongoose' 3Bướ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: Đầ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.
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.. |