-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayground-1.mongodb.js
More file actions
72 lines (61 loc) · 2.34 KB
/
playground-1.mongodb.js
File metadata and controls
72 lines (61 loc) · 2.34 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
/* 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/
// MongoDB Playground for Order Form
// Create a new database if it doesn't exist
use("artwork_orders");
// Create a collection for orders if it doesn't exist
db.createCollection("orders");
// Sample document insertion based on your form structure
db.orders.insertOne({
fullName: "John Doe",
email: "john.doe@example.com",
phone: "+1234567890",
preferredCommunication: "email",
artworkType: "painting",
medium: "oil",
size: 'medium (12-24")',
colorPalette: "Warm tones with earth colors",
imageUrls: ["https://example.com/reference1.jpg"],
purpose: "personal collection",
shippingAddress: {
addressLine1: "123 Art Street",
addressLine2: "Apt 456",
city: "Creative City",
state: "Artistic State",
postalCode: "12345",
country: "United States",
},
additionalNotes: "I'd like the artwork to be inspired by African landscapes",
newsletter: true,
orderDate: new Date(),
});
// Query to find all orders
db.orders.find();
// Query to find orders by email
db.orders.find({ email: "john.doe@example.com" });
// Query to find orders by artwork type
db.orders.find({ artworkType: "painting" });
// Query to find orders from a specific country
db.orders.find({ "shippingAddress.country": "United States" });
// Create index on email for faster queries
db.orders.createIndex({ email: 1 });
// Create index on orderDate for sorting
db.orders.createIndex({ orderDate: -1 });
// Aggregation to get count of orders by artwork type
db.orders.aggregate([
{ $group: { _id: "$artworkType", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
]);
// Aggregation to get count of orders by country
db.orders.aggregate([
{ $group: { _id: "$shippingAddress.country", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
]);