deno.land / x / mongoose@6.7.5 / test / docs / findoneandupdate.test.js

findoneandupdate.test.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
const assert = require('assert');const start = require('../common');
// This file is in `es-next` because it uses async/await for convenience
describe('Tutorial: findOneAndUpdate()', function() { const mongoose = new start.mongoose.Mongoose(); let Character;
before(async function() { await mongoose.connect(start.uri);
await mongoose.connection.dropDatabase(); });
beforeEach(async function() { mongoose.connection.deleteModel(/Character/); Character = mongoose.model('Character', new mongoose.Schema({ name: String, age: Number }));
await mongoose.model('Character').deleteMany({}); await Character.create({ name: 'Jean-Luc Picard' }); });
it('basic case', async function() { // acquit:ignore:start await mongoose.model('Character').deleteMany({}); mongoose.connection.deleteModel(/Character/); // acquit:ignore:end const Character = mongoose.model('Character', new mongoose.Schema({ name: String, age: Number }));
await Character.create({ name: 'Jean-Luc Picard' });
const filter = { name: 'Jean-Luc Picard' }; const update = { age: 59 };
// `doc` is the document _before_ `update` was applied let doc = await Character.findOneAndUpdate(filter, update); doc.name; // 'Jean-Luc Picard' doc.age; // undefined // acquit:ignore:start assert.equal(doc.name, 'Jean-Luc Picard'); assert.equal(doc.age, undefined); // acquit:ignore:end
doc = await Character.findOne(filter); doc.age; // 59 // acquit:ignore:start assert.equal(doc.name, 'Jean-Luc Picard'); assert.equal(doc.age, 59); // acquit:ignore:end });
it('new option', async function() { const filter = { name: 'Jean-Luc Picard' }; const update = { age: 59 };
// `doc` is the document _after_ `update` was applied because of // `new: true` let doc = await Character.findOneAndUpdate(filter, update, { new: true }); doc.name; // 'Jean-Luc Picard' doc.age; // 59 // acquit:ignore:start assert.equal(doc.name, 'Jean-Luc Picard'); assert.equal(doc.age, 59); // acquit:ignore:end });
it('returnOriginal option', async function() { const filter = { name: 'Jean-Luc Picard' }; const update = { age: 59 };
// `doc` is the document _after_ `update` was applied because of // `returnOriginal: false` let doc = await Character.findOneAndUpdate(filter, update, { returnOriginal: false }); doc.name; // 'Jean-Luc Picard' doc.age; // 59 // acquit:ignore:start assert.equal(doc.name, 'Jean-Luc Picard'); assert.equal(doc.age, 59); // acquit:ignore:end });
it('save race condition', async function() { const filter = { name: 'Jean-Luc Picard' }; const update = { age: 59 };
let doc = await Character.findOne({ name: 'Jean-Luc Picard' });
// Document changed in MongoDB, but not in Mongoose await Character.updateOne(filter, { name: 'Will Riker' });
// This will update `doc` age to `59`, even though the doc changed. doc.age = 59; await doc.save();
doc = await Character.findOne(); doc.name; // Will Riker doc.age; // 59 // acquit:ignore:start assert.equal(doc.name, 'Will Riker'); assert.equal(doc.age, 59); // acquit:ignore:end });
it('upsert', async function() { const filter = { name: 'Will Riker' }; const update = { age: 29 };
await Character.countDocuments(filter); // 0 // acquit:ignore:start assert.equal(await Character.countDocuments(filter), 0); // acquit:ignore:end
let doc = await Character.findOneAndUpdate(filter, update, { new: true, upsert: true // Make this update into an upsert }); doc.name; // Will Riker doc.age; // 29 // acquit:ignore:start assert.equal(doc.name, 'Will Riker'); assert.equal(doc.age, 29); // acquit:ignore:end });
it('rawResult', async function() { const filter = { name: 'Will Riker' }; const update = { age: 29 };
await Character.countDocuments(filter); // 0 // acquit:ignore:start assert.equal(await Character.countDocuments(filter), 0); // acquit:ignore:end
let res = await Character.findOneAndUpdate(filter, update, { new: true, upsert: true, rawResult: true // Return the raw result from the MongoDB driver });
res.value instanceof Character; // true // The below property will be `false` if MongoDB upserted a new // document, and `true` if MongoDB updated an existing object. res.lastErrorObject.updatedExisting; // false // acquit:ignore:start assert.ok(res.value instanceof Character); assert.ok(!res.lastErrorObject.updatedExisting); // acquit:ignore:end });});
mongoose

Version Info

Tagged at
a year ago