文字

說明

從預先定義的一組固定文件物件傳回文件。

這個階段通常用於單獨測試其他階段,但也可以做為聯結條件的輸入內容。

語法

Node.js

const results = await db.pipeline()
  .literals({ name: "joe", age: 10 }, { name: "bob", age: 30 }, { name: "alice", age: 40 })
  .where(field("age").lessThan(35))
  .execute();

...

[
  { name: "joe", age: 10 },
  { name: "bob", age: 30 }
]

行為

literals(...) 階段只能做為管道 (或子管道) 的第一個階段。從 literals 傳回的文件順序,與定義文件的順序相同。

雖然最常見的是字面值,但也可以傳入運算式,系統會評估並傳回運算式,因此不必先建立測試資料,就能測試不同的查詢 / 運算式行為。

舉例來說,以下說明如何快速測試一些常數測試集上的 length(...) 函式:

Node.js

const results = await db.pipeline()
  .literals({ x: constant("foo-bar-baz").length() }, { x: constant("bar").length() })
  .execute();

...

[
  { x: 11 },
  { x: 3 }
]