-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmoke.bnl
More file actions
75 lines (60 loc) · 2.14 KB
/
Copy pathsmoke.bnl
File metadata and controls
75 lines (60 loc) · 2.14 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
73
74
75
import "mongodb" as mongo;
function main() {
try {
var client = wait mongo.connect("mongodb://127.0.0.1:27017/mytest_db");
print("--- version ---");
print("driver:", mongo.version());
print("--- ping ---");
wait client.ping();
print("ping ok");
var db = client.db("bnl_test");
var users = db.collection("users");
print("--- reset ---");
try { wait users.drop(); } catch (e) { /* collection may not exist */ }
print("--- insert ---");
var ins = wait users.insert_one({name: "Alice", age: 30});
print("inserted_id type:", type(ins.inserted_id)); // map ({$oid: ...})
print("n:", ins.n);
var many = wait users.insert_many([
{name: "Bob", age: 25},
{name: "Carol", age: 28}
]);
print("inserted:", many.n);
print("--- find ---");
var rows = wait users.find();
print("rows:", rows.length);
var i = 0;
while (i < rows.length) {
print(" ", rows[i].name, rows[i].age);
i = i + 1;
}
print("--- find_one ---");
var alice = wait users.find_one({name: "Alice"});
print("alice:", alice.name, "age:", alice.age);
print("--- count ---");
var n = wait users.count_documents({});
print("count:", n);
print("--- update ---");
var u = wait users.update_one({name: "Alice"}, {"$inc": {age: 1}});
print("matched:", u.matched, "modified:", u.modified);
var alice2 = wait users.find_one({name: "Alice"});
print("alice age:", alice2.age);
print("--- delete ---");
var d = wait users.delete_one({name: "Bob"});
print("deleted:", d.deleted);
print("--- error (bad command) ---");
try {
wait db.run_command({nosuchcommand: 1});
print("FAIL");
} catch (e) {
print("caught code:", e.code, "name:", e.codeName);
}
print("--- cleanup ---");
wait users.drop();
client.close();
print("done");
} catch (err) {
print("FATAL:", err);
}
}
main();