-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo-delete.js
More file actions
51 lines (48 loc) · 1.4 KB
/
Copy pathmongo-delete.js
File metadata and controls
51 lines (48 loc) · 1.4 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
const { MongoClient } = require("mongodb");
const url = "mongodb://127.0.0.1/";
const client = new MongoClient(url);
const obj ={
id: 8888,
first_name: 'Emili',
last_name: 'Heelis',
email: 'aheelislm@about.me',
gender: 'Female',
ip_address: '179.44.12.193',
company: 'Tanoodle',
};
// (async () => {
// try {
// await client.connect()
// const db = client.db('test')
// const collection = db.collection('users')
// let result = await collection.findOne({id: 8888})
// console.log(result)
// await collection.deleteMany({id: 8888})
// result = await collection.findOne({id: 8888})
// console.log( result)
// } catch (err) {
// console.error(err)
// } finally {
// client.close()
// }
// })(); // это hard delete, т.е полное удаление из БД
(async () => {
try {
await client.connect()
const db = client.db("test");
const collection = db.collection("users");
let result = await collection.findOne({ id: 16 });
await collection.updateMany({gender: 'M'}, {
$set: {
isDeleted: true
}},
{ multi: true, upsert: false}
)
result = await collection.find({ gender: "M", isDeleted: false }).toArray();
console.log(result);
} catch (err) {
console.error(err)
} finally {
client.close()
}
})(); // это soft delete, т.е не удаляются из БД, а помечаются,как удаленые