Nota
L'accesso a questa pagina richiede l'autorizzazione. Puoi provare ad accedere o a cambiare directory.
L'accesso a questa pagina richiede l'autorizzazione. Puoi provare a cambiare directory.
La fase $lookup in Aggregation Framework viene usata per eseguire left outer join con altre raccolte. Consente di combinare documenti di raccolte diverse in base a una condizione specificata. Questo operatore è utile per arricchire i documenti con dati correlati di altre raccolte senza dover eseguire più query.
Sintassi
{
$lookup: {
from: <collection to join>,
localField: <field from input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}
Parametri
| Parametro | Description |
|---|---|
from |
Nome della raccolta con cui eseguire il join. |
localField |
Campo dei documenti di input corrispondenti all'oggetto foreignField. |
foreignField |
Campo dei documenti dell'insieme from che corrispondono a localField. |
as |
Nome del nuovo campo dell'array da aggiungere ai documenti di input. Questo array contiene i documenti corrispondenti dell'insieme from . |
Esempi
Si consideri questo documento di esempio dalla raccolta negozi.
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
Si supponga di avere un'altra ratings raccolta con due documenti.
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"rating": 5
}
{
"_id": "fecca713-35b6-44fb-898d-85232c62db2f",
"rating": 3
}
Esempio 1: Combinare due raccolte per elencare gli eventi promozionali per gli store con una valutazione pari a 5
Questa query unisce la ratings raccolta alla stores raccolta per elencare gli eventi promozionali correlati a ogni archivio con una valutazione di 5.
db.ratings.aggregate([
// filter based on rating in ratings collection
{
$match: {
"rating": 5
}
},
// find related documents in stores collection
{
$lookup: {
from: "stores",
localField: "_id",
foreignField: "_id",
as: "storeEvents"
}
},
// deconstruct array to output a document for each element of the array
{
$unwind: "$storeEvents"
},
// Include only _id and name fields in the output
{ $project: { _id: 1, "storeEvents.name": 1 } }
])
Questa query restituisce il risultato seguente:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"storeEvents": { "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile" }
}
]
Esempio 2: Unire due raccolte (classificazioni e archivi) usando una variabile dalle classificazioni.
db.ratings.aggregate([
{
$match: { rating: 5 }
},
{
$lookup: {
from: "stores",
let: { id: "$_id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$_id", "$$id"] }
}
},
{
$project: { _id: 0, name: 1 }
}
],
as: "storeInfo"
}
},
{
$unwind: "$storeInfo"
},
{
$project: {
_id: 1,
rating: 1,
"storeInfo.name": 1
}
}
])
Questa query restituisce il risultato seguente:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"rating": 5,
"storeInfo": {
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile"
}
}
]
Contenuti correlati
- Esaminare le opzioni per la migrazione da MongoDB ad Azure DocumentDB.
- Altre informazioni sulla compatibilità delle funzionalità con MongoDB.