123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998 |
- import * as assert from 'power-assert'
- import tcb from '../../../src/index'
- import * as Config from '../../config.local'
- import * as common from '../../common/index'
- const app = tcb.init(Config)
- const db = app.database()
- describe.skip('test/unit/collection.test.ts', () => {
- const collName = 'coll-1'
- const collection = db.collection(collName)
- const command = db.command
- const _ = command
- it('operator', async () => {
- let a
- a = _.gt(4)
- const result = await collection.where({ a }).update({ c: { d: _.mul(3) } })
- })
- })
- describe('stdDevPop', () => {
- let studentsCollection = null
- const collectionName = 'test-students'
- const data = [
- { group: 'a', score: 84 },
- { group: 'a', score: 96 },
- { group: 'b', score: 80 },
- { group: 'b', score: 100 }
- ]
- beforeAll(async () => {
- studentsCollection = await common.safeCollection(db, collectionName)
- const success = await studentsCollection.create(data)
- assert.strictEqual(success, true)
- })
- afterAll(async () => {
- const success = await studentsCollection.remove()
- assert.strictEqual(success, true)
- })
- it('计算不同组的标准差', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .group({
- _id: '$group',
- stdDev: $.stdDevPop('$score')
- })
- .end()
- const stdDevs = {
- a: null,
- b: null
- }
- result.data.forEach(item => (stdDevs[item._id] = item.stdDev))
- // a分组标准差为: 6; b分组标准差为: 10
- assert.strictEqual(stdDevs.a, 6)
- assert.strictEqual(stdDevs.b, 10)
- })
- })
- describe('stdDevSamp', () => {
- let studentsCollection = null
- const collectionName = 'test-students'
- const data = [{ score: 80 }, { score: 100 }]
- beforeAll(async () => {
- studentsCollection = await common.safeCollection(db, collectionName)
- const success = await studentsCollection.create(data)
- assert.strictEqual(success, true)
- })
- afterAll(async () => {
- const success = await studentsCollection.remove()
- assert.strictEqual(success, true)
- })
- it('计算标准样本偏差', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .group({
- _id: null,
- ageStdDev: $.stdDevSamp('$score')
- })
- .end()
- // data 的标准样本偏差为 14.14
- assert.strictEqual(result.data[0].ageStdDev.toFixed(2), '14.14')
- })
- })
- describe('sum', () => {
- let goodsCollection = null
- const $ = db.command.aggregate
- const collectionName = 'test-goods'
- const data = [
- { cost: -10, price: 100 },
- { cost: -15, price: 1 },
- { cost: -10, price: 10 }
- ]
- beforeAll(async () => {
- goodsCollection = await common.safeCollection(db, collectionName)
- const success = await goodsCollection.create(data)
- assert.strictEqual(success, true)
- })
- afterAll(async () => {
- const success = await goodsCollection.remove()
- assert.strictEqual(success, true)
- })
- it('参数为单独字段', async () => {
- const result = await db
- .collection(collectionName)
- .aggregate()
- .group({
- _id: null,
- totalPrice: $.sum('$price')
- })
- .end()
- assert.strictEqual(result.data[0].totalPrice, 111)
- })
- it('参数为字段列表', async () => {
- const result = await db
- .collection(collectionName)
- .aggregate()
- .group({
- _id: null,
- totalProfit: $.sum($.sum(['$price', '$cost']))
- })
- .end()
- assert.strictEqual(result.data[0].totalProfit, 76)
- })
- })
- describe('let', () => {
- let goodsCollection = null
- const $ = db.command.aggregate
- const collectionName = 'test-goods'
- const data = [
- { cost: -10, discount: 0.95, price: 100 },
- { cost: -15, discount: 0.98, price: 1 },
- { cost: -10, discount: 1, price: 10 }
- ]
- beforeAll(async () => {
- goodsCollection = await common.safeCollection(db, collectionName)
- const success = await goodsCollection.create(data)
- assert.strictEqual(success, true)
- })
- afterAll(async () => {
- const success = await goodsCollection.remove()
- assert.strictEqual(success, true)
- })
- it('参数为单独字段', async () => {
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- profit: $.let({
- vars: {
- priceTotal: $.multiply(['$price', '$discount'])
- },
- in: $.sum(['$$priceTotal', '$cost'])
- })
- })
- .end()
- assert.strictEqual(result.data.length, 3)
- })
- })
- describe('条件操作符', () => {
- let goodsCollection = null
- const $ = db.command.aggregate
- const collectionName = 'test-goods'
- const data = [
- { name: 'a', amount: 100, desc: 'A' },
- { name: 'b', amount: 200, desc: null },
- { name: 'c', amount: 300 }
- ]
- beforeAll(async () => {
- goodsCollection = await common.safeCollection(db, collectionName)
- const success = await goodsCollection.create(data)
- assert.strictEqual(success, true)
- })
- afterAll(async () => {
- const success = await goodsCollection.remove()
- assert.strictEqual(success, true)
- })
- it('cond', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- name: 1,
- discount: $.cond({
- if: $.gte(['$amount', 200]),
- then: 0.7,
- else: 0.9
- })
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { name: 'a', discount: 0.9 },
- { name: 'b', discount: 0.7 },
- { name: 'c', discount: 0.7 }
- ])
- })
- it('ifNull', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- name: 1,
- desc: $.ifNull(['$desc', 'Not defined'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { desc: 'A', name: 'a' },
- { desc: 'Not defined', name: 'b' },
- { desc: 'Not defined', name: 'c' }
- ])
- })
- it('switch', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- name: 1,
- discount: $.switch({
- branches: [
- { case: $.gt(['$amount', 250]), then: 0.8 },
- { case: $.gt(['$amount', 150]), then: 0.9 }
- ],
- default: 1
- })
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { name: 'a', discount: 1 },
- { name: 'b', discount: 0.9 },
- { name: 'c', discount: 0.8 }
- ])
- })
- })
- describe('group操作符', () => {
- let studentsCollection = null,
- passagesCollection = null
- const $ = db.command.aggregate
- const studentsName = 'test-students'
- const studentsData = [
- { group: 'a', name: 'stu1', score: 84 },
- { group: 'a', name: 'stu2', score: 96 },
- { group: 'b', name: 'stu3', score: 80 },
- { group: 'b', name: 'stu4', score: 100 }
- ]
- const passagesName = 'test-passages'
- const passagesData = [
- { category: 'web', tags: ['JavaScript', 'CSS'], title: 'title1' },
- { category: 'System', tags: ['C++', 'C'], title: 'title2' }
- ]
- beforeAll(async () => {
- studentsCollection = await common.safeCollection(db, studentsName)
- passagesCollection = await common.safeCollection(db, passagesName)
- assert.strictEqual(await studentsCollection.create(studentsData), true)
- assert.strictEqual(await passagesCollection.create(passagesData), true)
- })
- afterAll(async () => {
- assert.strictEqual(await studentsCollection.remove(), true)
- assert.strictEqual(await passagesCollection.remove(), true)
- })
- it('push', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .group({
- _id: '$group',
- students: $.push({
- name: '$name',
- score: '$score'
- })
- })
- .end()
- const valid = result.data.every(item => 'students' in item)
- assert.strictEqual(valid, true)
- })
- it('max', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .group({
- _id: '$group',
- maxScore: $.max('$score')
- })
- .end()
- assert.strictEqual(result.data.length, 2)
- })
- it('min', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .group({
- _id: '$group',
- minScore: $.min('$score')
- })
- .end()
- assert.strictEqual(result.data.length, 2)
- })
- it('last', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .sort({
- score: 1
- })
- .group({
- _id: null,
- max: $.last('$score')
- })
- .end()
- assert(result.data[0].max, 100)
- })
- it('first', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .sort({
- score: 1
- })
- .group({
- _id: null,
- min: $.first('$score')
- })
- .end()
- assert(result.data[0].min, 80)
- })
- it('avg', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .group({
- _id: null,
- average: $.avg('$score')
- })
- .end()
- assert(result.data[0].average, 90)
- })
- describe('addToSet', async () => {
- it('非数组字段', async () => {
- const result = await db
- .collection(passagesName)
- .aggregate()
- .group({
- _id: null,
- categories: $.addToSet('$category')
- })
- .end()
- assert(result.data[0].categories.length, 2)
- })
- it('数组字段', async () => {
- const result = await db
- .collection(passagesName)
- .aggregate()
- .group({
- _id: null,
- tagsList: $.addToSet('$tags')
- })
- .end()
- const valid = result.data.some(item => Array.isArray(item.tagsList))
- assert(valid, true)
- })
- })
- })
- describe('字面量操作符', () => {
- let goodsCollection = null
- const $ = db.command.aggregate
- const collectionName = 'test-goods'
- const data = [{ price: '$1' }, { price: '$2.50' }, { price: '$3.60' }, { price: '$4.60' }]
- beforeAll(async () => {
- goodsCollection = await common.safeCollection(db, collectionName)
- const success = await goodsCollection.create(data)
- assert.strictEqual(success, true)
- })
- afterAll(async () => {
- const success = await goodsCollection.remove()
- assert.strictEqual(success, true)
- })
- it('以字面量的形式使用$', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- isOneDollar: $.eq(['$price', $.literal('$1')])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { isOneDollar: true },
- { isOneDollar: false },
- { isOneDollar: false },
- { isOneDollar: false }
- ])
- })
- it('投影一个字段,对应的值为1', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- price: 1,
- amount: $.literal(1)
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { amount: 1, price: '$1' },
- { amount: 1, price: '$2.50' },
- { amount: 1, price: '$3.60' },
- { amount: 1, price: '$4.60' }
- ])
- })
- })
- describe('字符串操作符', () => {
- let studentsCollection = null,
- personCollection = null
- const $ = db.command.aggregate
- const studentsName = 'test-students'
- const studentsData = [
- {
- birthday: '1999/12/12',
- date: new Date('1999/12/12'),
- firstName: 'Yuanxin',
- group: 'a',
- lastName: 'Dong',
- score: 84
- },
- {
- birthday: '1998/11/11',
- date: new Date('1998/11/11'),
- firstName: 'Weijia',
- group: 'a',
- lastName: 'Wang',
- score: 96
- },
- {
- birthday: '1997/10/10',
- date: new Date('1997/10/10'),
- firstName: 'Chengxi',
- group: 'b',
- lastName: 'Li',
- score: 80
- }
- ]
- const personName = 'test-person'
- const personData = [{ name: 'dongyuanxin', nickname: '心谭' }]
- beforeAll(async () => {
- studentsCollection = await common.safeCollection(db, studentsName)
- personCollection = await common.safeCollection(db, personName)
- assert.strictEqual(await studentsCollection.create(studentsData), true)
- assert.strictEqual(await personCollection.create(personData), true)
- })
- afterAll(async () => {
- assert.strictEqual(await studentsCollection.remove(), true)
- assert.strictEqual(await personCollection.remove(), true)
- })
- it('concat', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- fullName: $.concat(['$firstName', ' ', '$lastName'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { fullName: 'Yuanxin Dong' },
- { fullName: 'Weijia Wang' },
- { fullName: 'Chengxi Li' }
- ])
- })
- describe('dateToString', async () => {
- it('格式化日期', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- formatDate: $.dateToString({
- date: '$date',
- format: '%Y-%m-%d'
- })
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { formatDate: '1999-12-11' },
- { formatDate: '1998-11-10' },
- { formatDate: '1997-10-09' }
- ])
- })
- it('时区时间', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- formatDate: $.dateToString({
- date: '$date',
- format: '%H:%M:%S',
- timezone: 'Asia/Shanghai'
- })
- })
- .end()
- const valid = result.data.every(item => item.formatDate === '00:00:00')
- assert.ok(valid)
- })
- it('缺失情况的默认值', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- formatDate: $.dateToString({
- date: '$empty',
- onNull: 'null'
- })
- })
- .end()
- const valid = result.data.every(item => item.formatDate === 'null')
- assert.ok(valid)
- })
- })
- it('indexOfBytes', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- aStrIndex: $.indexOfBytes(['$firstName', 'a'])
- })
- .end()
- assert.deepStrictEqual(result.data, [{ aStrIndex: 2 }, { aStrIndex: 5 }, { aStrIndex: -1 }])
- })
- it('indexOfCP', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- aStrIndex: $.indexOfCP(['$firstName', 'a'])
- })
- .end()
- assert.deepStrictEqual(result.data, [{ aStrIndex: 2 }, { aStrIndex: 5 }, { aStrIndex: -1 }])
- })
- it('split', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- birthday: $.split(['$birthday', '/'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { birthday: ['1999', '12', '12'] },
- { birthday: ['1998', '11', '11'] },
- { birthday: ['1997', '10', '10'] }
- ])
- })
- it('strcasecmp', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- result: $.strcasecmp(['$firstName', '$lastName'])
- })
- .end()
- assert.deepStrictEqual(result.data, [{ result: 1 }, { result: 1 }, { result: -1 }])
- })
- it.skip('strLenBytes', async () => {
- const result = await db
- .collection(personName)
- .aggregate()
- .project({
- _id: 0,
- nameLength: $.strLenBytes('$name'),
- nicknameLength: $.strLenBytes('$nickname')
- })
- .end()
- assert.deepStrictEqual(result.data, [{ nameLength: 11, nicknameLength: 6 }])
- })
- it.skip('strLenCP', async () => {
- const result = await db
- .collection(personName)
- .aggregate()
- .project({
- _id: 0,
- nameLength: $.strLenCP('$name'),
- nicknameLength: $.strLenCP('$nickname')
- })
- .end()
- assert.deepStrictEqual(result.data, [{ nameLength: 11, nicknameLength: 2 }])
- })
- it('substr', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- year: $.substr(['$birthday', 0, 4]),
- month: $.substr(['$birthday', 5, 2]),
- day: $.substr(['$birthday', 8, -1])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { day: '12', month: '12', year: '1999' },
- { day: '11', month: '11', year: '1998' },
- { day: '10', month: '10', year: '1997' }
- ])
- })
- it('substrBytes', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- year: $.substrBytes(['$birthday', 0, 4]),
- month: $.substrBytes(['$birthday', 5, 2]),
- day: $.substrBytes(['$birthday', 8, -1])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { day: '12', month: '12', year: '1999' },
- { day: '11', month: '11', year: '1998' },
- { day: '10', month: '10', year: '1997' }
- ])
- })
- test.skip('substrCP', async () => {
- const result = await db
- .collection(personName)
- .aggregate()
- .project({
- _id: 0,
- firstCh: $.substrCP(['$nickname', 0, 1])
- })
- .end()
- assert.deepStrictEqual(result.data, [{ firstCh: '心' }])
- })
- test('toLower', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- result: $.toLower('$firstName')
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { result: 'yuanxin' },
- { result: 'weijia' },
- { result: 'chengxi' }
- ])
- })
- test('toUpper', async () => {
- const result = await db
- .collection(studentsName)
- .aggregate()
- .project({
- _id: 0,
- result: $.toUpper('$lastName')
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { result: 'DONG' },
- { result: 'WANG' },
- { result: 'LI' }
- ])
- })
- })
- describe('mergeObjects', () => {
- let goodsCollection = null
- const $ = db.command.aggregate
- const collectionName = 'test-goods'
- const data = [
- { name: 'A', foo: { a: 1 }, bar: { b: 2 } },
- { name: 'A', foo: { c: 1 }, bar: { d: 2 } },
- { name: 'A', foo: { e: 1 }, bar: { f: 2 } }
- ]
- beforeAll(async () => {
- goodsCollection = await common.safeCollection(db, collectionName)
- const success = await goodsCollection.create(data)
- assert.strictEqual(success, true)
- })
- afterAll(async () => {
- const success = await goodsCollection.remove()
- assert.strictEqual(success, true)
- })
- it('在group中使用', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .group({
- _id: '$name',
- mergedFoo: $.mergeObjects('$foo'),
- mergeBar: $.mergeObjects('$bar')
- })
- .end()
- assert.deepStrictEqual(result.data, [
- {
- _id: 'A',
- mergedFoo: { a: 1, c: 1, e: 1 },
- mergeBar: { b: 2, d: 2, f: 2 }
- }
- ])
- })
- it('在group以外使用', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- foobar: $.mergeObjects(['$foo', '$bar'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { foobar: { a: 1, b: 2 } },
- { foobar: { c: 1, d: 2 } },
- { foobar: { e: 1, f: 2 } }
- ])
- })
- })
- describe('集合操作', () => {
- let goodsCollection = null
- const $ = db.command.aggregate
- const collectionName = 'test-goods'
- const data = [
- { arr: [true] },
- { arr: [] },
- { arr: [true, false] },
- { arr: [1] },
- { arr: [1, 0] },
- { arr: ['stark'] }
- ]
- beforeAll(async () => {
- goodsCollection = await common.safeCollection(db, collectionName)
- const success = await goodsCollection.create(data)
- assert.strictEqual(success, true)
- })
- afterAll(async () => {
- const success = await goodsCollection.remove()
- assert.strictEqual(success, true)
- })
- it('allElementsTrue', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- allElementsTrue: $.allElementsTrue(['$arr'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { allElementsTrue: true },
- { allElementsTrue: true },
- { allElementsTrue: false },
- { allElementsTrue: true },
- { allElementsTrue: false },
- { allElementsTrue: true }
- ])
- })
- it('anyElementTrue', async () => {
- const $ = db.command.aggregate
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- anyElementTrue: $.anyElementTrue(['$arr'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { anyElementTrue: true },
- { anyElementTrue: false },
- { anyElementTrue: true },
- { anyElementTrue: true },
- { anyElementTrue: true },
- { anyElementTrue: true }
- ])
- })
- })
- describe('集合操作2', () => {
- let goodsCollection = null
- const $ = db.command.aggregate
- const collectionName = 'test-goods'
- const data = [
- { A: [1, 2], B: [1, 2] },
- { A: [1, 2], B: [2, 1, 2] },
- { A: [1, 2], B: [1, 2, 3] },
- { A: [1, 2], B: [3, 1] },
- { A: [1, 2], B: [] },
- { A: [1, 2], B: [{}, []] },
- { A: [], B: [] },
- { A: [], B: [1] }
- ]
- beforeAll(async () => {
- goodsCollection = await common.safeCollection(db, collectionName)
- const success = await goodsCollection.create(data)
- assert.strictEqual(success, true)
- })
- afterAll(async () => {
- const success = await goodsCollection.remove()
- assert.strictEqual(success, true)
- })
- it('setDifference', async () => {
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- isBOnly: $.setDifference(['$B', '$A'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { isBOnly: [] },
- { isBOnly: [] },
- { isBOnly: [3] },
- { isBOnly: [3] },
- { isBOnly: [] },
- { isBOnly: [{}, []] },
- { isBOnly: [] },
- { isBOnly: [1] }
- ])
- })
- it('setEquals', async () => {
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- sameElements: $.setEquals(['$A', '$B'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { sameElements: true },
- { sameElements: true },
- { sameElements: false },
- { sameElements: false },
- { sameElements: false },
- { sameElements: false },
- { sameElements: true },
- { sameElements: false }
- ])
- })
- it('setIntersection', async () => {
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- setIntersection: $.setIntersection(['$A', '$B'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { setIntersection: [1, 2] },
- { setIntersection: [1, 2] },
- { setIntersection: [1, 2] },
- { setIntersection: [1] },
- { setIntersection: [] },
- { setIntersection: [] },
- { setIntersection: [] },
- { setIntersection: [] }
- ])
- })
- it('setIsSubset', async () => {
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- setIsSubset: $.setIsSubset(['$A', '$B'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { setIsSubset: true },
- { setIsSubset: true },
- { setIsSubset: true },
- { setIsSubset: false },
- { setIsSubset: false },
- { setIsSubset: false },
- { setIsSubset: true },
- { setIsSubset: true }
- ])
- })
- it('setUnion', async () => {
- const result = await db
- .collection(collectionName)
- .aggregate()
- .project({
- _id: 0,
- setUnion: $.setUnion(['$A', '$B'])
- })
- .end()
- assert.deepStrictEqual(result.data, [
- { setUnion: [1, 2] },
- { setUnion: [1, 2] },
- { setUnion: [1, 2, 3] },
- { setUnion: [1, 2, 3] },
- { setUnion: [1, 2] },
- { setUnion: [1, 2, {}, []] },
- { setUnion: [] },
- { setUnion: [1] }
- ])
- })
- })
|