Node.js MongoDB Join - W3Schools
Có thể bạn quan tâm
Join Collections
MongoDB is not a relational database, but you can perform a left outer join by using the $lookup stage.
The $lookup stage lets you specify which collection you want to join with the current collection, and which fields that should match.
Consider you have a "orders" collection and a "products" collection:
orders
[ { _id: 1, product_id: 154, status: 1 }]products
[ { _id: 154, name: 'Chocolate Heaven' }, { _id: 155, name: 'Tasty Lemons' }, { _id: 156, name: 'Vanilla Dreams' }]Example
Join the matching "products" document(s) to the "orders" collection:
let MongoClient = require('mongodb').MongoClient;let url = "mongodb://127.0.0.1:27017/"; MongoClient.connect(url, function(err, db) { if (err) throw err; let dbo = db.db("mydb"); dbo.collection('orders').aggregate([ { $lookup: { from: 'products', localField: 'product_id', foreignField: '_id', as: 'orderdetails' } } ]).toArray(function(err, res) { if (err) throw err; console.log(JSON.stringify(res)); db.close(); });}); Run example »Save the code above in a file called "demo_mongodb_join.js" and run the file:
Run "demo_mongodb_join.js"
C:\Users\Your Name>node demo_mongodb_join.jsWhich will give you this result:
[ { "_id": 1, "product_id": 154, "status": 1, "orderdetails": [ { "_id": 154, "name": "Chocolate Heaven" } ] }]As you can see from the result above, the matching document from the products collection is included in the orders collection as an array.
❮ Previous Next ❯ ★ +1 Sign in to track progressTừ khóa » Join Bảng Trong Mongodb
-
Join Collection Trong MongoDB Trên Node.js
-
So Sánh JOINS Trong MongoDB Và PostgreSQL - Viblo
-
Join Nhiều Document Trong Mongodb Thế Nào | A Community Node.js
-
Làm Cách Nào để Tôi Thực Hiện SQL Join Tương đương Trong ...
-
3 Cách Tiếp Cận để Tạo SQL-Join Tương đương Trong MongoDB
-
Cách Nối Hai Bảng Trong đó Một Bảng Chứa Danh Sách ID Của Một ...
-
MongoDB - Xin Chào! Mình Là Dzung
-
Những Câu Lệnh Cơ Bản Trong MongoDB - TRẦN VĂN BÌNH MASTER
-
Populate Trong Mongoose
-
Sử Dụng JOIN Trong SQLite - Hoclaptrinh
-
Mô Hình Hóa Dữ Liệu Trong MongoDB - Vi
-
CẬP NHẬT SQL Với JOIN - W3seo Sử Dụng Update Và Join
-
Tối ưu Truy Vấn Db | TechHay Blog
-
How To Join Data In MongoDB - Rockset