Hướng dẫn how can we join two tables in mongodb? - làm thế nào chúng ta có thể nối hai bảng trong mongodb?

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

Bạn có thể sử dụng $lookup để tạo chế độ xem qua hai bộ sưu tập và sau đó chạy các truy vấn so với chế độ xem. Các ứng dụng có thể truy vấn chế độ xem mà không phải xây dựng hoặc duy trì các đường ống phức tạp.

Tạo hai bộ sưu tập mẫu, inventoryorders:

db.inventory.insertMany( [
{ prodId: 100, price: 20, quantity: 125 },
{ prodId: 101, price: 10, quantity: 234 },
{ prodId: 102, price: 15, quantity: 432 },
{ prodId: 103, price: 17, quantity: 320 }
] )
db.orders.insertMany( [
{ orderId: 201, custid: 301, prodId: 100, numPurchased: 20 },
{ orderId: 202, custid: 302, prodId: 101, numPurchased: 10 },
{ orderId: 203, custid: 303, prodId: 102, numPurchased: 5 },
{ orderId: 204, custid: 303, prodId: 103, numPurchased: 15 },
{ orderId: 205, custid: 303, prodId: 103, numPurchased: 20 },
{ orderId: 206, custid: 302, prodId: 102, numPurchased: 1 },
{ orderId: 207, custid: 302, prodId: 101, numPurchased: 5 },
{ orderId: 208, custid: 301, prodId: 100, numPurchased: 10 },
{ orderId: 209, custid: 303, prodId: 103, numPurchased: 30 }
] )

Lệnh này sử dụng db.createView() để tạo chế độ xem mới có tên sales dựa trên bộ sưu tập orders:

db.createView( "sales", "orders", [
{
$lookup:
{
from: "inventory",
localField: "prodId",
foreignField: "prodId",
as: "inventoryDocs"
}
},
{
$project:
{
_id: 0,
prodId: 1,
orderId: 1,
numPurchased: 1,
price: "$inventoryDocs.price"
}
},
{ $unwind: "$price" }
] )

Trong ví dụ:

  • Giai đoạn $lookup sử dụng trường

    db.createView( "sales", "orders", [
    {
    $lookup:
    {
    from: "inventory",
    localField: "prodId",
    foreignField: "prodId",
    as: "inventoryDocs"
    }
    },
    {
    $project:
    {
    _id: 0,
    prodId: 1,
    orderId: 1,
    numPurchased: 1,
    price: "$inventoryDocs.price"
    }
    },
    { $unwind: "$price" }
    ] )
    2 trong bộ sưu tập orders để "tham gia" các tài liệu trong bộ sưu tập inventory có các trường
    db.createView( "sales", "orders", [
    {
    $lookup:
    {
    from: "inventory",
    localField: "prodId",
    foreignField: "prodId",
    as: "inventoryDocs"
    }
    },
    {
    $project:
    {
    _id: 0,
    prodId: 1,
    orderId: 1,
    numPurchased: 1,
    price: "$inventoryDocs.price"
    }
    },
    { $unwind: "$price" }
    ] )
    2 phù hợp.

  • Các tài liệu phù hợp được thêm vào dưới dạng một mảng trong trường

    db.createView( "sales", "orders", [
    {
    $lookup:
    {
    from: "inventory",
    localField: "prodId",
    foreignField: "prodId",
    as: "inventoryDocs"
    }
    },
    {
    $project:
    {
    _id: 0,
    prodId: 1,
    orderId: 1,
    numPurchased: 1,
    price: "$inventoryDocs.price"
    }
    },
    { $unwind: "$price" }
    ] )
    6.

  • Giai đoạn

    db.createView( "sales", "orders", [
    {
    $lookup:
    {
    from: "inventory",
    localField: "prodId",
    foreignField: "prodId",
    as: "inventoryDocs"
    }
    },
    {
    $project:
    {
    _id: 0,
    prodId: 1,
    orderId: 1,
    numPurchased: 1,
    price: "$inventoryDocs.price"
    }
    },
    { $unwind: "$price" }
    ] )
    7 chọn một tập hợp con của các trường có sẵn.

  • Giai đoạn

    db.createView( "sales", "orders", [
    {
    $lookup:
    {
    from: "inventory",
    localField: "prodId",
    foreignField: "prodId",
    as: "inventoryDocs"
    }
    },
    {
    $project:
    {
    _id: 0,
    prodId: 1,
    orderId: 1,
    numPurchased: 1,
    price: "$inventoryDocs.price"
    }
    },
    { $unwind: "$price" }
    ] )
    8 chuyển đổi trường
    db.createView( "sales", "orders", [
    {
    $lookup:
    {
    from: "inventory",
    localField: "prodId",
    foreignField: "prodId",
    as: "inventoryDocs"
    }
    },
    {
    $project:
    {
    _id: 0,
    prodId: 1,
    orderId: 1,
    numPurchased: 1,
    price: "$inventoryDocs.price"
    }
    },
    { $unwind: "$price" }
    ] )
    9 từ một mảng thành giá trị vô hướng.

Các tài liệu trong chế độ xem sales là:

{ orderId: 201, prodId: 100, numPurchased: 20, price: 20 },
{ orderId: 202, prodId: 101, numPurchased: 10, price: 10 },
{ orderId: 203, prodId: 102, numPurchased: 5, price: 15 },
{ orderId: 204, prodId: 103, numPurchased: 15, price: 17 },
{ orderId: 205, prodId: 103, numPurchased: 20, price: 17 },
{ orderId: 206, prodId: 102, numPurchased: 1, price: 15 },
{ orderId: 207, prodId: 101, numPurchased: 5, price: 10 },
{ orderId: 208, prodId: 100, numPurchased: 10, price: 20 },
{ orderId: 209, prodId: 103, numPurchased: 30, price: 17 }

Để tìm tổng số tiền được bán của mỗi sản phẩm, truy vấn xem:

db.sales.aggregate( [
{
$group:
{
_id: "$prodId",
amountSold: { $sum: { $multiply: [ "$price", "$numPurchased" ] } }
}
}
] )

Đầu ra là:

[
{ _id: 102, amountSold: 90 },
{ _id: 101, amountSold: 150 },
{ _id: 103, amountSold: 1105 },
{ _id: 100, amountSold: 600 }
]

Chúng ta có thể tham gia nhiều bảng trong MongoDB không?

Trong MongoDB, chúng ta có thể kết hợp dữ liệu của nhiều bộ sưu tập thành một thông qua giai đoạn tổng hợp tra cứu $.Trong đó, bạn phải chỉ định bộ sưu tập nào bạn muốn tham gia với bộ sưu tập hiện tại và chọn trường khớp với cả hai bộ sưu tập.we can combine data of multiple collections into one through the $lookup aggregation stage. In this, you have to specify which collection you want to join with the current collection and select the field that matches in both the collection.

Có thể tham gia các bảng trong MongoDB không?

May mắn thay, MongoDB tham gia có thể được thực hiện trong MongoDB 3.2 khi nó giới thiệu một hoạt động tra cứu mới có thể thực hiện các hoạt động tham gia trên các bộ sưu tập.MongoDB Joins can be performed in MongoDB 3.2 as it introduces a new Lookup operation that can perform Join operations on Collections.

Làm cách nào để tham gia hai bảng trong NoQuery?

Có hai cách mà bạn có thể tham gia các bảng trong cùng một hệ thống phân cấp trong cơ sở dữ liệu Oracle NoQuery ...
Điều khoản bàn lồng ..
Bên trái tham gia ..

Làm thế nào tìm nạp dữ liệu từ hai bảng trong MongoDB?

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.