


{
"_id":"ALFKI",
"CUSTOMER":{
"customer code":"ALFKI",
"company name":"Alfreds Futterkiste",
"contact name":"Maria Anders",
"contact title":"Sales Representative",
"address":"Obere Str. 57",
"city":"Berlin",
"region":null,
"postal code":"12209",
"country":"Germany",
"phone":"030-0074321",
"fax":"030-0076545",
"submits":[ ]
}
}
{
"_id":"FAMIA",
"CUSTOMER":{
"customer code":"FAMIA",
... ... ... ... ... ...
"submits":[
{
"ORDER":{
"order id":328,
... ... ... ... ...
"consists of":[
{
"ORDER DETAIL":{
"product name":"Louisiana Fiery Hot Pepper Sauce",
... ... ... ... ...
}
},
{
"ORDER DETAIL":{
"product name":"Raclette Courdavault",
... ... ... ... ...
}
}
]
}
}
] } }
{
"_id":"Karkki Oy",
"SUPPLIER":{
"company name":"Karkki Oy",
"contact name":"Anne Heikkonen",
"contact title":"Product Manager",
"address":"Valtakatu 12",
... ... ... ... ... ...
"supplies":[
{
"PRODUCT":{
"product name":"Maxilaku",
"category name":"Confections",
... ... ... ... ...
}
},
{
"PRODUCT":{
"product name":"Valkoinen suklaa",
"category name":"Confections",
... ... ... ... ...
}
}
]
}
}
db.orders.aggregate([ {$project:{"CUSTOMER.company name":1,"_id":0}} ])
{ "CUSTOMER" : { "company name" : "Alfreds Futterkiste" } }
{ "CUSTOMER" : { "company name" : "Ana Trujillo Emparedados y helados" } }
{ "CUSTOMER" : { "company name" : "Antonio Moreno Taquería" } }
... ... ...
db.orders.aggregate([ {$project:{"Company":"$CUSTOMER.company name","_id":0}} ])
{ "Company" : "Alfreds Futterkiste" }
{ "Company" : "Ana Trujillo Emparedados y helados" }
{ "Company" : "Antonio Moreno Taquería" }
... ... ...
db.orders.aggregate([ {$project:{"Customer address":"$CUSTOMER.address","_id":0}} ])
{ "Customer address" : "Obere Str. 57" }
{ "Customer address" : "Avda. de la Constitución 2222" }
{ "Customer address" : "Mataderos 2312" }
... ... ...
db.orders.aggregate([ {$project:{"Customer address":{$concat:["$CUSTOMER.address",
"-",
"$CUSTOMER.customer code"]}}}])
{ "_id" : "ALFKI", "Customer address" : "Johan Strauss Str. 23-ALFKI" }
{ "_id" : "ANATR", "Customer address" : "Avda. de la Constitución 2222-ANATR" }
{ "_id" : "ANTON", "Customer address" : "Mataderos 2312-ANTON" }
... ... ...
db.orders.aggregate([{$match:{"SUPPLIER.city":"Sandvika"}}])
db.orders.aggregate([{$match:{"SUPPLIER.city":"Sandvika"}},
{$project:{"SUPPLIER.company name":1,"SUPPLIER.city":1,
"SUPPLIER.supplies.PRODUCT.product name":1,"_id":0}}])
db.orders.aggregate([{$match:{"SUPPLIER.country":"Germany"}},
{$match:{"SUPPLIER.supplies.PRODUCT.product name":"Rossle Sauerkraut"}},
{$project:{"SUPPLIER.company name":1,"SUPPLIER.city":1,
"SUPPLIER.supplies.PRODUCT.product name":1,"_id":0}}])
db.orders.aggregate([{$match:{"SUPPLIER.city":"Sandvika"}},
{$project:{"SUPPLIER.company name":1}},
{$project:{"SUPPLIER.city":1}},
{$project:{"SUPPLIER.supplies.PRODUCT.product name":1,"_id":0}}])
{ "SUPPLIER" : { } })
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$limit:2}])
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$skip:2}])
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$limit:4},
{$skip:2}])
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$skip:2},
{$limit:2}])
db.orders.aggregate([{$match:{"CUSTOMER.country":"France"}},
{$sample:{size:2}}])
db.orders.aggregate([{$count:"Total documents"}])
{ "Total documents" : 117 }
db.orders.aggregate([{$match:{"SUPPLIER.country":"Canada"}},
{$count:"Total suppliers in Canada"}]))
{ "Total suppliers in Canada" : 2 }
db.orders.aggregate([{$match:{"SUPPLIER.country":"Canada"}},
{$project:{"SUPPLIER.company name":1,"_id":0}},
{$sort:{"SUPPLIER.company name":1}}])
db.orders.aggregate([{$sort:{"SUPPLIER.company name":1}},
{$match:{"SUPPLIER.country":"Canada"}},
{$project:{"SUPPLIER.company name":1,"_id":0}} ]))
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$project:{"SUPPLIER.company name":1,"SUPPLIER.postal code":1,"_id":0}},
{$sort:{"SUPPLIER.postal code":-1}},
{$limit:1}])))
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$limit:2},
{$project:{"SUPPLIER.supplies.PRODUCT.product name":1,"_id":0}}])
{ "SUPPLIER" : { "supplies" : [ { "PRODUCT" : { "product name" : "Côte de Blaye" } } ] } }
{ "SUPPLIER" : { "supplies" : [ { "PRODUCT" : { "product name" : "Sasquatch Ale" } },
{ "PRODUCT" : { "product name" : "Steeleye Stout" } },
{ "PRODUCT" : { "product name" : "Laughing Lumberjack Lager" } } ] } }
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$limit:2},
{$project:{"product":"$SUPPLIER.supplies.PRODUCT.product name","_id":0}}])
{ "product" : [ "Côte de Blaye" ] }
{ "product" : [ "Sasquatch Ale", "Steeleye Stout", "Laughing Lumberjack Lager" ] }
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$limit:2},
{$unwind:"$SUPPLIER.supplies"},
{$project:{"product":"$SUPPLIER.supplies.PRODUCT.product name","_id":0}}])
{ "product" : "Côte de Blaye" }
{ "product" : "Sasquatch Ale" }
{ "product" : "Steeleye Stout" }
{ "product" : "Laughing Lumberjack Lager" }
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$limit:2},
{$project:{"supplier":"$SUPPLIER.company name",
"product":"$SUPPLIER.supplies.PRODUCT.product name",
"_id":0}}])
{ "supplier" : "Aux joyeux ecclesiastiques", "product" : [ "Côte de Blaye" ] }
{ "supplier" : "Bigfoot Breweries", "product" : [ "Sasquatch Ale", "Steeleye Stout", "Laughing Lumberjack Lager" ]
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$limit:2},
{$unwind:"$SUPPLIER.supplies"},
{$project:{"supplier":"$SUPPLIER.company name",
"product":"$SUPPLIER.supplies.PRODUCT.product name",
"_id":0}}])
{ "supplier" : "Aux joyeux ecclesiastiques", "product" : "Côte de Blaye" }
{ "supplier" : "Bigfoot Breweries", "product" : "Sasquatch Ale" }
{ "supplier" : "Bigfoot Breweries", "product" : "Steeleye Stout" }
{ "supplier" : "Bigfoot Breweries", "product" : "Laughing Lumberjack Lager" }
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$limit:2},
{$unwind:"$SUPPLIER.supplies"},
{$project:{"product":"$SUPPLIER.supplies.PRODUCT.product name","_id":0}},
{$count:"Total number of products supplied by the first 2 suppliers"}])
{ "Total number of products supplied by the first 2 suppliers" : 4 }
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$count:"total"}])
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$group:{"_id": null,"total":{$sum:1}}}])}
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$project:{"country":"$SUPPLIER.country","_id":0}}])
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$project:{"country":"$SUPPLIER.country","_id":0}},
{$group:{"_id":"$country"}}]))
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$group:{"_id":"$SUPPLIER.country","total":{$sum:1}}}])
{ "_id" : "Italy", "total" : 2 }
{ "_id" : "Sweden", "total" : 1 }
{ "_id" : "Germany", "total" : 3 }
... ... ...
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$project:{"country":"$SUPPLIER.country","_id":0}},
{$group:{"_id":"$country","total":{$sum:1}}}])
{ "_id" : "Italy", "total" : 2 }
{ "_id" : "Sweden", "total" : 1 }
{ "_id" : "Germany", "total" : 3 }
... ... ...
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$unwind:"$SUPPLIER.supplies"},
{$project:{"supplier":"$SUPPLIER.company name",
"product":"$SUPPLIER.supplies.PRODUCT.product name",
"_id":0}} ])
{ "supplier" : "Aux joyeux ecclesiastiques", "product" : "Côte de Blaye" }
{ "supplier" : "Bigfoot Breweries", "product" : "Sasquatch Ale" }
{ "supplier" : "Bigfoot Breweries", "product" : "Steeleye Stout" }
{ "supplier" : "Bigfoot Breweries", "product" : "Laughing Lumberjack Lager" }
... ... ...
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$unwind:"$SUPPLIER.supplies"},
{$project:{"supplier":"$SUPPLIER.company name",
"product":"$SUPPLIER.supplies.PRODUCT.product name",
"_id":0}},
{$group:{"_id":"$supplier","total":{$sum:1}}}])
{ "_id" : "Aux joyeux ecclesiastiques", "total" : 1 }
{ "_id" : "Bigfoot Breweries", "total" : 3 }
... ... ...
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$unwind:"$SUPPLIER.supplies"},
{$project:{"supplier":"$SUPPLIER.company name",
"price":"$SUPPLIER.supplies.PRODUCT.unit price",
"_id":0}} ])
{ "supplier" : "Aux joyeux ecclesiastiques", "price" : 263.5 }
{ "supplier" : "Bigfoot Breweries", "price" : 14 }
{ "supplier" : "Bigfoot Breweries", "price" : 18 }
{ "supplier" : "Bigfoot Breweries", "price" : 14 }
... ... ...
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$unwind:"$SUPPLIER.supplies"},
{$project:{"supplier":"$SUPPLIER.company name",
"product":"$SUPPLIER.supplies.PRODUCT.product name",
"price":"$SUPPLIER.supplies.PRODUCT.unit price",
"_id":0}},
{$group:{"_id":"$supplier","total":{$sum:"$price"}}}])
{ "_id" : "Aux joyeux ecclesiastiques", "total" : 263.5 }
{ "_id" : "Bigfoot Breweries", "total" : 46 }
... ... ...
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$unwind:"$SUPPLIER.supplies"},
{$project:{"supplier":"$SUPPLIER.company name",
"price":"$SUPPLIER.supplies.PRODUCT.unit price",
"_id":0}},
{$group:{"_id":"$supplier","average":{$avg:"$price"}}}])}
{ "_id" : "Aux joyeux ecclesiastiques", "average" : 263.5 }
{ "_id" : "Bigfoot Breweries", "average" : 15.333333333333334}
... ... ...
db.orders.aggregate([{$match:{"SUPPLIER":{$exists:true}}},
{$unwind:"$SUPPLIER.supplies"},
{$project:{"supplier":"$SUPPLIER.company name",
"price":"$SUPPLIER.supplies.PRODUCT.unit price",
"_id":0}},
{$group:{"_id":"$supplier","min":{$min:"$price"},"max":{$max:"$price"}}}])
{ "_id" : "Aux joyeux ecclesiastiques", "min" : 263.5, "max" : 263.5 }
{ "_id" : "Bigfoot Breweries", "min" : 14, "max" : 18 }
... ... ...
db.orders.aggregate([{$match:{"CUSTOMER.customer code":"AROUT"}},
{$project:{"Code":"$CUSTOMER.customer code",
"Purchased":"$CUSTOMER.submits.ORDER.consists of.ORDER DETAIL.product name"}} ])
{ "_id" : "AROUT","Code" : "AROUT", "Purchased" : [
[
"Gorgonzola Telino",
"Grandma's Boysenberry Spread",
"Konbu",
"Mozzarella di Giovanni",
"Tofu"
]
]
}
db.orders.aggregate([{$match:{"CUSTOMER.customer code":"AROUT"}},
{$project:{"Code":"$CUSTOMER.customer code",
"Purchased":"$CUSTOMER.submits.ORDER.consists of.ORDER DETAIL.product name"}},
{$unwind:"$Purchased"}])
{ "_id" : "AROUT","Code" : "AROUT", "Purchased" : [
"Gorgonzola Telino",
"Grandma's Boysenberry Spread",
"Konbu",
"Mozzarella di Giovanni",
"Tofu"
]
}
db.orders.aggregate([{$match:{"CUSTOMER.customer code":"AROUT"}},
{$project:{"Code":"$CUSTOMER.customer code",
"Purchased":"$CUSTOMER.submits.ORDER.consists of.ORDER DETAIL.product name"}},
{$unwind:"$Purchased"},
{$unwind:"$Purchased"}])
{ "_id" : "AROUT", "Code" : "AROUT", "Purchased" : "Gorgonzola Telino" }
{ "_id" : "AROUT", "Code" : "AROUT", "Purchased" : "Grandma's Boysenberry Spread" }
{ "_id" : "AROUT", "Code" : "AROUT", "Purchased" : "Konbu" }
{ "_id" : "AROUT", "Code" : "AROUT", "Purchased" : "Mozzarella di Giovanni" }
{ "_id" : "AROUT", "Code" : "AROUT", "Purchased" : "Tofu" }
db.orders.aggregate([{$match:{"CUSTOMER.customer code":"AROUT"}},
{$project:{"Code":"$CUSTOMER.customer code",
"Purchased":"$CUSTOMER.submits.ORDER.consists of.ORDER DETAIL.product name"}},
{$unwind:"$Purchased"},
{$unwind:"$Purchased"},
{$project:{"_id":0}},
{$out:"purchases"}])
db.purchases.find().pretty()
db.orders.aggregate([{$project:{"product":"$SUPPLIER.supplies.PRODUCT",
"_id":0 }} ])
{ "product" : [ { "product name" : "Chef Anton's Cajun Seasoning",
"category name" : "Condiments",
"quantity per unit" : "48 - 6 oz jars",
"unit price" : 25,
"units in stock" : 53,
"units on order" : 0,
"reorder level" : 0,
"discontinued" : "N"
},
... ... ...
db.orders.aggregate([{$project:{"product":"$SUPPLIER.supplies.PRODUCT",
"_id":0 }},
{$unwind:"$product"} ])
db.orders.aggregate([{$project:{"product":"$SUPPLIER.supplies.PRODUCT",
"_id":0 }},
{$unwind:"$product"},
{$project:{"name":"$product.product name",
"category":"$product.category name",
"price":"$product.unit price"}} ])
{ "name" : "Côte de Blaye", "category" : "Beverages", "price" : 263.5 }
{ "name" : "Sasquatch Ale", "category" : "Beverages", "price" : 14 }
{ "name" : "Steeleye Stout", "category" : "Beverages", "price" : 18 }
... ... ...
db.orders.aggregate([{$project:{"product":"$SUPPLIER.supplies.PRODUCT",
"_id":0 }},
{$unwind:"$product"},
{$project:{"name":"$product.product name",
"category":"$product.category name",
"price":"$product.unit price"}},
{$out:"products"}])
db.products.find().pretty()
{ "_id" : ObjectId("61342804f26c5a766e0e1c6f"),
"Code" : "AROUT", "Purchased" : "Gorgonzola Telino" }
{ "_id" : ObjectId("61342804f26c5a766e0e1c70"),
"Code" : "AROUT", "Purchased" : "Grandma's Boysenberry Spread" }
... ... ...
{ "_id" : ObjectId("61342773f26c5a766e0e1c1a"),
"name" : "Gorgonzola Telino","category" : "Dairy Products","price" : 12.5 }
{ "_id" : ObjectId("61342773f26c5a766e0e1c22"),
"name" : "Grandma's Boysenberry Spread","category" : "Condiments","price" : 25 }
... ... ...
db.purchases.aggregate([{$lookup: {from:"products",
localField:"Purchased",
foreignField:"name",
as:"result"}} ])
{ "_id" : ObjectId("61342804f26c5a766e0e1c6f"),
"Code" : "AROUT","Purchased" : "Gorgonzola Telino",
"result" : [ {"_id" : ObjectId("61342773f26c5a766e0e1c1a"),
"name" : "Gorgonzola Telino",
"category" : "Dairy Products",
"price" : 12.5}
]
}
{ "_id" : ObjectId("61342804f26c5a766e0e1c70"),
"Code" : "AROUT","Purchased" : "Grandma's Boysenberry Spread",
"result" : [ { "_id" : ObjectId("61342773f26c5a766e0e1c22"),
"name" : "Grandma's Boysenberry Spread",
"category" : "Condiments",
"price" : 25}
]
}
... ... ...
db.products.aggregate([{$lookup: {from:"purchases",
localField:"name",
foreignField:"Purchased",
as:"result"}} ])
{"_id" : ObjectId("61342773f26c5a766e0e1c22"), "name" : "Grandma's Boysenberry Spread","category" : "Condiments", "price" : 25,
"result" : [{"_id" : ObjectId("61342804f26c5a766e0e1c70"),"Code" : "AROUT","Purchased" : "Grandma's Boysenberry Spread"}]
}
{"_id" : ObjectId("61342773f26c5a766e0e1c1a"),"name" : "Gorgonzola Telino","category" : "Dairy Products","price" : 12.5,
"result" : [{"_id" : ObjectId("61342804f26c5a766e0e1c6f"),"Code" : "AROUT","Purchased" : "Gorgonzola Telino"}]
}
... ... ...
{"_id" : ObjectId("61342773f26c5a766e0e1c0e"),"name" : "Côte de Blaye","category" : "Beverages","price" : 263.5,
"result" : [ ]
}
{"_id" : ObjectId("61342773f26c5a766e0e1c0f"),"name" : "Sasquatch Ale","category" : "Beverages","price" : 14,
"result" : [ ]
}
... ... ...