-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayground-1.mongodb.js
59 lines (54 loc) · 1.84 KB
/
playground-1.mongodb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* global use, db */
// MongoDB Playground
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
// Make sure you are connected to enable completions and to be able to run a playground.
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.
// The result of the last command run in a playground is shown on the results panel.
// By default the first 20 documents will be returned with a cursor.
// Use 'console.log()' to print to the debug output.
// For more documentation on playgrounds please refer to
// https://www.mongodb.com/docs/mongodb-vscode/playgrounds/
// Select the database to use.
use("Mobile-App-Final");
// Insert a few documents into the sales collection.
db.getCollection("sales").insertMany([
{
item: "abc",
price: 10,
quantity: 2,
date: new Date("2014-03-01T08:00:00Z"),
},
{
item: "jkl",
price: 20,
quantity: 1,
date: new Date("2014-03-01T09:00:00Z"),
},
]);
// Run a find command to view items sold on April 4th, 2014.
const salesOnApril4th = db
.getCollection("sales")
.find({
date: { $gte: new Date("2014-04-04"), $lt: new Date("2014-04-05") },
})
.count();
// Print a message to the output window.
console.log(`${salesOnApril4th} sales occurred in 2014.`);
// Here we run an aggregation and open a cursor to the results.
// Use '.toArray()' to exhaust the cursor to return the whole result set.
// You can use '.hasNext()/.next()' to iterate through the cursor page by page.
db.getCollection("sales").aggregate([
// Find all of the sales that occurred in 2014.
{
$match: {
date: { $gte: new Date("2014-01-01"), $lt: new Date("2015-01-01") },
},
},
// Group the total sales for each product.
{
$group: {
_id: "$item",
totalSaleAmount: { $sum: { $multiply: ["$price", "$quantity"] } },
},
},
]);