-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
aggregate.test.ts
101 lines (82 loc) · 3.15 KB
/
aggregate.test.ts
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
import { Schema, model, Document, Expression, PipelineStage, Types } from 'mongoose';
import { expectType } from 'tsd';
const schema: Schema = new Schema({ name: { type: 'String' } });
interface ITest extends Document {
name?: string;
}
const Test = model<ITest>('Test', schema);
Test.aggregate([{ $match: { name: 'foo' } }]).exec().then((res: any) => console.log(res));
Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).exec().then((res: Array<ITest>) => console.log(res[0].name));
Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).then((res: Array<ITest>) => console.log(res[0].name));
run().catch((err: Error) => console.log(err.stack));
async function run() {
const res: Array<ITest> = await Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).exec();
console.log(res[0].name);
const res2: Array<ITest> = await Test.aggregate<ITest>([{ $match: { name: 'foo' } }]);
console.log(res2[0].name);
expectType<number | undefined>(Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).options.maxTimeMS);
expectType<boolean | undefined>(Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).options.allowDiskUse);
Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).option({ maxTimeMS: 222 });
await Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).cursor().eachAsync(async(res) => {
console.log(res);
});
for await (const obj of Test.aggregate<ITest>()) {
obj.name;
}
function eachAsync(): void {
Test.aggregate().cursor().eachAsync((doc) => {
expectType<any>(doc);
});
Test.aggregate().cursor().eachAsync((docs) => {
expectType<any[]>(docs);
}, { batchSize: 2 });
Test.aggregate().cursor<ITest>().eachAsync((doc) => {
expectType<ITest>(doc);
});
Test.aggregate().cursor<ITest>().eachAsync((docs) => {
expectType<ITest[]>(docs);
}, { batchSize: 2 });
}
// Aggregate.prototype.sort()
expectType<ITest[]>(await Test.aggregate<ITest>().sort('-name'));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 1 }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: -1 }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'asc' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'ascending' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'desc' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'descending' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: { $meta: 'textScore' } }));
}
function gh12017_1() {
const a: Expression = { $subtract: [
{ $dayOfWeek: new Date() } as Expression.DayOfWeek, // errors
2
] };
}
function gh12017_2() {
const a: Expression.Reduce = {
$reduce: {
input: '$values',
initialValue: { depth: -1 }, // errors
in: {
depth: '$$this.depth' // errors
}
}
};
const b: Expression.Reduce = {
$reduce: {
input: '$values',
initialValue: 0,
in: { $add: ['$$value', '$$this'] }
}
};
const c: PipelineStage.Set = {
$set: {
child: {
foo: 'bar' // errors
},
friend: new Types.ObjectId()
}
};
const d: Expression.ToInt = { $toInt: 2.5 };
}