-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.js
84 lines (50 loc) · 2.32 KB
/
simple.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const mdb = require("../index")
async function main()
{
const client = await mdb.connect("mysql://root:123456@192.168.1.4:3310", { log: console.log })
const db = client.db("project")
const collection = db.collection("user")
collection.createIndex({ name: 1 }, { unique: 1 })
collection.createIndex({ union: 1, member: 1 }, { unique: 1 })
collection.insertOne({ _id: 1, name: "张三", union: 1, member: 3, lvl: 10, temp: "this is a test" })
collection.updateOne({ _id: 2 }, { $set: { name: "李四", union: 2, member: 3, lvl: 10 } }, { upsert: 1 })
let cursor = await collection.find({})
console.table(cursor.toArray())
collection.updateOne({ _id: 1 }, { $set: { is_new: true } })
let data = await collection.findOne({ _id: 1 })
console.dir(data)
data = await collection.findOne({ name: "张三" }, { projection: { name: 1 } })
console.dir(data)
data = await collection.findOne({ union: 1, member: 3 }, { projection: { name: 1 } })
console.dir(data)
data = await collection.findOne({ lvl: 10 }, { projection: { name: 1 } })
console.dir(data)
await collection.deleteOne({ _id: 1 })
cursor = await collection.find({})
console.table(cursor.toArray())
await collection.deleteMany({})
}
async function test_auto()
{
const client = await mdb.connect("mysql://root:123456@192.168.1.4", { log: console.log })
const db = client.db("project")
const collection = db.collection("auto")
collection.createIndex({ name: 1 }, { unique: 1 })
collection.createIndex({ union: 1, member: 1 }, { unique: 1 })
collection.insertOne({ name: "张三", union: 1, member: 3, lvl: 10, is_new: true })
collection.insertOne({ name: "李四", union: 2, member: 3, lvl: 5 })
let cursor = await collection.find({})
console.table(cursor.toArray())
let data = await collection.findOne({ name: "张三" }, { projection: { name: 1 } })
console.dir(data)
data = await collection.findOne({ union: 1, member: 3 }, { projection: { name: 1 } })
console.dir(data)
data = await collection.findOne({ lvl: 10 }, { projection: { name: 1 } })
console.dir(data)
await collection.deleteOne({ _id: 1 })
cursor = await collection.find({})
console.table(cursor.toArray())
await collection.deleteMany({})
}
main()
// test_auto()