deno.land / x / mongoose@6.7.5 / test / schema.select.test.js

schema.select.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
'use strict';
/** * Test dependencies. */
const start = require('./common');
const assert = require('assert');const mongoose = start.mongoose;const Schema = mongoose.Schema;
describe('schema select option', function() { let db;
before(function() { db = start(); });
after(async function() { await db.close(); });
beforeEach(() => db.deleteModel(/.*/)); afterEach(() => require('./util').clearTestData(db)); afterEach(() => require('./util').stopRemainingOps(db));
it('excluding paths through schematype', function(done) { const schema = new Schema({ thin: Boolean, name: { type: String, select: false }, docs: [new Schema({ bool: Boolean, name: { type: String, select: false } })] });
const S = db.model('Test', schema); S.create({ thin: true, name: 'the excluded', docs: [{ bool: true, name: 'test' }] }, function(err, s) { assert.ifError(err); assert.equal(s.name, 'the excluded'); assert.equal(s.docs[0].name, 'test');
let pending = 5; const item = s;
function cb(err, s) { pending--;
if (Array.isArray(s)) { s = s[0]; } assert.strictEqual(null, err); assert.equal(s.isSelected('name'), false); assert.equal(s.isSelected('docs.name'), false); assert.strictEqual(undefined, s.name); // we need to make sure this executes absolutely last. if (pending === 1) { S.findOneAndRemove({ _id: item._id }, cb); } if (pending === 0) { done(); } }
S.findById(s).select('-thin -docs.bool').exec(cb); S.find({ _id: s._id }).select('thin docs.bool').exec(cb); S.findById(s, cb); S.findOneAndUpdate({ _id: s._id }, { name: 'changed' }, cb); }); });
it('including paths through schematype', function(done) { const schema = new Schema({ thin: Boolean, name: { type: String, select: true }, docs: [new Schema({ bool: Boolean, name: { type: String, select: true } })] });
const S = db.model('Test', schema); S.create({ thin: true, name: 'the included', docs: [{ bool: true, name: 'test' }] }, function(err, s) { assert.ifError(err); assert.equal(s.name, 'the included'); assert.equal(s.docs[0].name, 'test');
let pending = 4;
function cb(err, s) { pending--;
if (Array.isArray(s)) { s = s[0]; } assert.strictEqual(null, err); assert.strictEqual(true, s.isSelected('name')); assert.strictEqual(true, s.isSelected('docs.name')); assert.equal(s.name, 'the included');
if (pending === 0) { done(); } }
S.findById(s).select('-thin -docs.bool').exec(function(err, res) { cb(err, res); S.find({ _id: s._id }).select('thin docs.bool').exec(function(err, res) { cb(err, res); S.findOneAndUpdate({ _id: s._id }, { thin: false }, function(err, s) { cb(err, s); S.findOneAndRemove({ _id: s._id }, cb); }); }); }); }); });
describe('overriding schematype select options', function() { let selected, excluded, S, E;
before(function() { selected = new Schema({ thin: Boolean, name: { type: String, select: true }, docs: [new Schema({ name: { type: String, select: true }, bool: Boolean })] }); excluded = new Schema({ thin: Boolean, name: { type: String, select: false }, docs: [new Schema({ name: { type: String, select: false }, bool: Boolean })] });
S = db.model('Test1', selected); E = db.model('Test2', excluded); });
describe('works', function() { describe('for inclusions', function() { let s; beforeEach(function(done) { S.create({ thin: true, name: 'the included', docs: [{ name: 'test', bool: true }] }, function(err, s_) { assert.ifError(err); s = s_; assert.equal(s.name, 'the included'); assert.equal(s.docs[0].name, 'test'); done(); }); }); it('with find', function(done) { S.find({ _id: s._id }).select('thin name docs.bool docs.name').exec(function(err, s) { assert.ifError(err); assert.ok(s && s.length > 0, 'no document found'); s = s[0]; assert.strictEqual(true, s.isSelected('name')); assert.strictEqual(true, s.isSelected('thin')); assert.strictEqual(true, s.isSelected('docs.name')); assert.strictEqual(true, s.isSelected('docs.bool')); assert.equal(s.name, 'the included'); assert.equal(s.docs[0].name, 'test'); assert.ok(s.thin); assert.ok(s.docs[0].bool); done(); }); }); it('for findById', function(done) { S.findById(s).select('-name -docs.name').exec(function(err, s) { assert.strictEqual(null, err); assert.equal(s.isSelected('name'), false); assert.equal(s.isSelected('thin'), true); assert.equal(s.isSelected('docs.name'), false); assert.equal(s.isSelected('docs.bool'), true); assert.ok(s.isSelected('docs')); assert.strictEqual(undefined, s.name); assert.strictEqual(undefined, s.docs[0].name); assert.equal(s.thin, true); assert.equal(s.docs[0].bool, true); done(); }); }); it('with findOneAndUpdate', function(done) { S.findOneAndUpdate({ _id: s._id }, { name: 'changed' }, { new: true }).select('thin name docs.bool docs.name').exec(function(err, s) { assert.ifError(err); assert.strictEqual(true, s.isSelected('name')); assert.strictEqual(true, s.isSelected('thin')); assert.strictEqual(true, s.isSelected('docs.name')); assert.strictEqual(true, s.isSelected('docs.bool')); assert.equal(s.name, 'changed'); assert.equal(s.docs[0].name, 'test'); assert.ok(s.thin); assert.ok(s.docs[0].bool); done(); }); }); it('for findByIdAndUpdate', function(done) { S.findByIdAndUpdate(s, { thin: false }, { new: true }).select('-name -docs.name').exec(function(err, s) { assert.strictEqual(null, err); assert.equal(s.isSelected('name'), false); assert.equal(s.isSelected('thin'), true); assert.equal(s.isSelected('docs.name'), false); assert.equal(s.isSelected('docs.bool'), true); assert.strictEqual(undefined, s.name); assert.strictEqual(undefined, s.docs[0].name); assert.equal(s.thin, false); assert.equal(s.docs[0].bool, true); done(); }); }); });
describe('for exclusions', function() { let e; beforeEach(function(done) { E.create({ thin: true, name: 'the excluded', docs: [{ name: 'test', bool: true }] }, function(err, e_) { e = e_; assert.ifError(err); assert.equal(e.name, 'the excluded'); assert.equal(e.docs[0].name, 'test'); done(); }); }); it('with find', function(done) { E.find({ _id: e._id }).select('thin name docs.name docs.bool').exec(function(err, e) { e = e[0]; assert.strictEqual(null, err); assert.equal(e.isSelected('name'), true); assert.equal(e.isSelected('thin'), true); assert.equal(e.isSelected('docs.name'), true); assert.equal(e.isSelected('docs.bool'), true); assert.equal(e.name, 'the excluded'); assert.equal(e.docs[0].name, 'test'); assert.ok(e.thin); assert.ok(e.docs[0].bool); done(); }); }); it('with findById', function(done) { E.findById(e).select('-name -docs.name').exec(function(err, e) { assert.strictEqual(null, err); assert.equal(e.isSelected('name'), false); assert.equal(e.isSelected('thin'), true); assert.equal(e.isSelected('docs.name'), false); assert.equal(e.isSelected('docs.bool'), true); assert.strictEqual(undefined, e.name); assert.strictEqual(undefined, e.docs[0].name); assert.strictEqual(true, e.thin); assert.strictEqual(true, e.docs[0].bool); done(); }); }); it('with findOneAndUpdate', function(done) { E.findOneAndUpdate({ _id: e._id }, { name: 'changed' }, { new: true }).select('thin name docs.name docs.bool').exec(function(err, e) { assert.strictEqual(null, err); assert.equal(e.isSelected('name'), true); assert.equal(e.isSelected('thin'), true); assert.equal(e.isSelected('docs.name'), true); assert.equal(e.isSelected('docs.bool'), true); assert.equal(e.name, 'changed'); assert.equal(e.docs[0].name, 'test'); assert.ok(e.thin); assert.ok(e.docs[0].bool); done(); }); }); it('with findOneAndRemove', function(done) { E.findOneAndRemove({ _id: e._id }).select('-name -docs.name').exec(function(err, e) { assert.strictEqual(null, err); assert.equal(e.isSelected('name'), false); assert.equal(e.isSelected('thin'), true); assert.equal(e.isSelected('docs.name'), false); assert.equal(e.isSelected('docs.bool'), true); assert.strictEqual(undefined, e.name); assert.strictEqual(undefined, e.docs[0].name); assert.strictEqual(true, e.thin); assert.strictEqual(true, e.docs[0].bool); done(); }); }); }); }); });
describe('exclusion in root schema should override child schema', function() { it('works (gh-1333)', function(done) { const m = new mongoose.Mongoose(); const child = new Schema({ name1: { type: String, select: false }, name2: { type: String, select: true } }); const selected = new Schema({ docs: { type: [child], select: false } }); const M = m.model('Test', selected);
const query = M.findOne(); query._applyPaths(); assert.equal(Object.keys(query._fields).length, 1); assert.equal(query._fields['docs.name1'], undefined); assert.equal(query._fields['docs.name2'], undefined); assert.equal(query._fields.docs, 0); done(); });
it('with nested (gh-7945)', function() { const child = new Schema({ name1: { type: String, select: false }, name2: { type: String, select: true } }); const selected = new Schema({ parent: { docs: { type: [child], select: false } } }); const M = db.model('Test', selected);
const query = M.findOne(); query._applyPaths(); assert.equal(Object.keys(query._fields).length, 1); assert.equal(query._fields['parent.docs.name1'], undefined); assert.equal(query._fields['parent.docs.name2'], undefined); assert.equal(query._fields['parent.docs'], 0);
return M.create({ parent: { docs: [{ name1: 'foo', name2: 'bar' }] } }). then(() => query). then(doc => { assert.ok(!doc.parent.docs); }); }); });
it('should not project in discriminator key if projected in implicitly with .$ (gh-9361)', function() { const eventSchema = new Schema({ message: String }, { discriminatorKey: 'kind', _id: false });
const batchSchema = new Schema({ events: [eventSchema] }); batchSchema.path('events').discriminator('Clicked', new Schema({ element: String }, { _id: false })); batchSchema.path('events').discriminator('Purchased', new Schema({ product: String }, { _id: false }));
const MyModel = db.model('Test', batchSchema);
const query = MyModel.find({ 'events.message': 'foo' }).select({ 'events.$': 1 }); query._applyPaths();
assert.equal(Object.keys(query._fields).length, 1); assert.ok(query._fields['events.$']); });
describe('forcing inclusion of a deselected schema path', function() { it('works', function(done) { const excluded = new Schema({ thin: Boolean, name: { type: String, select: false }, docs: [new Schema({ name: { type: String, select: false }, bool: Boolean })] });
const M = db.model('Test', excluded); M.create({ thin: false, name: '1 meter', docs: [{ name: 'test', bool: false }] }, function(err, d) { assert.ifError(err);
M.findById(d) .select('+name +docs.name') .exec(function(err, doc) { assert.ifError(err); assert.equal(doc.thin, false); assert.equal(doc.name, '1 meter'); assert.equal(doc.docs[0].bool, false); assert.equal(doc.docs[0].name, 'test'); assert.equal(d.id, doc.id);
M.findById(d) .select('+name -thin +docs.name -docs.bool') .exec(function(err, doc) { assert.ifError(err); assert.equal(doc.thin, undefined); assert.equal(doc.name, '1 meter'); assert.equal(doc.docs[0].bool, undefined); assert.equal(doc.docs[0].name, 'test'); assert.equal(d.id, doc.id);
M.findById(d) .select('-thin +name -docs.bool +docs.name') .exec(function(err, doc) { assert.ifError(err); assert.equal(doc.thin, undefined); assert.equal(doc.name, '1 meter'); assert.equal(doc.docs[0].bool, undefined); assert.equal(doc.docs[0].name, 'test'); assert.equal(d.id, doc.id);
M.findById(d) .select('-thin -docs.bool') .exec(function(err, doc) { assert.ifError(err); assert.equal(doc.thin, undefined); assert.equal(doc.name, undefined); assert.equal(doc.docs[0].bool, undefined); assert.equal(doc.docs[0].name, undefined); assert.equal(d.id, doc.id); done(); }); }); }); }); }); });
it('works with query.slice (gh-1370)', function(done) { const M = db.model('Test', new Schema({ many: { type: [String], select: false } }));
M.create({ many: ['1', '2', '3', '4', '5'] }, function(err) { if (err) { return done(err); }
const query = M.findOne().select('+many').where('many').slice(2);
query.exec(function(err, doc) { if (err) { return done(err); } assert.equal(doc.many.length, 2); done(); }); }); });
it('ignores if path does not have select in schema (gh-6785)', async function() { const M = db.model('Test', new Schema({ a: String, b: String }));
await M.create({ a: 'foo', b: 'bar' });
const doc = await M.findOne().select('+a'); assert.equal(doc.a, 'foo'); assert.equal(doc.b, 'bar'); });
it('omits if not in schema (gh-7017)', async function() { const M = db.model('Test', new Schema({ a: { type: String, select: false }, b: { type: String, select: false } }), 'Test');
await db.$initialConnection; await db.collection('Test').insertOne({ a: 'foo', b: 'bar', c: 'baz' });
const q = M.find({}).select('+c'); const doc = await q.then(res => res[0]); assert.deepEqual(q._fields, { a: 0, b: 0 });
assert.strictEqual(doc.a, void 0); assert.strictEqual(doc.b, void 0); assert.equal(doc.toObject().c, 'baz'); }); });
it('conflicting schematype path selection should not error', function(done) { const schema = new Schema({ thin: Boolean, name: { type: String, select: true }, conflict: { type: String, select: false } });
const S = db.model('Test', schema); S.create({ thin: true, name: 'bing', conflict: 'crosby' }, function(err, s) { assert.strictEqual(null, err); assert.equal(s.name, 'bing'); assert.equal(s.conflict, 'crosby');
let pending = 2;
function cb(err, s) { if (!--pending) { done(); } if (Array.isArray(s)) { s = s[0]; } assert.ifError(err); assert.equal(s.name, 'bing'); assert.equal(s.conflict, undefined); }
S.findById(s).exec(cb); S.find({ _id: s._id }).exec(cb); }); });
it('selecting _id works with excluded schematype path', function(done) { const schema = new Schema({ name: { type: String, select: false } });
const M = db.model('Test', schema); M.find().select('_id').exec(function(err) { assert.ifError(err, err && err.stack); done(); }); });
it('selecting _id works with excluded schematype path on sub doc', function(done) { const schema = new Schema({ docs: [new Schema({ name: { type: String, select: false } })] });
const M = db.model('Test', schema); M.find().select('_id').exec(function(err) { assert.ifError(err, err && err.stack); done(); }); });
it('all inclusive/exclusive combos work', function(done) { const coll = 'Test';
const schema = new Schema({ name: { type: String }, age: Number }, { collection: coll }); const M = db.model('Test1', schema);
const schema1 = new Schema({ name: { type: String, select: false }, age: Number }, { collection: coll }); const S = db.model('Test2', schema1);
const schema2 = new Schema({ name: { type: String, select: true }, age: Number }, { collection: coll }); const T = db.model('Test3', schema2);
function useId(M, id, cb) { M.findOne().select('-_id name').exec(function(err, d) { // mongo special case for exclude _id + include path assert.ifError(err); assert.equal(d.id, undefined); assert.equal(d.name, 'ssd'); assert.equal(d.age, undefined); M.findOne().select('-_id -name').exec(function(err, d) { assert.ifError(err); assert.equal(d.id, undefined); assert.equal(d.name, undefined); assert.equal(d.age, 0); M.findOne().select('_id name').exec(function(err, d) { assert.ifError(err); assert.equal(d.id, id); assert.equal(d.name, 'ssd'); assert.equal(d.age, undefined); cb(); }); }); }); }
function nonId(M, id, cb) { M.findOne().select('age -name').exec(function(err, d) { assert.ok(err); assert.ok(!d); M.findOne().select('-age name').exec(function(err, d) { assert.ok(err); assert.ok(!d); M.findOne().select('-age -name').exec(function(err, d) { assert.ifError(err); assert.equal(d.id, id); assert.equal(d.name, undefined); assert.equal(d.age, undefined); M.findOne().select('age name').exec(function(err, d) { assert.ifError(err); assert.equal(d.id, id); assert.equal(d.name, 'ssd'); assert.equal(d.age, 0); cb(); }); }); }); }); }
M.create({ name: 'ssd', age: 0 }, function(err, d) { assert.ifError(err); const id = d.id; useId(M, id, function() { nonId(M, id, function() { useId(S, id, function() { nonId(S, id, function() { useId(T, id, function() { nonId(T, id, function() { done(); }); }); }); }); }); }); }); });
it('does not set defaults for nested objects (gh-4707)', function(done) { const userSchema = new Schema({ name: String, age: Number, profile: { email: String, flags: { type: String, default: 'bacon' } } });
const User = db.model('Test', userSchema);
const obj = { name: 'Val', age: 28, profile: { email: 'test@a.co', flags: 'abc' } }; User.create(obj). then(function(user) { return User.findById(user._id). select('name profile.email'); }). then(function(user) { assert.ok(!user.profile.flags); done(); }). catch(done); });
it('does not create nested objects if not included (gh-4669)', function(done) { const schema = new Schema({ field1: String, field2: String, field3: { f1: String, f2: { type: String, default: 'abc' } }, field4: { f1: String }, field5: String }, { minimize: false });
const M = db.model('Test', schema); const obj = { field1: 'a', field2: 'b', field3: { f1: 'c', f2: 'd' }, field4: { f1: 'e' }, field5: 'f' }; M.create(obj, function(error, doc) { assert.ifError(error); M.findOne({ _id: doc._id }, 'field1 field2', function(error, doc) { assert.ifError(error); assert.ok(!doc.toObject({ minimize: false }).field3); assert.ok(!doc.toObject({ minimize: false }).field4); done(); }); }); });
it('initializes nested defaults with selected objects (gh-2629)', function(done) { const NestedSchema = new mongoose.Schema({ nested: { name: { type: String, default: 'val' } } });
const Model = db.model('Test', NestedSchema);
const doc = new Model(); doc.nested.name = undefined; doc.save(function(error) { assert.ifError(error); Model.findOne({}, { nested: 1 }, function(error, doc) { assert.ifError(error); assert.equal(doc.nested.name, 'val'); done(); }); }); });});
mongoose

Version Info

Tagged at
a year ago