projection.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import * as assert from 'power-assert'
  2. import tcb from '../../../src/index'
  3. import * as Config from '../../config.local'
  4. import * as common from '../../common/index'
  5. const app = tcb.init(Config)
  6. const db = app.database()
  7. const _ = db.command
  8. describe('projection', () => {
  9. const collName = 'db-test-projection'
  10. let passagesCollection = null
  11. const data = [
  12. { category: 'Web', tags: ['JavaScript', 'C#'] },
  13. { category: 'Web', tags: ['Go', 'C#'] },
  14. { category: 'Life', tags: ['Go', 'Python', 'JavaScript'] }
  15. ]
  16. beforeAll(async () => {
  17. passagesCollection = await common.safeCollection(db, collName)
  18. const success = await passagesCollection.create(data)
  19. assert.strictEqual(success, true)
  20. })
  21. afterAll(async () => {
  22. const success = await passagesCollection.remove()
  23. assert.strictEqual(success, true)
  24. })
  25. it('slice', async () => {
  26. const result = await db
  27. .collection(collName)
  28. .field({
  29. tags: db.command.project.slice(1)
  30. })
  31. .get()
  32. for (let item of result.data) {
  33. assert(item.tags.length === 1)
  34. }
  35. })
  36. it('projection true false', async () => {
  37. const result = await db
  38. .collection(collName)
  39. .field({
  40. category: true
  41. })
  42. .get()
  43. // console.log('result:', result)
  44. for (let item of result.data) {
  45. assert(item.category)
  46. }
  47. // assert.strictEqual(result.data.length, 3)
  48. })
  49. it('projection 1 0', async () => {
  50. const result1 = await db
  51. .collection(collName)
  52. .field({
  53. category: 0
  54. })
  55. .get()
  56. // 检查回包中没有category
  57. assert(Object.keys(result1.data[0]).indexOf('category') < 0, true)
  58. })
  59. })