Skip to content

Commit 7660346

Browse files
committed
Update MongoDB tutorial code examples and formatting
1 parent e2963d7 commit 7660346

File tree

1 file changed

+59
-35
lines changed

1 file changed

+59
-35
lines changed

docs/mongodb-rest/tutorial.adoc

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -53,47 +53,71 @@ echo '[
5353

5454
[source,javascript]
5555
----
56-
const data = [
57-
{ "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
58-
{ "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
59-
{ "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
60-
{ "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
61-
{ "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
62-
];
63-
6456
// Create collection
65-
fetch('[INSTANCE-URL]/inventory', {
66-
method: 'PUT',
67-
headers: {
68-
'Authorization': 'Basic [BASIC-AUTH]'
69-
}
70-
})
71-
.then(response => {
72-
if (response.ok) {
73-
console.log('Collection created successfully');
74-
} else {
75-
console.error('Failed to create collection:', response.status);
76-
}
77-
})
78-
.catch(error => console.error('Error:', error));
79-
80-
// Insert documents
81-
fetch('[INSTANCE-URL]/inventory', {
82-
method: 'POST',
83-
body: JSON.stringify(data),
57+
fetch("http://localhost:8080/inventory", {
58+
method: "PUT",
8459
headers: {
85-
'Authorization': 'Basic [BASIC-AUTH]',
86-
'Content-Type': 'application/json'
87-
}
88-
})
89-
.then(response => {
60+
Authorization: "Basic YWRtaW46c2VjcmV0",
61+
},
62+
}).then((response) => {
9063
if (response.ok) {
91-
console.log('Documents inserted successfully');
64+
console.log("Collection created successfully");
65+
66+
const data = [
67+
{
68+
item: "journal",
69+
qty: 25,
70+
size: { h: 14, w: 21, uom: "cm" },
71+
status: "A",
72+
},
73+
{
74+
item: "notebook",
75+
qty: 50,
76+
size: { h: 8.5, w: 11, uom: "in" },
77+
status: "A",
78+
},
79+
{
80+
item: "paper",
81+
qty: 100,
82+
size: { h: 8.5, w: 11, uom: "in" },
83+
status: "D",
84+
},
85+
{
86+
item: "planner",
87+
qty: 75,
88+
size: { h: 22.85, w: 30, uom: "cm" },
89+
status: "D",
90+
},
91+
{
92+
item: "postcard",
93+
qty: 45,
94+
size: { h: 10, w: 15.25, uom: "cm" },
95+
status: "A",
96+
},
97+
];
98+
99+
// Insert documents
100+
fetch("http://localhost:8080/inventory", {
101+
method: "POST",
102+
body: JSON.stringify(data),
103+
headers: {
104+
Authorization: "Basic YWRtaW46c2VjcmV0",
105+
"Content-Type": "application/json",
106+
},
107+
})
108+
.then((response) => {
109+
if (response.ok) {
110+
console.log("Documents inserted successfully");
111+
} else {
112+
console.error("Failed to insert documents:", response.status);
113+
}
114+
})
115+
.catch((error) => console.error("Error:", error));
92116
} else {
93-
console.error('Failed to insert documents:', response.status);
117+
console.error("Failed to create collection:", response.status);
94118
}
95119
})
96-
.catch(error => console.error('Error:', error));
120+
.catch((error) => console.error("Error:", error));
97121
----
98122

99123
=== Retrieving documents

0 commit comments

Comments
 (0)