From 6efee6a18f398981547005e7033b4375166a4830 Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 6 Nov 2025 20:01:18 -0800 Subject: [PATCH 1/3] add admin snippets temporarily --- firestore-temp/package.json | 17 + firestore-temp/test.firestore.js | 1292 ++++++++++++++++++++++++++++++ 2 files changed, 1309 insertions(+) create mode 100644 firestore-temp/package.json create mode 100644 firestore-temp/test.firestore.js diff --git a/firestore-temp/package.json b/firestore-temp/package.json new file mode 100644 index 00000000..33968c49 --- /dev/null +++ b/firestore-temp/package.json @@ -0,0 +1,17 @@ +{ + "name": "firestore-temp", + "version": "1.0.0", + "scripts": { + "compile": "cp ../tsconfig.json.template ./tsconfig.json && tsc" + }, + "license": "Apache-2.0", + "devDependencies": { + "@types/chai": "^5.2.3", + "@types/mocha": "^10.0.10", + "chai": "^6.2.0", + "mocha": "^11.7.4" + }, + "dependencies": { + "@google-cloud/firestore": "^8.0.0-pipelines.1" + } +} diff --git a/firestore-temp/test.firestore.js b/firestore-temp/test.firestore.js new file mode 100644 index 00000000..3babe1d4 --- /dev/null +++ b/firestore-temp/test.firestore.js @@ -0,0 +1,1292 @@ +// [SNIPPET_REGISTRY disabled] +// [SNIPPETS_SEPARATION enabled] + +const { expect } = require('chai'); + +describe("firestore-pipelines", () => { + const { + Firestore + } = require("@google-cloud/firestore") + const { + Pipeline, + field, + constant, + countAll, + AggregateFunction, + and, + or, + xor, + conditional + } = require("@google-cloud/firestore/pipelines"); + + let app; + /** @type {Firestore} */ let db; + + before(() => { + db = new Firestore({ + projectId: 'your-project-id', + databaseId: 'your-new-enterprise-database' + }); + }); + + async function basicRead() { + // [START basic_read] + const readDataPipeline = db.pipeline() + .collection("users"); + + // Execute the pipeline and handle the result + try { + const querySnapshot = await readDataPipeline.execute(); + querySnapshot.results.forEach((result) => { + console.log(`${result.id} => ${result.data()}`); + }); + } catch (error) { + console.error("Error getting documents: ", error); + } + // [END basic_read] + } + + function pipelineConcepts() { + // [START pipeline_concepts] + const pipeline = db.pipeline() + // Step 1: Start a query with collection scope + .collection("cities") + // Step 2: Filter the collection + .where(field("population").greaterThan(100000)) + // Step 3: Sort the remaining documents + .sort(field("name").ascending()) + // Step 4: Return the top 10. Note applying the limit earlier in the + // pipeline would have unintentional results. + .limit(10); + // [END pipeline_concepts] + console.log(pipeline); + } + + function pipelineInitialization() { + // [START pipeline_initialization] + const { Firestore } = require("@google-cloud/firestore"); + const database = new Firestore({ + projectId: 'your-project-id', + databaseId: 'your-new-enterprise-database' + }); + const pipeline = database.pipeline(); + // [END pipeline_initialization] + console.log(pipeline); + } + + function fieldVsConstants() { + // [START field_or_constant] + const pipeline = db.pipeline() + .collection("cities") + .where(field("name").equal(constant("Toronto"))); + // [END field_or_constant] + console.log(pipeline); + } + + async function inputStages() { + // [START input_stages] + let results; + + // Return all restaurants in San Francisco + results = await db.pipeline().collection("cities/sf/restaurants").execute(); + + // Return all restaurants + results = await db.pipeline().collectionGroup("restaurants").execute(); + + // Return all documents across all collections in the database (the entire database) + results = await db.pipeline().database().execute(); + + // Batch read of 3 documents + results = await db.pipeline().documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "DC"), + doc(db, "cities", "NY") + ]).execute(); + // [END input_stages] + console.log(results); + } + + async function wherePipeline() { + // [START pipeline_where] + let results; + + results = await db.pipeline().collection("books") + .where(field("rating").equal(5)) + .where(field("published").lessThan(1900)) + .execute(); + + results = await db.pipeline().collection("books") + .where(and(field("rating").equal(5), field("published").lessThan(1900))) + .execute(); + // [END pipeline_where] + console.log(results); + } + + async function aggregateGroups() { + // [START aggregate_groups] + const results = await db.pipeline() + .collection("books") + .aggregate( + field("rating").average().as("avg_rating") + ) + .distinct(field("genre")) + .execute(); + // [END aggregate_groups] + console.log(results); + } + + async function aggregateDistinct() { + // [START aggregate_distinct] + const results = await execute(db.pipeline() + .collection("books") + .distinct( + field("author").toUpper().as("author"), + field("genre") + ) + ); + // [END aggregate_distinct] + console.log(results); + } + + async function sort() { + // [START sort] + const results = await execute(db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ) + ); + // [END sort] + console.log(results); + } + + function sortComparison() { + // [START sort_comparison] + const q = query(collection(db, "cities"), + orderBy("state"), + orderBy("population", "desc")); + + const pipeline = db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ); + // [END sort_comparison] + console.log(q); + console.log(pipeline); + } + + async function functions() { + // [START functions_example] + let results; + + // Type 1: Scalar (for use in non-aggregation stages) + // Example: Return the min store price for each book. + results = await execute(db.pipeline().collection("books") + .select(field("current").logicalMinimum(field("updated")).as("price_min")) + ); + + // Type 2: Aggregation (for use in aggregate stages) + // Example: Return the min price of all books. + results = await execute(db.pipeline().collection("books") + .aggregate(field("price").minimum().as("min_price")) + ); + // [END functions_example] + console.log(results); + } + + async function creatingIndexes() { + // [START query_example] + const results = await execute(db.pipeline() + .collection("books") + .where(field("published").lessThan(1900)) + .where(field("genre").equal("Science Fiction")) + .where(field("rating").greaterThan(4.3)) + .sort(field("published").descending()) + ); + // [END query_example] + console.log(results); + } + + async function sparseIndexes() { + // [START sparse_index_example] + const results = await execute(db.pipeline() + .collection("books") + .where(field("category").like("%fantasy%")) + ); + // [END sparse_index_example] + console.log(results); + } + + async function sparseIndexes2() { + // [START sparse_index_example_2] + const results = await execute(db.pipeline() + .collection("books") + .sort(field("release_date").ascending()) + ); + // [END sparse_index_example_2] + console.log(results); + } + + async function coveredQuery() { + // [START covered_query] + const results = await execute(db.pipeline() + .collection("books") + .where(field("category").like("%fantasy%")) + .where(field("title").exists()) + .where(field("author").exists()) + .select(field("title"), field("author")) + ); + // [END covered_query] + console.log(results); + } + + async function pagination() { + // [START pagination_not_supported_preview] + // Existing pagination via `startAt()` + const q = + query(collection(db, "cities"), orderBy("population"), startAt(1000000)); + + // Private preview workaround using pipelines + const pageSize = 2; + const pipeline = db.pipeline() + .collection("cities") + .select("name", "population", "__name__") + .sort(field("population").descending(), field("__name__").ascending()); + + // Page 1 results + let snapshot = await execute(pipeline.limit(pageSize)); + + // End of page marker + const lastDoc = snapshot.results[snapshot.results.length - 1]; + + // Page 2 results + snapshot = await execute( + pipeline + .where( + or( + and( + field("population").equal(lastDoc.get("population")), + field("__name__").greaterThan(lastDoc.ref) + ), + field("population").lessThan(lastDoc.get("population")) + ) + ) + .limit(pageSize) + ); + // [END pagination_not_supported_preview] + console.log(q); + console.log(pipeline); + } + + async function collectionStage() { + // [START collection_example] + const results = await execute(db.pipeline() + .collection("users/bob/games") + .sort(field("name").ascending()) + ); + // [END collection_example] + console.log(results); + } + + async function collectionGroupStage() { + // [START collection_group_example] + const results = await execute(db.pipeline() + .collectionGroup("games") + .sort(field("name").ascending()) + ); + // [END collection_group_example] + console.log(results); + } + + async function databaseStage() { + // [START database_example] + // Count all documents in the database + const results = await execute(db.pipeline() + .database() + .aggregate(countAll().as("total")) + ); + // [END database_example] + console.log(results); + } + + async function documentsStage() { + // [START documents_example] + const results = await execute(db.pipeline() + .documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "DC"), + doc(db, "cities", "NY") + ]) + ); + // [END documents_example] + console.log(results); + } + + async function replaceWithStage() { + // [START initial_data] + await setDoc(doc(collection(db, "cities"), "SF"), { + "name": "San Francisco", + "population": 800000, + "location": { + "country": "USA", + "state": "California" + } + }); + await setDoc(doc(collection(db, "cities"), "TO"), { + "name": "Toronto", + "population": 3000000, + "province": "ON", + "location": { + "country": "Canada", + "province": "Ontario" + } + }); + await setDoc(doc(collection(db, "cities"), "NY"), { + "name": "New York", + "location": { + "country": "USA", + "state": "New York" + } + }); + await setDoc(doc(collection(db, "cities"), "AT"), { + "name": "Atlantis", + }); + // [END initial_data] + + // [START full_replace] + const names = await execute(db.pipeline() + .collection("cities") + .replaceWith(field("location")) + ); + // [END full_replace] + + // [START map_merge_overwrite] + // unsupported in client SDKs for now + // [END map_merge_overwrite] + console.log(names); + } + + async function sampleStage() { + // [START sample_example] + let results; + + // Get a sample of 100 documents in a database + results = await execute(db.pipeline() + .database() + .sample(100) + ); + + // Randomly shuffle a list of 3 documents + results = await execute(db.pipeline() + .documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "NY"), + doc(db, "cities", "DC"), + ]) + .sample(3) + ); + // [END sample_example] + console.log(results); + } + + async function samplePercent() { + // [START sample_percent] + // Get a sample of on average 50% of the documents in the database + const results = await execute(db.pipeline() + .database() + .sample({ percentage: 0.5 }) + ); + // [END sample_percent] + console.log(results); + } + + async function unionStage() { + // [START union_stage] + const results = await execute(db.pipeline() + .collection("cities/SF/restaurants") + .where(field("type").equal("Chinese")) + .union(db.pipeline() + .collection("cities/NY/restaurants") + .where(field("type").equal("Italian"))) + .where(field("rating").greaterThanOrEqual(4.5)) + .sort(field("__name__").descending()) + ); + // [END union_stage] + console.log(results); + } + + async function unnestStage() { + // [START unnest_stage] + const results = await execute(db.pipeline() + .database() + .unnest(field("arrayField").as("unnestedArrayField"), "index") + ); + // [END unnest_stage] + console.log(results); + } + + async function unnestStageEmptyOrNonArray() { + // [START unnest_edge_cases] + // Input + // { identifier : 1, neighbors: [ "Alice", "Cathy" ] } + // { identifier : 2, neighbors: [] } + // { identifier : 3, neighbors: "Bob" } + + const results = await execute(db.pipeline() + .database() + .unnest(field("neighbors").as("unnestedNeighbors"), "index" ) + ); + + // Output + // { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Alice", index: 0 } + // { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Cathy", index: 1 } + // { identifier: 3, neighbors: "Bob", index: null} + // [END unnest_edge_cases] + console.log(results); + } + + async function countFunction() { + // [START count_function] + // Total number of books in the collection + const countOfAll = await execute(db.pipeline() + .collection("books") + .aggregate(countAll().as("count")) + ); + + // Number of books with nonnull `ratings` field + const countField = await execute(db.pipeline() + .collection("books") + .aggregate(field("ratings").count().as("count")) + ); + // [END count_function] + console.log(countOfAll); + console.log(countField); + } + + async function countIfFunction() { + // [START count_if] + const result = await execute(db.pipeline() + .collection("books") + .aggregate( + field("rating").greaterThan(4).countIf().as("filteredCount") + ) + ); + // [END count_if] + console.log(result); + } + + async function countDistinctFunction() { + // [START count_distinct] + const result = await execute(db.pipeline() + .collection("books") + .aggregate(field("author").countDistinct().as("unique_authors")) + ); + // [END count_distinct] + console.log(result); + } + + async function sumFunction() { + // [START sum_function] + const result = await execute(db.pipeline() + .collection("cities") + .aggregate(field("population").sum().as("totalPopulation")) + ); + // [END sum_function] + console.log(result); + } + + async function avgFunction() { + // [START avg_function] + const result = await execute(db.pipeline() + .collection("cities") + .aggregate(field("population").average().as("averagePopulation")) + ); + // [END avg_function] + console.log(result); + } + + async function minFunction() { + // [START min_function] + const result = await execute(db.pipeline() + .collection("books") + .aggregate(field("price").minimum().as("minimumPrice")) + ); + // [END min_function] + console.log(result); + } + + async function maxFunction() { + // [START max_function] + const result = await execute(db.pipeline() + .collection("books") + .aggregate(field("price").maximum().as("maximumPrice")) + ); + // [END max_function] + console.log(result); + } + + async function addFunction() { + // [START add_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) + ); + // [END add_function] + console.log(result); + } + + async function subtractFunction() { + // [START subtract_function] + const storeCredit = 7; + const result = await execute(db.pipeline() + .collection("books") + .select(field("price").subtract(constant(storeCredit)).as("totalCost")) + ); + // [END subtract_function] + console.log(result); + } + + async function multiplyFunction() { + // [START multiply_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("price").multiply(field("soldBooks")).as("revenue")) + ); + // [END multiply_function] + console.log(result); + } + + async function divideFunction() { + // [START divide_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) + ); + // [END divide_function] + console.log(result); + } + + async function modFunction() { + // [START mod_function] + const displayCapacity = 1000; + const result = await execute(db.pipeline() + .collection("books") + .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) + ); + // [END mod_function] + console.log(result); + } + + async function ceilFunction() { + // [START ceil_function] + const booksPerShelf = 100; + const result = await execute(db.pipeline() + .collection("books") + .select( + field("unsoldBooks").divide(constant(booksPerShelf)).ceil().as("requiredShelves") + ) + ); + // [END ceil_function] + console.log(result); + } + + async function floorFunction() { + // [START floor_function] + const result = await execute(db.pipeline() + .collection("books") + .addFields( + field("wordCount").divide(field("pages")).floor().as("wordsPerPage") + ) + ); + // [END floor_function] + console.log(result); + } + + async function roundFunction() { + // [START round_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) + .aggregate(field("partialRevenue").sum().as("totalRevenue")) + ); + // [END round_function] + console.log(result); + } + + async function powFunction() { + // [START pow_function] + const googleplex = { latitude: 37.4221, longitude: 122.0853 }; + const result = await execute(db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + ); + // [END pow_function] + console.log(result); + } + + async function sqrtFunction() { + // [START sqrt_function] + const googleplex = { latitude: 37.4221, longitude: 122.0853 }; + const result = await execute(db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + ); + // [END sqrt_function] + console.log(result); + } + + async function expFunction() { + // [START exp_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").exp().as("expRating")) + ); + // [END exp_function] + console.log(result); + } + + async function lnFunction() { + // [START ln_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").ln().as("lnRating")) + ); + // [END ln_function] + console.log(result); + } + + async function logFunction() { + // [START log_function] + // Not supported on JS + // [END log_function] + } + + async function arrayConcat() { + // [START array_concat] + const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayConcat([field("subGenre")]).as("allGenres")) + ); + // [END array_concat] + console.log(result); + } + + async function arrayContains() { + // [START array_contains] + const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) + ); + // [END array_contains] + console.log(result); + } + + async function arrayContainsAll() { + // [START array_contains_all] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAll([constant("fantasy"), constant("adventure")]) + .as("isFantasyAdventure") + ) + ); + // [END array_contains_all] + console.log(result); + } + + async function arrayContainsAny() { + // [START array_contains_any] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) + .as("isMysteryOrFantasy") + ) + ); + // [END array_contains_any] + console.log(result); + } + + async function arrayLength() { + // [START array_length] + const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayLength().as("genreCount")) + ); + // [END array_length] + console.log(result); + } + + async function arrayReverse() { + // [START array_reverse] + const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayReverse().as("reversedGenres")) + ); + // [END array_reverse] + console.log(result); + } + + async function equalFunction() { + // [START equal_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").equal(5).as("hasPerfectRating")) + ); + // [END equal_function] + console.log(result); + } + + async function greaterThanFunction() { + // [START greater_than] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").greaterThan(4).as("hasHighRating")) + ); + // [END greater_than] + console.log(result); + } + + async function greaterThanOrEqualToFunction() { + // [START greater_or_equal] + const result = await execute(db.pipeline() + .collection("books") + .select(field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")) + ); + // [END greater_or_equal] + console.log(result); + } + + async function lessThanFunction() { + // [START less_than] + const result = await execute(db.pipeline() + .collection("books") + .select(field("published").lessThan(1923).as("isPublicDomainProbably")) + ); + // [END less_than] + console.log(result); + } + + async function lessThanOrEqualToFunction() { + // [START less_or_equal] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").lessThanOrEqual(2).as("hasBadRating")) + ); + // [END less_or_equal] + console.log(result); + } + + async function notEqualFunction() { + // [START not_equal] + const result = await execute(db.pipeline() + .collection("books") + .select(field("title").notEqual("1984").as("not1984")) + ); + // [END not_equal] + console.log(result); + } + + async function existsFunction() { + // [START exists_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").exists().as("hasRating")) + ); + // [END exists_function] + console.log(result); + } + + async function andFunction() { + // [START and_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + and(field("rating").greaterThan(4), field("price").lessThan(10)) + .as("under10Recommendation") + ) + ); + // [END and_function] + console.log(result); + } + + async function orFunction() { + // [START or_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) + .as("matchesSearchFilters") + ) + ); + // [END or_function] + console.log(result); + } + + async function xorFunction() { + // [START xor_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) + .as("matchesSearchFilters") + ) + ); + // [END xor_function] + console.log(result); + } + + async function notFunction() { + // [START not_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("tags").arrayContains("nonfiction").not() + .as("isFiction") + ) + ); + // [END not_function] + console.log(result); + } + + async function condFunction() { + // [START cond_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("tags").arrayConcat([ + field("pages").greaterThan(100) + .conditional(constant("longRead"), constant("shortRead")) + ]).as("extendedTags") + ) + ); + // [END cond_function] + console.log(result); + } + + async function equalAnyFunction() { + // [START eq_any] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) + .as("matchesGenreFilters") + ) + ); + // [END eq_any] + console.log(result); + } + + async function notEqualAnyFunction() { + // [START not_eq_any] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) + .as("byExcludedAuthors") + ) + ); + // [END not_eq_any] + console.log(result); + } + + async function maxLogicalFunction() { + // [START max_logical_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("rating").logicalMaximum(1).as("flooredRating") + ) + ); + // [END max_logical_function] + console.log(result); + } + + async function minLogicalFunction() { + // [START min_logical_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("rating").logicalMinimum(5).as("cappedRating") + ) + ); + // [END min_logical_function] + console.log(result); + } + + async function mapGetFunction() { + // [START map_get] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("awards").mapGet("pulitzer").as("hasPulitzerAward") + ) + ); + // [END map_get] + console.log(result); + } + + async function byteLengthFunction() { + // [START byte_length] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").byteLength().as("titleByteLength") + ) + ); + // [END byte_length] + console.log(result); + } + + async function charLengthFunction() { + // [START char_length] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").charLength().as("titleCharLength") + ) + ); + // [END char_length] + console.log(result); + } + + async function startsWithFunction() { + // [START starts_with] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").startsWith("The") + .as("needsSpecialAlphabeticalSort") + ) + ); + // [END starts_with] + console.log(result); + } + + async function endsWithFunction() { + // [START ends_with] + const result = await execute(db.pipeline() + .collection("inventory/devices/laptops") + .select( + field("name").endsWith("16 inch") + .as("16InLaptops") + ) + ); + // [END ends_with] + console.log(result); + } + + async function likeFunction() { + // [START like] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre").like("%Fiction") + .as("anyFiction") + ) + ); + // [END like] + console.log(result); + } + + async function regexContainsFunction() { + // [START regex_contains] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("title").regexContains("Firestore (Enterprise|Standard)") + .as("isFirestoreRelated") + ) + ); + // [END regex_contains] + console.log(result); + } + + async function regexMatchFunction() { + // [START regex_match] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("title").regexMatch("Firestore (Enterprise|Standard)") + .as("isFirestoreExactly") + ) + ); + // [END regex_match] + console.log(result); + } + + async function strConcatFunction() { + // [START str_concat] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").stringConcat(" by ", field("author")) + .as("fullyQualifiedTitle") + ) + ); + // [END str_concat] + console.log(result); + } + + async function strContainsFunction() { + // [START string_contains] + const result = await execute(db.pipeline() + .collection("articles") + .select( + field("body").stringContains("Firestore") + .as("isFirestoreRelated") + ) + ); + // [END string_contains] + console.log(result); + } + + async function toUpperFunction() { + // [START to_upper] + const result = await execute(db.pipeline() + .collection("authors") + .select( + field("name").toUpper() + .as("uppercaseName") + ) + ); + // [END to_upper] + console.log(result); + } + + async function toLowerFunction() { + // [START to_lower] + const result = await execute(db.pipeline() + .collection("authors") + .select( + field("genre").toLower().equal("fantasy") + .as("isFantasy") + ) + ); + // [END to_lower] + } + + async function substrFunction() { + // [START substr_function] + const result = await execute(db.pipeline() + .collection("books") + .where(field("title").startsWith("The ")) + .select( + field("title").substring(4) + .as("titleWithoutLeadingThe") + ) + ); + // [END substr_function] + console.log(result); + } + + async function strReverseFunction() { + // [START str_reverse] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("name").reverse().as("reversedName") + ) + ); + // [END str_reverse] + console.log(result); + } + + async function strTrimFunction() { + // [START trim_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("name").trim().as("whitespaceTrimmedName") + ) + ); + // [END trim_function] + console.log(result); + } + + async function strReplaceFunction() { + // not yet supported until GA + } + + async function strSplitFunction() { + // not yet supported until GA + } + + async function unixMicrosToTimestampFunction() { + // [START unix_micros_timestamp] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") + ) + ); + // [END unix_micros_timestamp] + console.log(result); + } + + async function unixMillisToTimestampFunction() { + // [START unix_millis_timestamp] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") + ) + ); + // [END unix_millis_timestamp] + console.log(result); + } + + async function unixSecondsToTimestampFunction() { + // [START unix_seconds_timestamp] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") + ) + ); + // [END unix_seconds_timestamp] + console.log(result); + } + + async function timestampAddFunction() { + // [START timestamp_add] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAt").timestampAdd("day", 3653).as("expiresAt") + ) + ); + // [END timestamp_add] + console.log(result); + } + + async function timestampSubFunction() { + // [START timestamp_sub] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") + ) + ); + // [END timestamp_sub] + console.log(result); + } + + async function timestampToUnixMicrosFunction() { + // [START timestamp_unix_micros] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMicros().as("unixMicros") + ) + ); + // [END timestamp_unix_micros] + console.log(result); + } + + async function timestampToUnixMillisFunction() { + // [START timestamp_unix_millis] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMillis().as("unixMillis") + ) + ); + // [END timestamp_unix_millis] + console.log(result); + } + + async function timestampToUnixSecondsFunction() { + // [START timestamp_unix_seconds] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixSeconds().as("unixSeconds") + ) + ); + // [END timestamp_unix_seconds] + console.log(result); + } + + async function cosineDistanceFunction() { + // [START cosine_distance] + const sampleVector = [0.0, 1, 2, 3, 4, 5]; + const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").cosineDistance(sampleVector).as("cosineDistance") + ) + ); + // [END cosine_distance] + console.log(result); + } + + async function dotProductFunction() { + // [START dot_product] + const sampleVector = [0.0, 1, 2, 3, 4, 5]; + const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").dotProduct(sampleVector).as("dotProduct") + ) + ); + // [END dot_product] + console.log(result); + } + + async function euclideanDistanceFunction() { + // [START euclidean_distance] + const sampleVector = [0.0, 1, 2, 3, 4, 5]; + const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") + ) + ); + // [END euclidean_distance] + console.log(result); + } + + async function vectorLengthFunction() { + // [START vector_length] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").vectorLength().as("vectorLength") + ) + ); + // [END vector_length] + console.log(result); + } +}); From a1d68bf0ed90cd11829b10c5aad89b902764471f Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 6 Nov 2025 20:24:37 -0800 Subject: [PATCH 2/3] write node snippets --- firestore-temp/test.firestore.js | 490 ++++++++++++++++--------------- 1 file changed, 247 insertions(+), 243 deletions(-) diff --git a/firestore-temp/test.firestore.js b/firestore-temp/test.firestore.js index 3babe1d4..bd27da8f 100644 --- a/firestore-temp/test.firestore.js +++ b/firestore-temp/test.firestore.js @@ -9,11 +9,13 @@ describe("firestore-pipelines", () => { } = require("@google-cloud/firestore") const { Pipeline, + arrayReverse, field, constant, countAll, AggregateFunction, and, + like, or, xor, conditional @@ -98,9 +100,9 @@ describe("firestore-pipelines", () => { // Batch read of 3 documents results = await db.pipeline().documents([ - doc(db, "cities", "SF"), - doc(db, "cities", "DC"), - doc(db, "cities", "NY") + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY"), ]).execute(); // [END input_stages] console.log(results); @@ -137,34 +139,34 @@ describe("firestore-pipelines", () => { async function aggregateDistinct() { // [START aggregate_distinct] - const results = await execute(db.pipeline() + const results = await db.pipeline() .collection("books") .distinct( field("author").toUpper().as("author"), field("genre") ) - ); + .execute(); // [END aggregate_distinct] console.log(results); } async function sort() { // [START sort] - const results = await execute(db.pipeline() + const results = await db.pipeline() .collection("books") .sort( field("release_date").descending(), field("author").ascending() ) - ); + .execute(); // [END sort] console.log(results); } function sortComparison() { // [START sort_comparison] - const q = query(collection(db, "cities"), - orderBy("state"), - orderBy("population", "desc")); + const q = db.collection("cities") + .orderBy("state") + .orderBy("population", "desc"); const pipeline = db.pipeline() .collection("books") @@ -182,61 +184,61 @@ describe("firestore-pipelines", () => { // Type 1: Scalar (for use in non-aggregation stages) // Example: Return the min store price for each book. - results = await execute(db.pipeline().collection("books") + results = await db.pipeline().collection("books") .select(field("current").logicalMinimum(field("updated")).as("price_min")) - ); + .execute(); // Type 2: Aggregation (for use in aggregate stages) // Example: Return the min price of all books. - results = await execute(db.pipeline().collection("books") + results = await db.pipeline().collection("books") .aggregate(field("price").minimum().as("min_price")) - ); + .execute(); // [END functions_example] console.log(results); } async function creatingIndexes() { // [START query_example] - const results = await execute(db.pipeline() + const results = await db.pipeline() .collection("books") .where(field("published").lessThan(1900)) .where(field("genre").equal("Science Fiction")) .where(field("rating").greaterThan(4.3)) .sort(field("published").descending()) - ); + .execute(); // [END query_example] console.log(results); } async function sparseIndexes() { // [START sparse_index_example] - const results = await execute(db.pipeline() + const results = await db.pipeline() .collection("books") - .where(field("category").like("%fantasy%")) - ); + .where(like(field("category"), "%fantasy%")) + .execute(); // [END sparse_index_example] console.log(results); } async function sparseIndexes2() { // [START sparse_index_example_2] - const results = await execute(db.pipeline() + const results = await db.pipeline() .collection("books") .sort(field("release_date").ascending()) - ); + .execute(); // [END sparse_index_example_2] console.log(results); } async function coveredQuery() { // [START covered_query] - const results = await execute(db.pipeline() + const results = await db.pipeline() .collection("books") - .where(field("category").like("%fantasy%")) + .where(like(field("category"), "%fantasy%")) .where(field("title").exists()) .where(field("author").exists()) .select(field("title"), field("author")) - ); + .execute(); // [END covered_query] console.log(results); } @@ -245,7 +247,7 @@ describe("firestore-pipelines", () => { // [START pagination_not_supported_preview] // Existing pagination via `startAt()` const q = - query(collection(db, "cities"), orderBy("population"), startAt(1000000)); + db.collection("cities").orderBy("population").startAt(1000000); // Private preview workaround using pipelines const pageSize = 2; @@ -255,14 +257,13 @@ describe("firestore-pipelines", () => { .sort(field("population").descending(), field("__name__").ascending()); // Page 1 results - let snapshot = await execute(pipeline.limit(pageSize)); + let snapshot = await pipeline.limit(pageSize).execute(); // End of page marker const lastDoc = snapshot.results[snapshot.results.length - 1]; // Page 2 results - snapshot = await execute( - pipeline + snapshot = await pipeline .where( or( and( @@ -273,7 +274,7 @@ describe("firestore-pipelines", () => { ) ) .limit(pageSize) - ); + .execute(); // [END pagination_not_supported_preview] console.log(q); console.log(pipeline); @@ -281,20 +282,20 @@ describe("firestore-pipelines", () => { async function collectionStage() { // [START collection_example] - const results = await execute(db.pipeline() + const results = await db.pipeline() .collection("users/bob/games") .sort(field("name").ascending()) - ); + .execute(); // [END collection_example] console.log(results); } async function collectionGroupStage() { // [START collection_group_example] - const results = await execute(db.pipeline() + const results = await db.pipeline() .collectionGroup("games") .sort(field("name").ascending()) - ); + .execute(); // [END collection_group_example] console.log(results); } @@ -302,30 +303,30 @@ describe("firestore-pipelines", () => { async function databaseStage() { // [START database_example] // Count all documents in the database - const results = await execute(db.pipeline() + const results = await db.pipeline() .database() .aggregate(countAll().as("total")) - ); + .execute(); // [END database_example] console.log(results); } async function documentsStage() { // [START documents_example] - const results = await execute(db.pipeline() + const results = await db.pipeline() .documents([ - doc(db, "cities", "SF"), - doc(db, "cities", "DC"), - doc(db, "cities", "NY") + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY") ]) - ); + .execute(); // [END documents_example] console.log(results); } async function replaceWithStage() { // [START initial_data] - await setDoc(doc(collection(db, "cities"), "SF"), { + await db.collection("cities").doc("SF").set({ "name": "San Francisco", "population": 800000, "location": { @@ -333,7 +334,7 @@ describe("firestore-pipelines", () => { "state": "California" } }); - await setDoc(doc(collection(db, "cities"), "TO"), { + await db.collection("cities").doc("TO").set({ "name": "Toronto", "population": 3000000, "province": "ON", @@ -342,23 +343,23 @@ describe("firestore-pipelines", () => { "province": "Ontario" } }); - await setDoc(doc(collection(db, "cities"), "NY"), { + await db.collection("cities").doc("NY").set({ "name": "New York", "location": { "country": "USA", "state": "New York" } }); - await setDoc(doc(collection(db, "cities"), "AT"), { + await db.collection("cities").doc("AT").set({ "name": "Atlantis", }); // [END initial_data] // [START full_replace] - const names = await execute(db.pipeline() + const names = await db.pipeline() .collection("cities") .replaceWith(field("location")) - ); + .execute(); // [END full_replace] // [START map_merge_overwrite] @@ -372,20 +373,20 @@ describe("firestore-pipelines", () => { let results; // Get a sample of 100 documents in a database - results = await execute(db.pipeline() + results = await db.pipeline() .database() .sample(100) - ); + .execute(); // Randomly shuffle a list of 3 documents - results = await execute(db.pipeline() + results = await db.pipeline() .documents([ - doc(db, "cities", "SF"), - doc(db, "cities", "NY"), - doc(db, "cities", "DC"), + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY") ]) .sample(3) - ); + .execute(); // [END sample_example] console.log(results); } @@ -393,17 +394,17 @@ describe("firestore-pipelines", () => { async function samplePercent() { // [START sample_percent] // Get a sample of on average 50% of the documents in the database - const results = await execute(db.pipeline() + const results = await db.pipeline() .database() .sample({ percentage: 0.5 }) - ); + .execute(); // [END sample_percent] console.log(results); } async function unionStage() { // [START union_stage] - const results = await execute(db.pipeline() + const results = await db.pipeline() .collection("cities/SF/restaurants") .where(field("type").equal("Chinese")) .union(db.pipeline() @@ -411,17 +412,17 @@ describe("firestore-pipelines", () => { .where(field("type").equal("Italian"))) .where(field("rating").greaterThanOrEqual(4.5)) .sort(field("__name__").descending()) - ); + .execute(); // [END union_stage] console.log(results); } async function unnestStage() { // [START unnest_stage] - const results = await execute(db.pipeline() + const results = await db.pipeline() .database() .unnest(field("arrayField").as("unnestedArrayField"), "index") - ); + .execute(); // [END unnest_stage] console.log(results); } @@ -433,10 +434,10 @@ describe("firestore-pipelines", () => { // { identifier : 2, neighbors: [] } // { identifier : 3, neighbors: "Bob" } - const results = await execute(db.pipeline() + const results = await db.pipeline() .database() .unnest(field("neighbors").as("unnestedNeighbors"), "index" ) - ); + .execute(); // Output // { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Alice", index: 0 } @@ -449,16 +450,16 @@ describe("firestore-pipelines", () => { async function countFunction() { // [START count_function] // Total number of books in the collection - const countOfAll = await execute(db.pipeline() + const countOfAll = await db.pipeline() .collection("books") .aggregate(countAll().as("count")) - ); + .execute(); // Number of books with nonnull `ratings` field - const countField = await execute(db.pipeline() + const countField = await db.pipeline() .collection("books") .aggregate(field("ratings").count().as("count")) - ); + .execute(); // [END count_function] console.log(countOfAll); console.log(countField); @@ -466,72 +467,72 @@ describe("firestore-pipelines", () => { async function countIfFunction() { // [START count_if] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .aggregate( field("rating").greaterThan(4).countIf().as("filteredCount") ) - ); + .execute(); // [END count_if] console.log(result); } async function countDistinctFunction() { // [START count_distinct] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .aggregate(field("author").countDistinct().as("unique_authors")) - ); + .execute(); // [END count_distinct] console.log(result); } async function sumFunction() { // [START sum_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("cities") .aggregate(field("population").sum().as("totalPopulation")) - ); + .execute(); // [END sum_function] console.log(result); } async function avgFunction() { // [START avg_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("cities") .aggregate(field("population").average().as("averagePopulation")) - ); + .execute(); // [END avg_function] console.log(result); } async function minFunction() { // [START min_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .aggregate(field("price").minimum().as("minimumPrice")) - ); + .execute(); // [END min_function] console.log(result); } async function maxFunction() { // [START max_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .aggregate(field("price").maximum().as("maximumPrice")) - ); + .execute(); // [END max_function] console.log(result); } async function addFunction() { // [START add_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) - ); + .execute(); // [END add_function] console.log(result); } @@ -539,30 +540,30 @@ describe("firestore-pipelines", () => { async function subtractFunction() { // [START subtract_function] const storeCredit = 7; - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("price").subtract(constant(storeCredit)).as("totalCost")) - ); + .execute(); // [END subtract_function] console.log(result); } async function multiplyFunction() { // [START multiply_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("price").multiply(field("soldBooks")).as("revenue")) - ); + .execute(); // [END multiply_function] console.log(result); } async function divideFunction() { // [START divide_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) - ); + .execute(); // [END divide_function] console.log(result); } @@ -570,10 +571,10 @@ describe("firestore-pipelines", () => { async function modFunction() { // [START mod_function] const displayCapacity = 1000; - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) - ); + .execute(); // [END mod_function] console.log(result); } @@ -581,105 +582,105 @@ describe("firestore-pipelines", () => { async function ceilFunction() { // [START ceil_function] const booksPerShelf = 100; - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("unsoldBooks").divide(constant(booksPerShelf)).ceil().as("requiredShelves") ) - ); + .execute(); // [END ceil_function] console.log(result); } async function floorFunction() { // [START floor_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .addFields( field("wordCount").divide(field("pages")).floor().as("wordsPerPage") ) - ); + .execute(); // [END floor_function] console.log(result); } async function roundFunction() { // [START round_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) .aggregate(field("partialRevenue").sum().as("totalRevenue")) - ); + .execute(); // [END round_function] console.log(result); } async function powFunction() { - // [START pow_function] - const googleplex = { latitude: 37.4221, longitude: 122.0853 }; - const result = await execute(db.pipeline() - .collection("cities") - .addFields( - field("lat").subtract(constant(googleplex.latitude)) - .multiply(111 /* km per degree */) - .pow(2) - .as("latitudeDifference"), - field("lng").subtract(constant(googleplex.longitude)) - .multiply(111 /* km per degree */) - .pow(2) - .as("longitudeDifference") - ) - .select( - field("latitudeDifference").add(field("longitudeDifference")).sqrt() - // Inaccurate for large distances or close to poles - .as("approximateDistanceToGoogle") - ) - ); - // [END pow_function] - console.log(result); + // [START pow_function] + const googleplex = { latitude: 37.4221, longitude: 122.0853 }; + const result = await db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + .execute(); + // [END pow_function] + console.log(result); } async function sqrtFunction() { - // [START sqrt_function] - const googleplex = { latitude: 37.4221, longitude: 122.0853 }; - const result = await execute(db.pipeline() - .collection("cities") - .addFields( - field("lat").subtract(constant(googleplex.latitude)) - .multiply(111 /* km per degree */) - .pow(2) - .as("latitudeDifference"), - field("lng").subtract(constant(googleplex.longitude)) - .multiply(111 /* km per degree */) - .pow(2) - .as("longitudeDifference") - ) - .select( - field("latitudeDifference").add(field("longitudeDifference")).sqrt() - // Inaccurate for large distances or close to poles - .as("approximateDistanceToGoogle") - ) - ); - // [END sqrt_function] - console.log(result); + // [START sqrt_function] + const googleplex = { latitude: 37.4221, longitude: 122.0853 }; + const result = await db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + .execute(); + // [END sqrt_function] + console.log(result); } async function expFunction() { // [START exp_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("rating").exp().as("expRating")) - ); + .execute(); // [END exp_function] console.log(result); } async function lnFunction() { // [START ln_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("rating").ln().as("lnRating")) - ); + .execute(); // [END ln_function] console.log(result); } @@ -692,445 +693,448 @@ describe("firestore-pipelines", () => { async function arrayConcat() { // [START array_concat] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("genre").arrayConcat([field("subGenre")]).as("allGenres")) - ); + .execute(); // [END array_concat] console.log(result); } async function arrayContains() { // [START array_contains] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) - ); + .execute(); // [END array_contains] console.log(result); } async function arrayContainsAll() { // [START array_contains_all] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("genre") .arrayContainsAll([constant("fantasy"), constant("adventure")]) .as("isFantasyAdventure") ) - ); + .execute(); // [END array_contains_all] console.log(result); } async function arrayContainsAny() { // [START array_contains_any] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("genre") .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) .as("isMysteryOrFantasy") ) - ); + .execute(); // [END array_contains_any] console.log(result); } async function arrayLength() { // [START array_length] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("genre").arrayLength().as("genreCount")) - ); + .execute(); // [END array_length] console.log(result); } - async function arrayReverse() { + async function arrayReverseSnippet() { // [START array_reverse] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") - .select(field("genre").arrayReverse().as("reversedGenres")) - ); + .select(arrayReverse(field("genre")).as("reversedGenres")) + .execute(); // [END array_reverse] console.log(result); } async function equalFunction() { // [START equal_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("rating").equal(5).as("hasPerfectRating")) - ); + .execute(); // [END equal_function] console.log(result); } async function greaterThanFunction() { // [START greater_than] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("rating").greaterThan(4).as("hasHighRating")) - ); + .execute(); // [END greater_than] console.log(result); } async function greaterThanOrEqualToFunction() { // [START greater_or_equal] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")) - ); + .execute(); // [END greater_or_equal] console.log(result); } async function lessThanFunction() { // [START less_than] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("published").lessThan(1923).as("isPublicDomainProbably")) - ); + .execute(); // [END less_than] console.log(result); } async function lessThanOrEqualToFunction() { // [START less_or_equal] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("rating").lessThanOrEqual(2).as("hasBadRating")) - ); + .execute(); // [END less_or_equal] console.log(result); } async function notEqualFunction() { // [START not_equal] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("title").notEqual("1984").as("not1984")) - ); + .execute(); // [END not_equal] console.log(result); } async function existsFunction() { // [START exists_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select(field("rating").exists().as("hasRating")) - ); + .execute(); // [END exists_function] console.log(result); } async function andFunction() { // [START and_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( and(field("rating").greaterThan(4), field("price").lessThan(10)) .as("under10Recommendation") ) - ); + .execute(); // [END and_function] console.log(result); } async function orFunction() { // [START or_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) .as("matchesSearchFilters") ) - ); + .execute(); // [END or_function] console.log(result); } async function xorFunction() { // [START xor_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) .as("matchesSearchFilters") ) - ); + .execute(); // [END xor_function] console.log(result); } async function notFunction() { // [START not_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("tags").arrayContains("nonfiction").not() .as("isFiction") ) - ); + .execute(); // [END not_function] console.log(result); } async function condFunction() { // [START cond_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("tags").arrayConcat([ - field("pages").greaterThan(100) - .conditional(constant("longRead"), constant("shortRead")) + conditional( + field("pages").greaterThan(100), + constant("longRead"), + constant("shortRead") + ) ]).as("extendedTags") ) - ); + .execute(); // [END cond_function] console.log(result); } async function equalAnyFunction() { // [START eq_any] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) .as("matchesGenreFilters") ) - ); + .execute(); // [END eq_any] console.log(result); } async function notEqualAnyFunction() { // [START not_eq_any] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) .as("byExcludedAuthors") ) - ); + .execute(); // [END not_eq_any] console.log(result); } async function maxLogicalFunction() { // [START max_logical_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("rating").logicalMaximum(1).as("flooredRating") ) - ); + .execute(); // [END max_logical_function] console.log(result); } async function minLogicalFunction() { // [START min_logical_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("rating").logicalMinimum(5).as("cappedRating") ) - ); + .execute(); // [END min_logical_function] console.log(result); } async function mapGetFunction() { // [START map_get] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("awards").mapGet("pulitzer").as("hasPulitzerAward") ) - ); + .execute(); // [END map_get] console.log(result); } async function byteLengthFunction() { // [START byte_length] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("title").byteLength().as("titleByteLength") ) - ); + .execute(); // [END byte_length] console.log(result); } async function charLengthFunction() { // [START char_length] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("title").charLength().as("titleCharLength") ) - ); + .execute(); // [END char_length] console.log(result); } async function startsWithFunction() { // [START starts_with] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("title").startsWith("The") .as("needsSpecialAlphabeticalSort") ) - ); + .execute(); // [END starts_with] console.log(result); } async function endsWithFunction() { // [START ends_with] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("inventory/devices/laptops") .select( field("name").endsWith("16 inch") .as("16InLaptops") ) - ); + .execute(); // [END ends_with] console.log(result); } async function likeFunction() { // [START like] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("genre").like("%Fiction") .as("anyFiction") ) - ); + .execute(); // [END like] console.log(result); } async function regexContainsFunction() { // [START regex_contains] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("documents") .select( field("title").regexContains("Firestore (Enterprise|Standard)") .as("isFirestoreRelated") ) - ); + .execute(); // [END regex_contains] console.log(result); } async function regexMatchFunction() { // [START regex_match] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("documents") .select( field("title").regexMatch("Firestore (Enterprise|Standard)") .as("isFirestoreExactly") ) - ); + .execute(); // [END regex_match] console.log(result); } async function strConcatFunction() { // [START str_concat] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("title").stringConcat(" by ", field("author")) .as("fullyQualifiedTitle") ) - ); + .execute(); // [END str_concat] console.log(result); } async function strContainsFunction() { // [START string_contains] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("articles") .select( field("body").stringContains("Firestore") .as("isFirestoreRelated") ) - ); + .execute(); // [END string_contains] console.log(result); } async function toUpperFunction() { // [START to_upper] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("authors") .select( field("name").toUpper() .as("uppercaseName") ) - ); + .execute(); // [END to_upper] console.log(result); } async function toLowerFunction() { // [START to_lower] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("authors") .select( field("genre").toLower().equal("fantasy") .as("isFantasy") ) - ); + .execute(); // [END to_lower] } async function substrFunction() { // [START substr_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .where(field("title").startsWith("The ")) .select( field("title").substring(4) .as("titleWithoutLeadingThe") ) - ); + .execute(); // [END substr_function] console.log(result); } async function strReverseFunction() { // [START str_reverse] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("name").reverse().as("reversedName") ) - ); + .execute(); // [END str_reverse] console.log(result); } async function strTrimFunction() { // [START trim_function] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("name").trim().as("whitespaceTrimmedName") ) - ); + .execute(); // [END trim_function] console.log(result); } @@ -1145,96 +1149,96 @@ describe("firestore-pipelines", () => { async function unixMicrosToTimestampFunction() { // [START unix_micros_timestamp] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("documents") .select( field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") ) - ); + .execute(); // [END unix_micros_timestamp] console.log(result); } async function unixMillisToTimestampFunction() { // [START unix_millis_timestamp] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("documents") .select( field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") ) - ); + .execute(); // [END unix_millis_timestamp] console.log(result); } async function unixSecondsToTimestampFunction() { // [START unix_seconds_timestamp] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("documents") .select( field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") ) - ); + .execute(); // [END unix_seconds_timestamp] console.log(result); } async function timestampAddFunction() { // [START timestamp_add] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("documents") .select( field("createdAt").timestampAdd("day", 3653).as("expiresAt") ) - ); + .execute(); // [END timestamp_add] console.log(result); } async function timestampSubFunction() { // [START timestamp_sub] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("documents") .select( field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") ) - ); + .execute(); // [END timestamp_sub] console.log(result); } async function timestampToUnixMicrosFunction() { // [START timestamp_unix_micros] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMicros().as("unixMicros") ) - ); + .execute(); // [END timestamp_unix_micros] console.log(result); } async function timestampToUnixMillisFunction() { // [START timestamp_unix_millis] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixMillis().as("unixMillis") ) - ); + .execute(); // [END timestamp_unix_millis] console.log(result); } async function timestampToUnixSecondsFunction() { // [START timestamp_unix_seconds] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("documents") .select( field("dateString").timestampToUnixSeconds().as("unixSeconds") ) - ); + .execute(); // [END timestamp_unix_seconds] console.log(result); } @@ -1242,12 +1246,12 @@ describe("firestore-pipelines", () => { async function cosineDistanceFunction() { // [START cosine_distance] const sampleVector = [0.0, 1, 2, 3, 4, 5]; - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("embedding").cosineDistance(sampleVector).as("cosineDistance") ) - ); + .execute(); // [END cosine_distance] console.log(result); } @@ -1255,12 +1259,12 @@ describe("firestore-pipelines", () => { async function dotProductFunction() { // [START dot_product] const sampleVector = [0.0, 1, 2, 3, 4, 5]; - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("embedding").dotProduct(sampleVector).as("dotProduct") ) - ); + .execute(); // [END dot_product] console.log(result); } @@ -1268,24 +1272,24 @@ describe("firestore-pipelines", () => { async function euclideanDistanceFunction() { // [START euclidean_distance] const sampleVector = [0.0, 1, 2, 3, 4, 5]; - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") ) - ); + .execute(); // [END euclidean_distance] console.log(result); } async function vectorLengthFunction() { // [START vector_length] - const result = await execute(db.pipeline() + const result = await db.pipeline() .collection("books") .select( field("embedding").vectorLength().as("vectorLength") ) - ); + .execute(); // [END vector_length] console.log(result); } From 89635c35968d89a5548dfa103930ffff80670851 Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 6 Nov 2025 20:27:22 -0800 Subject: [PATCH 3/3] run snippets --- .../test-firestore/and_function.js | 2 +- .../test-firestore/functions_example.js | 4 +- .../test-firestore/max_logical_function.js | 2 +- .../test-firestore/min_logical_function.js | 2 +- .../test-firestore/not_function.js | 2 +- .../test-firestore/or_function.js | 2 +- .../pagination_not_supported_preview.js | 28 ++++++++++++-- .../test-firestore/pipeline_where.js | 2 +- .../test-firestore/xor_function.js | 2 +- .../test-firestore/add_function.js | 12 ++++++ .../test-firestore/aggregate_distinct.js | 15 ++++++++ .../test-firestore/aggregate_groups.js | 15 ++++++++ .../test-firestore/and_function.js | 15 ++++++++ .../test-firestore/array_concat.js | 12 ++++++ .../test-firestore/array_contains.js | 12 ++++++ .../test-firestore/array_contains_all.js | 16 ++++++++ .../test-firestore/array_contains_any.js | 16 ++++++++ .../test-firestore/array_length.js | 12 ++++++ .../test-firestore/array_reverse.js | 12 ++++++ .../test-firestore/avg_function.js | 12 ++++++ .../test-firestore/basic_read.js | 20 ++++++++++ .../test-firestore/byte_length.js | 14 +++++++ .../test-firestore/ceil_function.js | 15 ++++++++ .../test-firestore/char_length.js | 14 +++++++ .../test-firestore/collection_example.js | 12 ++++++ .../collection_group_example.js | 12 ++++++ .../test-firestore/cond_function.js | 20 ++++++++++ .../test-firestore/cosine_distance.js | 15 ++++++++ .../test-firestore/count_distinct.js | 12 ++++++ .../test-firestore/count_function.js | 19 ++++++++++ .../firestore-temp/test-firestore/count_if.js | 14 +++++++ .../test-firestore/covered_query.js | 15 ++++++++ .../test-firestore/database_example.js | 13 +++++++ .../test-firestore/divide_function.js | 12 ++++++ .../test-firestore/documents_example.js | 15 ++++++++ .../test-firestore/dot_product.js | 15 ++++++++ .../test-firestore/ends_with.js | 15 ++++++++ .../firestore-temp/test-firestore/eq_any.js | 15 ++++++++ .../test-firestore/equal_function.js | 12 ++++++ .../test-firestore/euclidean_distance.js | 15 ++++++++ .../test-firestore/exists_function.js | 12 ++++++ .../test-firestore/exp_function.js | 12 ++++++ .../test-firestore/field_or_constant.js | 11 ++++++ .../test-firestore/floor_function.js | 14 +++++++ .../test-firestore/full_replace.js | 12 ++++++ .../test-firestore/functions_example.js | 21 ++++++++++ .../test-firestore/greater_or_equal.js | 12 ++++++ .../test-firestore/greater_than.js | 12 ++++++ .../test-firestore/initial_data.js | 35 +++++++++++++++++ .../test-firestore/input_stages.js | 25 ++++++++++++ .../test-firestore/less_or_equal.js | 12 ++++++ .../test-firestore/less_than.js | 12 ++++++ .../firestore-temp/test-firestore/like.js | 15 ++++++++ .../test-firestore/ln_function.js | 12 ++++++ .../test-firestore/log_function.js | 9 +++++ .../firestore-temp/test-firestore/map_get.js | 14 +++++++ .../test-firestore/map_merge_overwrite.js | 9 +++++ .../test-firestore/max_function.js | 12 ++++++ .../test-firestore/max_logical_function.js | 14 +++++++ .../test-firestore/min_function.js | 12 ++++++ .../test-firestore/min_logical_function.js | 14 +++++++ .../test-firestore/mod_function.js | 13 +++++++ .../test-firestore/multiply_function.js | 12 ++++++ .../test-firestore/not_eq_any.js | 15 ++++++++ .../test-firestore/not_equal.js | 12 ++++++ .../test-firestore/not_function.js | 15 ++++++++ .../test-firestore/or_function.js | 15 ++++++++ .../pagination_not_supported_preview.js | 38 +++++++++++++++++++ .../test-firestore/pipeline_concepts.js | 18 +++++++++ .../test-firestore/pipeline_initialization.js | 14 +++++++ .../test-firestore/pipeline_where.js | 18 +++++++++ .../test-firestore/pow_function.js | 27 +++++++++++++ .../test-firestore/query_example.js | 15 ++++++++ .../test-firestore/regex_contains.js | 15 ++++++++ .../test-firestore/regex_match.js | 15 ++++++++ .../test-firestore/round_function.js | 13 +++++++ .../test-firestore/sample_example.js | 25 ++++++++++++ .../test-firestore/sample_percent.js | 13 +++++++ .../firestore-temp/test-firestore/sort.js | 14 +++++++ .../test-firestore/sort_comparison.js | 17 +++++++++ .../test-firestore/sparse_index_example.js | 12 ++++++ .../test-firestore/sqrt_function.js | 27 +++++++++++++ .../test-firestore/starts_with.js | 15 ++++++++ .../test-firestore/str_concat.js | 15 ++++++++ .../test-firestore/str_reverse.js | 14 +++++++ .../test-firestore/string_contains.js | 15 ++++++++ .../test-firestore/substr_function.js | 16 ++++++++ .../test-firestore/subtract_function.js | 13 +++++++ .../test-firestore/sum_function.js | 12 ++++++ .../test-firestore/timestamp_add.js | 14 +++++++ .../test-firestore/timestamp_sub.js | 14 +++++++ .../test-firestore/timestamp_unix_micros.js | 14 +++++++ .../test-firestore/timestamp_unix_millis.js | 14 +++++++ .../test-firestore/timestamp_unix_seconds.js | 14 +++++++ .../firestore-temp/test-firestore/to_lower.js | 15 ++++++++ .../firestore-temp/test-firestore/to_upper.js | 15 ++++++++ .../test-firestore/trim_function.js | 14 +++++++ .../test-firestore/union_stage.js | 17 +++++++++ .../test-firestore/unix_micros_timestamp.js | 14 +++++++ .../test-firestore/unix_millis_timestamp.js | 14 +++++++ .../test-firestore/unix_seconds_timestamp.js | 14 +++++++ .../test-firestore/unnest_edge_cases.js | 21 ++++++++++ .../test-firestore/unnest_stage.js | 12 ++++++ .../test-firestore/vector_length.js | 14 +++++++ .../test-firestore/xor_function.js | 15 ++++++++ 105 files changed, 1475 insertions(+), 13 deletions(-) create mode 100644 snippets/firestore-temp/test-firestore/add_function.js create mode 100644 snippets/firestore-temp/test-firestore/aggregate_distinct.js create mode 100644 snippets/firestore-temp/test-firestore/aggregate_groups.js create mode 100644 snippets/firestore-temp/test-firestore/and_function.js create mode 100644 snippets/firestore-temp/test-firestore/array_concat.js create mode 100644 snippets/firestore-temp/test-firestore/array_contains.js create mode 100644 snippets/firestore-temp/test-firestore/array_contains_all.js create mode 100644 snippets/firestore-temp/test-firestore/array_contains_any.js create mode 100644 snippets/firestore-temp/test-firestore/array_length.js create mode 100644 snippets/firestore-temp/test-firestore/array_reverse.js create mode 100644 snippets/firestore-temp/test-firestore/avg_function.js create mode 100644 snippets/firestore-temp/test-firestore/basic_read.js create mode 100644 snippets/firestore-temp/test-firestore/byte_length.js create mode 100644 snippets/firestore-temp/test-firestore/ceil_function.js create mode 100644 snippets/firestore-temp/test-firestore/char_length.js create mode 100644 snippets/firestore-temp/test-firestore/collection_example.js create mode 100644 snippets/firestore-temp/test-firestore/collection_group_example.js create mode 100644 snippets/firestore-temp/test-firestore/cond_function.js create mode 100644 snippets/firestore-temp/test-firestore/cosine_distance.js create mode 100644 snippets/firestore-temp/test-firestore/count_distinct.js create mode 100644 snippets/firestore-temp/test-firestore/count_function.js create mode 100644 snippets/firestore-temp/test-firestore/count_if.js create mode 100644 snippets/firestore-temp/test-firestore/covered_query.js create mode 100644 snippets/firestore-temp/test-firestore/database_example.js create mode 100644 snippets/firestore-temp/test-firestore/divide_function.js create mode 100644 snippets/firestore-temp/test-firestore/documents_example.js create mode 100644 snippets/firestore-temp/test-firestore/dot_product.js create mode 100644 snippets/firestore-temp/test-firestore/ends_with.js create mode 100644 snippets/firestore-temp/test-firestore/eq_any.js create mode 100644 snippets/firestore-temp/test-firestore/equal_function.js create mode 100644 snippets/firestore-temp/test-firestore/euclidean_distance.js create mode 100644 snippets/firestore-temp/test-firestore/exists_function.js create mode 100644 snippets/firestore-temp/test-firestore/exp_function.js create mode 100644 snippets/firestore-temp/test-firestore/field_or_constant.js create mode 100644 snippets/firestore-temp/test-firestore/floor_function.js create mode 100644 snippets/firestore-temp/test-firestore/full_replace.js create mode 100644 snippets/firestore-temp/test-firestore/functions_example.js create mode 100644 snippets/firestore-temp/test-firestore/greater_or_equal.js create mode 100644 snippets/firestore-temp/test-firestore/greater_than.js create mode 100644 snippets/firestore-temp/test-firestore/initial_data.js create mode 100644 snippets/firestore-temp/test-firestore/input_stages.js create mode 100644 snippets/firestore-temp/test-firestore/less_or_equal.js create mode 100644 snippets/firestore-temp/test-firestore/less_than.js create mode 100644 snippets/firestore-temp/test-firestore/like.js create mode 100644 snippets/firestore-temp/test-firestore/ln_function.js create mode 100644 snippets/firestore-temp/test-firestore/log_function.js create mode 100644 snippets/firestore-temp/test-firestore/map_get.js create mode 100644 snippets/firestore-temp/test-firestore/map_merge_overwrite.js create mode 100644 snippets/firestore-temp/test-firestore/max_function.js create mode 100644 snippets/firestore-temp/test-firestore/max_logical_function.js create mode 100644 snippets/firestore-temp/test-firestore/min_function.js create mode 100644 snippets/firestore-temp/test-firestore/min_logical_function.js create mode 100644 snippets/firestore-temp/test-firestore/mod_function.js create mode 100644 snippets/firestore-temp/test-firestore/multiply_function.js create mode 100644 snippets/firestore-temp/test-firestore/not_eq_any.js create mode 100644 snippets/firestore-temp/test-firestore/not_equal.js create mode 100644 snippets/firestore-temp/test-firestore/not_function.js create mode 100644 snippets/firestore-temp/test-firestore/or_function.js create mode 100644 snippets/firestore-temp/test-firestore/pagination_not_supported_preview.js create mode 100644 snippets/firestore-temp/test-firestore/pipeline_concepts.js create mode 100644 snippets/firestore-temp/test-firestore/pipeline_initialization.js create mode 100644 snippets/firestore-temp/test-firestore/pipeline_where.js create mode 100644 snippets/firestore-temp/test-firestore/pow_function.js create mode 100644 snippets/firestore-temp/test-firestore/query_example.js create mode 100644 snippets/firestore-temp/test-firestore/regex_contains.js create mode 100644 snippets/firestore-temp/test-firestore/regex_match.js create mode 100644 snippets/firestore-temp/test-firestore/round_function.js create mode 100644 snippets/firestore-temp/test-firestore/sample_example.js create mode 100644 snippets/firestore-temp/test-firestore/sample_percent.js create mode 100644 snippets/firestore-temp/test-firestore/sort.js create mode 100644 snippets/firestore-temp/test-firestore/sort_comparison.js create mode 100644 snippets/firestore-temp/test-firestore/sparse_index_example.js create mode 100644 snippets/firestore-temp/test-firestore/sqrt_function.js create mode 100644 snippets/firestore-temp/test-firestore/starts_with.js create mode 100644 snippets/firestore-temp/test-firestore/str_concat.js create mode 100644 snippets/firestore-temp/test-firestore/str_reverse.js create mode 100644 snippets/firestore-temp/test-firestore/string_contains.js create mode 100644 snippets/firestore-temp/test-firestore/substr_function.js create mode 100644 snippets/firestore-temp/test-firestore/subtract_function.js create mode 100644 snippets/firestore-temp/test-firestore/sum_function.js create mode 100644 snippets/firestore-temp/test-firestore/timestamp_add.js create mode 100644 snippets/firestore-temp/test-firestore/timestamp_sub.js create mode 100644 snippets/firestore-temp/test-firestore/timestamp_unix_micros.js create mode 100644 snippets/firestore-temp/test-firestore/timestamp_unix_millis.js create mode 100644 snippets/firestore-temp/test-firestore/timestamp_unix_seconds.js create mode 100644 snippets/firestore-temp/test-firestore/to_lower.js create mode 100644 snippets/firestore-temp/test-firestore/to_upper.js create mode 100644 snippets/firestore-temp/test-firestore/trim_function.js create mode 100644 snippets/firestore-temp/test-firestore/union_stage.js create mode 100644 snippets/firestore-temp/test-firestore/unix_micros_timestamp.js create mode 100644 snippets/firestore-temp/test-firestore/unix_millis_timestamp.js create mode 100644 snippets/firestore-temp/test-firestore/unix_seconds_timestamp.js create mode 100644 snippets/firestore-temp/test-firestore/unnest_edge_cases.js create mode 100644 snippets/firestore-temp/test-firestore/unnest_stage.js create mode 100644 snippets/firestore-temp/test-firestore/vector_length.js create mode 100644 snippets/firestore-temp/test-firestore/xor_function.js diff --git a/snippets/firestore-next/test-firestore/and_function.js b/snippets/firestore-next/test-firestore/and_function.js index 27e40a44..f84865be 100644 --- a/snippets/firestore-next/test-firestore/and_function.js +++ b/snippets/firestore-next/test-firestore/and_function.js @@ -8,7 +8,7 @@ const result = await execute(db.pipeline() .collection("books") .select( - and(field("rating").greaterThan(4), (field("price").lessThan(10))) + and(field("rating").greaterThan(4), field("price").lessThan(10)) .as("under10Recommendation") ) ); diff --git a/snippets/firestore-next/test-firestore/functions_example.js b/snippets/firestore-next/test-firestore/functions_example.js index cdc86768..b1a80aeb 100644 --- a/snippets/firestore-next/test-firestore/functions_example.js +++ b/snippets/firestore-next/test-firestore/functions_example.js @@ -10,9 +10,7 @@ let results; // Type 1: Scalar (for use in non-aggregation stages) // Example: Return the min store price for each book. results = await execute(db.pipeline().collection("books") - .select( - field("current").logicalMinimum(["updated"]).as("price_min") - ) + .select(field("current").logicalMinimum(field("updated")).as("price_min")) ); // Type 2: Aggregation (for use in aggregate stages) diff --git a/snippets/firestore-next/test-firestore/max_logical_function.js b/snippets/firestore-next/test-firestore/max_logical_function.js index 196e0ebd..e884226f 100644 --- a/snippets/firestore-next/test-firestore/max_logical_function.js +++ b/snippets/firestore-next/test-firestore/max_logical_function.js @@ -8,7 +8,7 @@ const result = await execute(db.pipeline() .collection("books") .select( - field("rating").logicalMaximum([1]).as("flooredRating") + field("rating").logicalMaximum(1).as("flooredRating") ) ); // [END max_logical_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/min_logical_function.js b/snippets/firestore-next/test-firestore/min_logical_function.js index aa2ea820..91ae75a1 100644 --- a/snippets/firestore-next/test-firestore/min_logical_function.js +++ b/snippets/firestore-next/test-firestore/min_logical_function.js @@ -8,7 +8,7 @@ const result = await execute(db.pipeline() .collection("books") .select( - field("rating").logicalMinimum([5]).as("cappedRating") + field("rating").logicalMinimum(5).as("cappedRating") ) ); // [END min_logical_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/not_function.js b/snippets/firestore-next/test-firestore/not_function.js index 9324b74e..f6905f23 100644 --- a/snippets/firestore-next/test-firestore/not_function.js +++ b/snippets/firestore-next/test-firestore/not_function.js @@ -8,7 +8,7 @@ const result = await execute(db.pipeline() .collection("books") .select( - (field("tags").arrayContains("nonfiction").not()) + field("tags").arrayContains("nonfiction").not() .as("isFiction") ) ); diff --git a/snippets/firestore-next/test-firestore/or_function.js b/snippets/firestore-next/test-firestore/or_function.js index 4199bdce..035512d7 100644 --- a/snippets/firestore-next/test-firestore/or_function.js +++ b/snippets/firestore-next/test-firestore/or_function.js @@ -8,7 +8,7 @@ const result = await execute(db.pipeline() .collection("books") .select( - or(field("genre").equal("Fantasy"), (field("tags").arrayContains("adventure"))) + or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) .as("matchesSearchFilters") ) ); diff --git a/snippets/firestore-next/test-firestore/pagination_not_supported_preview.js b/snippets/firestore-next/test-firestore/pagination_not_supported_preview.js index 4a78a54d..02687926 100644 --- a/snippets/firestore-next/test-firestore/pagination_not_supported_preview.js +++ b/snippets/firestore-next/test-firestore/pagination_not_supported_preview.js @@ -6,12 +6,34 @@ // [START pagination_not_supported_preview_modular] // Existing pagination via `startAt()` -const q = // db.collection("cities").orderBy("population").startAt(1000000); +const q = query(collection(db, "cities"), orderBy("population"), startAt(1000000)); // Private preview workaround using pipelines +const pageSize = 2; const pipeline = db.pipeline() .collection("cities") - .where(field("population").greaterThanOrEqual(1000000)) - .sort(field("population").descending()); + .select("name", "population", "__name__") + .sort(field("population").descending(), field("__name__").ascending()); + +// Page 1 results +let snapshot = await execute(pipeline.limit(pageSize)); + +// End of page marker +const lastDoc = snapshot.results[snapshot.results.length - 1]; + +// Page 2 results +snapshot = await execute( + pipeline + .where( + or( + and( + field("population").equal(lastDoc.get("population")), + field("__name__").greaterThan(lastDoc.ref) + ), + field("population").lessThan(lastDoc.get("population")) + ) + ) + .limit(pageSize) +); // [END pagination_not_supported_preview_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/pipeline_where.js b/snippets/firestore-next/test-firestore/pipeline_where.js index e4ca5182..7e7bd55e 100644 --- a/snippets/firestore-next/test-firestore/pipeline_where.js +++ b/snippets/firestore-next/test-firestore/pipeline_where.js @@ -13,6 +13,6 @@ results = await execute(db.pipeline().collection("books") ); results = await execute(db.pipeline().collection("books") - .where(and(field("rating").equal(5), (field("published").lessThan(1900)))) + .where(and(field("rating").equal(5), field("published").lessThan(1900))) ); // [END pipeline_where_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/xor_function.js b/snippets/firestore-next/test-firestore/xor_function.js index d8812705..f4523929 100644 --- a/snippets/firestore-next/test-firestore/xor_function.js +++ b/snippets/firestore-next/test-firestore/xor_function.js @@ -8,7 +8,7 @@ const result = await execute(db.pipeline() .collection("books") .select( - xor(field("tags").arrayContains("magic"), (field("tags").arrayContains("nonfiction"))) + xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) .as("matchesSearchFilters") ) ); diff --git a/snippets/firestore-temp/test-firestore/add_function.js b/snippets/firestore-temp/test-firestore/add_function.js new file mode 100644 index 00000000..ff354a12 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/add_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START add_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) + .execute(); +// [END add_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/aggregate_distinct.js b/snippets/firestore-temp/test-firestore/aggregate_distinct.js new file mode 100644 index 00000000..ee5464d6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/aggregate_distinct.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_distinct_modular] +const results = await db.pipeline() + .collection("books") + .distinct( + field("author").toUpper().as("author"), + field("genre") + ) + .execute(); +// [END aggregate_distinct_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/aggregate_groups.js b/snippets/firestore-temp/test-firestore/aggregate_groups.js new file mode 100644 index 00000000..b45c26e6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/aggregate_groups.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_groups_modular] +const results = await db.pipeline() + .collection("books") + .aggregate( + field("rating").average().as("avg_rating") + ) + .distinct(field("genre")) + .execute(); +// [END aggregate_groups_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/and_function.js b/snippets/firestore-temp/test-firestore/and_function.js new file mode 100644 index 00000000..817c21ea --- /dev/null +++ b/snippets/firestore-temp/test-firestore/and_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START and_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + and(field("rating").greaterThan(4), field("price").lessThan(10)) + .as("under10Recommendation") + ) + .execute(); +// [END and_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_concat.js b/snippets/firestore-temp/test-firestore/array_concat.js new file mode 100644 index 00000000..ac9d4482 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_concat.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_concat_modular] +const result = await db.pipeline() + .collection("books") + .select(field("genre").arrayConcat([field("subGenre")]).as("allGenres")) + .execute(); +// [END array_concat_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_contains.js b/snippets/firestore-temp/test-firestore/array_contains.js new file mode 100644 index 00000000..6b63e01a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_contains.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_contains_modular] +const result = await db.pipeline() + .collection("books") + .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) + .execute(); +// [END array_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_contains_all.js b/snippets/firestore-temp/test-firestore/array_contains_all.js new file mode 100644 index 00000000..d79e2250 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_contains_all.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_contains_all_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAll([constant("fantasy"), constant("adventure")]) + .as("isFantasyAdventure") + ) + .execute(); +// [END array_contains_all_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_contains_any.js b/snippets/firestore-temp/test-firestore/array_contains_any.js new file mode 100644 index 00000000..5ccbc3d5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_contains_any.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_contains_any_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) + .as("isMysteryOrFantasy") + ) + .execute(); +// [END array_contains_any_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_length.js b/snippets/firestore-temp/test-firestore/array_length.js new file mode 100644 index 00000000..2627557d --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_length.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_length_modular] +const result = await db.pipeline() + .collection("books") + .select(field("genre").arrayLength().as("genreCount")) + .execute(); +// [END array_length_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_reverse.js b/snippets/firestore-temp/test-firestore/array_reverse.js new file mode 100644 index 00000000..21b8ee7c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_reverse.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_reverse_modular] +const result = await db.pipeline() + .collection("books") + .select(arrayReverse(field("genre")).as("reversedGenres")) + .execute(); +// [END array_reverse_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/avg_function.js b/snippets/firestore-temp/test-firestore/avg_function.js new file mode 100644 index 00000000..dc7b1a8e --- /dev/null +++ b/snippets/firestore-temp/test-firestore/avg_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START avg_function_modular] +const result = await db.pipeline() + .collection("cities") + .aggregate(field("population").average().as("averagePopulation")) + .execute(); +// [END avg_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/basic_read.js b/snippets/firestore-temp/test-firestore/basic_read.js new file mode 100644 index 00000000..9589129b --- /dev/null +++ b/snippets/firestore-temp/test-firestore/basic_read.js @@ -0,0 +1,20 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START basic_read_modular] +const readDataPipeline = db.pipeline() + .collection("users"); + +// Execute the pipeline and handle the result +try { + const querySnapshot = await readDataPipeline.execute(); + querySnapshot.results.forEach((result) => { + console.log(`${result.id} => ${result.data()}`); + }); +} catch (error) { + console.error("Error getting documents: ", error); +} +// [END basic_read_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/byte_length.js b/snippets/firestore-temp/test-firestore/byte_length.js new file mode 100644 index 00000000..1de8e3dc --- /dev/null +++ b/snippets/firestore-temp/test-firestore/byte_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START byte_length_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("title").byteLength().as("titleByteLength") + ) + .execute(); +// [END byte_length_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/ceil_function.js b/snippets/firestore-temp/test-firestore/ceil_function.js new file mode 100644 index 00000000..5adb33d7 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/ceil_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START ceil_function_modular] +const booksPerShelf = 100; +const result = await db.pipeline() + .collection("books") + .select( + field("unsoldBooks").divide(constant(booksPerShelf)).ceil().as("requiredShelves") + ) + .execute(); +// [END ceil_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/char_length.js b/snippets/firestore-temp/test-firestore/char_length.js new file mode 100644 index 00000000..4483d7bc --- /dev/null +++ b/snippets/firestore-temp/test-firestore/char_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START char_length_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("title").charLength().as("titleCharLength") + ) + .execute(); +// [END char_length_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/collection_example.js b/snippets/firestore-temp/test-firestore/collection_example.js new file mode 100644 index 00000000..05d3be56 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/collection_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_example_modular] +const results = await db.pipeline() + .collection("users/bob/games") + .sort(field("name").ascending()) + .execute(); +// [END collection_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/collection_group_example.js b/snippets/firestore-temp/test-firestore/collection_group_example.js new file mode 100644 index 00000000..25c6d8da --- /dev/null +++ b/snippets/firestore-temp/test-firestore/collection_group_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_group_example_modular] +const results = await db.pipeline() + .collectionGroup("games") + .sort(field("name").ascending()) + .execute(); +// [END collection_group_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/cond_function.js b/snippets/firestore-temp/test-firestore/cond_function.js new file mode 100644 index 00000000..e630d758 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/cond_function.js @@ -0,0 +1,20 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START cond_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("tags").arrayConcat([ + conditional( + field("pages").greaterThan(100), + constant("longRead"), + constant("shortRead") + ) + ]).as("extendedTags") + ) + .execute(); +// [END cond_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/cosine_distance.js b/snippets/firestore-temp/test-firestore/cosine_distance.js new file mode 100644 index 00000000..c5a4293a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/cosine_distance.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START cosine_distance_modular] +const sampleVector = [0.0, 1, 2, 3, 4, 5]; +const result = await db.pipeline() + .collection("books") + .select( + field("embedding").cosineDistance(sampleVector).as("cosineDistance") + ) + .execute(); +// [END cosine_distance_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/count_distinct.js b/snippets/firestore-temp/test-firestore/count_distinct.js new file mode 100644 index 00000000..bb68e3f6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/count_distinct.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START count_distinct_modular] +const result = await db.pipeline() + .collection("books") + .aggregate(field("author").countDistinct().as("unique_authors")) + .execute(); +// [END count_distinct_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/count_function.js b/snippets/firestore-temp/test-firestore/count_function.js new file mode 100644 index 00000000..aceba2d6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/count_function.js @@ -0,0 +1,19 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START count_function_modular] +// Total number of books in the collection +const countOfAll = await db.pipeline() + .collection("books") + .aggregate(countAll().as("count")) + .execute(); + +// Number of books with nonnull `ratings` field +const countField = await db.pipeline() + .collection("books") + .aggregate(field("ratings").count().as("count")) + .execute(); +// [END count_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/count_if.js b/snippets/firestore-temp/test-firestore/count_if.js new file mode 100644 index 00000000..198574bc --- /dev/null +++ b/snippets/firestore-temp/test-firestore/count_if.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START count_if_modular] +const result = await db.pipeline() + .collection("books") + .aggregate( + field("rating").greaterThan(4).countIf().as("filteredCount") + ) + .execute(); +// [END count_if_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/covered_query.js b/snippets/firestore-temp/test-firestore/covered_query.js new file mode 100644 index 00000000..371a3c81 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/covered_query.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START covered_query_modular] +const results = await db.pipeline() + .collection("books") + .where(like(field("category"), "%fantasy%")) + .where(field("title").exists()) + .where(field("author").exists()) + .select(field("title"), field("author")) + .execute(); +// [END covered_query_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/database_example.js b/snippets/firestore-temp/test-firestore/database_example.js new file mode 100644 index 00000000..ab9c3f54 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/database_example.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START database_example_modular] +// Count all documents in the database +const results = await db.pipeline() + .database() + .aggregate(countAll().as("total")) + .execute(); +// [END database_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/divide_function.js b/snippets/firestore-temp/test-firestore/divide_function.js new file mode 100644 index 00000000..97507210 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/divide_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START divide_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) + .execute(); +// [END divide_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/documents_example.js b/snippets/firestore-temp/test-firestore/documents_example.js new file mode 100644 index 00000000..a515dd36 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/documents_example.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START documents_example_modular] +const results = await db.pipeline() + .documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY") + ]) + .execute(); +// [END documents_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/dot_product.js b/snippets/firestore-temp/test-firestore/dot_product.js new file mode 100644 index 00000000..f102e93d --- /dev/null +++ b/snippets/firestore-temp/test-firestore/dot_product.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START dot_product_modular] +const sampleVector = [0.0, 1, 2, 3, 4, 5]; +const result = await db.pipeline() + .collection("books") + .select( + field("embedding").dotProduct(sampleVector).as("dotProduct") + ) + .execute(); +// [END dot_product_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/ends_with.js b/snippets/firestore-temp/test-firestore/ends_with.js new file mode 100644 index 00000000..60b648e1 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/ends_with.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START ends_with_modular] +const result = await db.pipeline() + .collection("inventory/devices/laptops") + .select( + field("name").endsWith("16 inch") + .as("16InLaptops") + ) + .execute(); +// [END ends_with_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/eq_any.js b/snippets/firestore-temp/test-firestore/eq_any.js new file mode 100644 index 00000000..e73dde30 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/eq_any.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START eq_any_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) + .as("matchesGenreFilters") + ) + .execute(); +// [END eq_any_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/equal_function.js b/snippets/firestore-temp/test-firestore/equal_function.js new file mode 100644 index 00000000..36ae12ad --- /dev/null +++ b/snippets/firestore-temp/test-firestore/equal_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START equal_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").equal(5).as("hasPerfectRating")) + .execute(); +// [END equal_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/euclidean_distance.js b/snippets/firestore-temp/test-firestore/euclidean_distance.js new file mode 100644 index 00000000..f8bf9e87 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/euclidean_distance.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START euclidean_distance_modular] +const sampleVector = [0.0, 1, 2, 3, 4, 5]; +const result = await db.pipeline() + .collection("books") + .select( + field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") + ) + .execute(); +// [END euclidean_distance_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/exists_function.js b/snippets/firestore-temp/test-firestore/exists_function.js new file mode 100644 index 00000000..563c7b9e --- /dev/null +++ b/snippets/firestore-temp/test-firestore/exists_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START exists_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").exists().as("hasRating")) + .execute(); +// [END exists_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/exp_function.js b/snippets/firestore-temp/test-firestore/exp_function.js new file mode 100644 index 00000000..a74efb19 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/exp_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START exp_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").exp().as("expRating")) + .execute(); +// [END exp_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/field_or_constant.js b/snippets/firestore-temp/test-firestore/field_or_constant.js new file mode 100644 index 00000000..de7059c9 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/field_or_constant.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START field_or_constant_modular] +const pipeline = db.pipeline() + .collection("cities") + .where(field("name").equal(constant("Toronto"))); +// [END field_or_constant_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/floor_function.js b/snippets/firestore-temp/test-firestore/floor_function.js new file mode 100644 index 00000000..bc23f0fb --- /dev/null +++ b/snippets/firestore-temp/test-firestore/floor_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START floor_function_modular] +const result = await db.pipeline() + .collection("books") + .addFields( + field("wordCount").divide(field("pages")).floor().as("wordsPerPage") + ) + .execute(); +// [END floor_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/full_replace.js b/snippets/firestore-temp/test-firestore/full_replace.js new file mode 100644 index 00000000..9c2e3517 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/full_replace.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START full_replace_modular] +const names = await db.pipeline() + .collection("cities") + .replaceWith(field("location")) + .execute(); +// [END full_replace_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/functions_example.js b/snippets/firestore-temp/test-firestore/functions_example.js new file mode 100644 index 00000000..0c4d080b --- /dev/null +++ b/snippets/firestore-temp/test-firestore/functions_example.js @@ -0,0 +1,21 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START functions_example_modular] +let results; + +// Type 1: Scalar (for use in non-aggregation stages) +// Example: Return the min store price for each book. +results = await db.pipeline().collection("books") + .select(field("current").logicalMinimum(field("updated")).as("price_min")) + .execute(); + +// Type 2: Aggregation (for use in aggregate stages) +// Example: Return the min price of all books. +results = await db.pipeline().collection("books") + .aggregate(field("price").minimum().as("min_price")) + .execute(); +// [END functions_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/greater_or_equal.js b/snippets/firestore-temp/test-firestore/greater_or_equal.js new file mode 100644 index 00000000..77c01510 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/greater_or_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START greater_or_equal_modular] +const result = await db.pipeline() + .collection("books") + .select(field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")) + .execute(); +// [END greater_or_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/greater_than.js b/snippets/firestore-temp/test-firestore/greater_than.js new file mode 100644 index 00000000..f727a8df --- /dev/null +++ b/snippets/firestore-temp/test-firestore/greater_than.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START greater_than_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").greaterThan(4).as("hasHighRating")) + .execute(); +// [END greater_than_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/initial_data.js b/snippets/firestore-temp/test-firestore/initial_data.js new file mode 100644 index 00000000..0d8ea4b4 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/initial_data.js @@ -0,0 +1,35 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START initial_data_modular] +await db.collection("cities").doc("SF").set({ + "name": "San Francisco", + "population": 800000, + "location": { + "country": "USA", + "state": "California" + } +}); +await db.collection("cities").doc("TO").set({ + "name": "Toronto", + "population": 3000000, + "province": "ON", + "location": { + "country": "Canada", + "province": "Ontario" + } +}); +await db.collection("cities").doc("NY").set({ + "name": "New York", + "location": { + "country": "USA", + "state": "New York" + } +}); +await db.collection("cities").doc("AT").set({ + "name": "Atlantis", +}); +// [END initial_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/input_stages.js b/snippets/firestore-temp/test-firestore/input_stages.js new file mode 100644 index 00000000..432c0758 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/input_stages.js @@ -0,0 +1,25 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START input_stages_modular] +let results; + +// Return all restaurants in San Francisco +results = await db.pipeline().collection("cities/sf/restaurants").execute(); + +// Return all restaurants +results = await db.pipeline().collectionGroup("restaurants").execute(); + +// Return all documents across all collections in the database (the entire database) +results = await db.pipeline().database().execute(); + +// Batch read of 3 documents +results = await db.pipeline().documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY"), +]).execute(); +// [END input_stages_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/less_or_equal.js b/snippets/firestore-temp/test-firestore/less_or_equal.js new file mode 100644 index 00000000..1e6d427a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/less_or_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START less_or_equal_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").lessThanOrEqual(2).as("hasBadRating")) + .execute(); +// [END less_or_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/less_than.js b/snippets/firestore-temp/test-firestore/less_than.js new file mode 100644 index 00000000..f831bb91 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/less_than.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START less_than_modular] +const result = await db.pipeline() + .collection("books") + .select(field("published").lessThan(1923).as("isPublicDomainProbably")) + .execute(); +// [END less_than_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/like.js b/snippets/firestore-temp/test-firestore/like.js new file mode 100644 index 00000000..df0c5ffa --- /dev/null +++ b/snippets/firestore-temp/test-firestore/like.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START like_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("genre").like("%Fiction") + .as("anyFiction") + ) + .execute(); +// [END like_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/ln_function.js b/snippets/firestore-temp/test-firestore/ln_function.js new file mode 100644 index 00000000..31b3cd67 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/ln_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START ln_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").ln().as("lnRating")) + .execute(); +// [END ln_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/log_function.js b/snippets/firestore-temp/test-firestore/log_function.js new file mode 100644 index 00000000..20add84a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/log_function.js @@ -0,0 +1,9 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START log_function_modular] +// Not supported on JS +// [END log_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/map_get.js b/snippets/firestore-temp/test-firestore/map_get.js new file mode 100644 index 00000000..f08df243 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/map_get.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START map_get_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("awards").mapGet("pulitzer").as("hasPulitzerAward") + ) + .execute(); +// [END map_get_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/map_merge_overwrite.js b/snippets/firestore-temp/test-firestore/map_merge_overwrite.js new file mode 100644 index 00000000..92c1f05b --- /dev/null +++ b/snippets/firestore-temp/test-firestore/map_merge_overwrite.js @@ -0,0 +1,9 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START map_merge_overwrite_modular] +// unsupported in client SDKs for now +// [END map_merge_overwrite_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/max_function.js b/snippets/firestore-temp/test-firestore/max_function.js new file mode 100644 index 00000000..c553370a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/max_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START max_function_modular] +const result = await db.pipeline() + .collection("books") + .aggregate(field("price").maximum().as("maximumPrice")) + .execute(); +// [END max_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/max_logical_function.js b/snippets/firestore-temp/test-firestore/max_logical_function.js new file mode 100644 index 00000000..fbe6ce26 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/max_logical_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START max_logical_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("rating").logicalMaximum(1).as("flooredRating") + ) + .execute(); +// [END max_logical_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/min_function.js b/snippets/firestore-temp/test-firestore/min_function.js new file mode 100644 index 00000000..92978486 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/min_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START min_function_modular] +const result = await db.pipeline() + .collection("books") + .aggregate(field("price").minimum().as("minimumPrice")) + .execute(); +// [END min_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/min_logical_function.js b/snippets/firestore-temp/test-firestore/min_logical_function.js new file mode 100644 index 00000000..d2f8f0df --- /dev/null +++ b/snippets/firestore-temp/test-firestore/min_logical_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START min_logical_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("rating").logicalMinimum(5).as("cappedRating") + ) + .execute(); +// [END min_logical_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/mod_function.js b/snippets/firestore-temp/test-firestore/mod_function.js new file mode 100644 index 00000000..c7859f76 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/mod_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START mod_function_modular] +const displayCapacity = 1000; +const result = await db.pipeline() + .collection("books") + .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) + .execute(); +// [END mod_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/multiply_function.js b/snippets/firestore-temp/test-firestore/multiply_function.js new file mode 100644 index 00000000..bbe1ca6f --- /dev/null +++ b/snippets/firestore-temp/test-firestore/multiply_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START multiply_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("price").multiply(field("soldBooks")).as("revenue")) + .execute(); +// [END multiply_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/not_eq_any.js b/snippets/firestore-temp/test-firestore/not_eq_any.js new file mode 100644 index 00000000..68fca3ee --- /dev/null +++ b/snippets/firestore-temp/test-firestore/not_eq_any.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START not_eq_any_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) + .as("byExcludedAuthors") + ) + .execute(); +// [END not_eq_any_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/not_equal.js b/snippets/firestore-temp/test-firestore/not_equal.js new file mode 100644 index 00000000..837f08a5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/not_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START not_equal_modular] +const result = await db.pipeline() + .collection("books") + .select(field("title").notEqual("1984").as("not1984")) + .execute(); +// [END not_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/not_function.js b/snippets/firestore-temp/test-firestore/not_function.js new file mode 100644 index 00000000..1ecb8178 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/not_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START not_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("tags").arrayContains("nonfiction").not() + .as("isFiction") + ) + .execute(); +// [END not_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/or_function.js b/snippets/firestore-temp/test-firestore/or_function.js new file mode 100644 index 00000000..19ee8be3 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/or_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START or_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) + .as("matchesSearchFilters") + ) + .execute(); +// [END or_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/pagination_not_supported_preview.js b/snippets/firestore-temp/test-firestore/pagination_not_supported_preview.js new file mode 100644 index 00000000..93a797f5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/pagination_not_supported_preview.js @@ -0,0 +1,38 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pagination_not_supported_preview_modular] +// Existing pagination via `startAt()` +const q = + db.collection("cities").orderBy("population").startAt(1000000); + +// Private preview workaround using pipelines +const pageSize = 2; +const pipeline = db.pipeline() + .collection("cities") + .select("name", "population", "__name__") + .sort(field("population").descending(), field("__name__").ascending()); + +// Page 1 results +let snapshot = await pipeline.limit(pageSize).execute(); + +// End of page marker +const lastDoc = snapshot.results[snapshot.results.length - 1]; + +// Page 2 results +snapshot = await pipeline + .where( + or( + and( + field("population").equal(lastDoc.get("population")), + field("__name__").greaterThan(lastDoc.ref) + ), + field("population").lessThan(lastDoc.get("population")) + ) + ) + .limit(pageSize) + .execute(); +// [END pagination_not_supported_preview_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/pipeline_concepts.js b/snippets/firestore-temp/test-firestore/pipeline_concepts.js new file mode 100644 index 00000000..60e862b8 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/pipeline_concepts.js @@ -0,0 +1,18 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pipeline_concepts_modular] +const pipeline = db.pipeline() + // Step 1: Start a query with collection scope + .collection("cities") + // Step 2: Filter the collection + .where(field("population").greaterThan(100000)) + // Step 3: Sort the remaining documents + .sort(field("name").ascending()) + // Step 4: Return the top 10. Note applying the limit earlier in the + // pipeline would have unintentional results. + .limit(10); +// [END pipeline_concepts_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/pipeline_initialization.js b/snippets/firestore-temp/test-firestore/pipeline_initialization.js new file mode 100644 index 00000000..806986f6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/pipeline_initialization.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pipeline_initialization_modular] +import { Firestore } from "@google-cloud/firestore"; +const database = new Firestore({ + projectId: 'your-project-id', + databaseId: 'your-new-enterprise-database' +}); +const pipeline = database.pipeline(); +// [END pipeline_initialization_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/pipeline_where.js b/snippets/firestore-temp/test-firestore/pipeline_where.js new file mode 100644 index 00000000..ab9ba88b --- /dev/null +++ b/snippets/firestore-temp/test-firestore/pipeline_where.js @@ -0,0 +1,18 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pipeline_where_modular] +let results; + +results = await db.pipeline().collection("books") + .where(field("rating").equal(5)) + .where(field("published").lessThan(1900)) + .execute(); + +results = await db.pipeline().collection("books") + .where(and(field("rating").equal(5), field("published").lessThan(1900))) + .execute(); +// [END pipeline_where_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/pow_function.js b/snippets/firestore-temp/test-firestore/pow_function.js new file mode 100644 index 00000000..e13d2117 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/pow_function.js @@ -0,0 +1,27 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pow_function_modular] +const googleplex = { latitude: 37.4221, longitude: 122.0853 }; +const result = await db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + .execute(); +// [END pow_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/query_example.js b/snippets/firestore-temp/test-firestore/query_example.js new file mode 100644 index 00000000..eb3ead85 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/query_example.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START query_example_modular] +const results = await db.pipeline() + .collection("books") + .where(field("published").lessThan(1900)) + .where(field("genre").equal("Science Fiction")) + .where(field("rating").greaterThan(4.3)) + .sort(field("published").descending()) + .execute(); +// [END query_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/regex_contains.js b/snippets/firestore-temp/test-firestore/regex_contains.js new file mode 100644 index 00000000..33b617f5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/regex_contains.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START regex_contains_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("title").regexContains("Firestore (Enterprise|Standard)") + .as("isFirestoreRelated") + ) + .execute(); +// [END regex_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/regex_match.js b/snippets/firestore-temp/test-firestore/regex_match.js new file mode 100644 index 00000000..5dd8f123 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/regex_match.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START regex_match_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("title").regexMatch("Firestore (Enterprise|Standard)") + .as("isFirestoreExactly") + ) + .execute(); +// [END regex_match_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/round_function.js b/snippets/firestore-temp/test-firestore/round_function.js new file mode 100644 index 00000000..37017562 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/round_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START round_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) + .aggregate(field("partialRevenue").sum().as("totalRevenue")) + .execute(); +// [END round_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sample_example.js b/snippets/firestore-temp/test-firestore/sample_example.js new file mode 100644 index 00000000..e5cb7443 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sample_example.js @@ -0,0 +1,25 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_example_modular] +let results; + +// Get a sample of 100 documents in a database +results = await db.pipeline() + .database() + .sample(100) + .execute(); + +// Randomly shuffle a list of 3 documents +results = await db.pipeline() + .documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY") + ]) + .sample(3) + .execute(); +// [END sample_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sample_percent.js b/snippets/firestore-temp/test-firestore/sample_percent.js new file mode 100644 index 00000000..bb6876e9 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sample_percent.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_percent_modular] +// Get a sample of on average 50% of the documents in the database +const results = await db.pipeline() + .database() + .sample({ percentage: 0.5 }) + .execute(); +// [END sample_percent_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sort.js b/snippets/firestore-temp/test-firestore/sort.js new file mode 100644 index 00000000..62dc86c4 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sort.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sort_modular] +const results = await db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ) + .execute(); +// [END sort_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sort_comparison.js b/snippets/firestore-temp/test-firestore/sort_comparison.js new file mode 100644 index 00000000..d4c861a5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sort_comparison.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sort_comparison_modular] +const q = db.collection("cities") + .orderBy("state") + .orderBy("population", "desc"); + +const pipeline = db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ); +// [END sort_comparison_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sparse_index_example.js b/snippets/firestore-temp/test-firestore/sparse_index_example.js new file mode 100644 index 00000000..628ed8a6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sparse_index_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sparse_index_example_modular] +const results = await db.pipeline() + .collection("books") + .where(like(field("category"), "%fantasy%")) + .execute(); +// [END sparse_index_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sqrt_function.js b/snippets/firestore-temp/test-firestore/sqrt_function.js new file mode 100644 index 00000000..fc24ead6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sqrt_function.js @@ -0,0 +1,27 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sqrt_function_modular] +const googleplex = { latitude: 37.4221, longitude: 122.0853 }; +const result = await db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + .execute(); +// [END sqrt_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/starts_with.js b/snippets/firestore-temp/test-firestore/starts_with.js new file mode 100644 index 00000000..8cbfb6de --- /dev/null +++ b/snippets/firestore-temp/test-firestore/starts_with.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START starts_with_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("title").startsWith("The") + .as("needsSpecialAlphabeticalSort") + ) + .execute(); +// [END starts_with_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/str_concat.js b/snippets/firestore-temp/test-firestore/str_concat.js new file mode 100644 index 00000000..6386fef2 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/str_concat.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START str_concat_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("title").stringConcat(" by ", field("author")) + .as("fullyQualifiedTitle") + ) + .execute(); +// [END str_concat_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/str_reverse.js b/snippets/firestore-temp/test-firestore/str_reverse.js new file mode 100644 index 00000000..60b50ffc --- /dev/null +++ b/snippets/firestore-temp/test-firestore/str_reverse.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START str_reverse_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("name").reverse().as("reversedName") + ) + .execute(); +// [END str_reverse_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/string_contains.js b/snippets/firestore-temp/test-firestore/string_contains.js new file mode 100644 index 00000000..a785fc74 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/string_contains.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START string_contains_modular] +const result = await db.pipeline() + .collection("articles") + .select( + field("body").stringContains("Firestore") + .as("isFirestoreRelated") + ) + .execute(); +// [END string_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/substr_function.js b/snippets/firestore-temp/test-firestore/substr_function.js new file mode 100644 index 00000000..051f2dbb --- /dev/null +++ b/snippets/firestore-temp/test-firestore/substr_function.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START substr_function_modular] +const result = await db.pipeline() + .collection("books") + .where(field("title").startsWith("The ")) + .select( + field("title").substring(4) + .as("titleWithoutLeadingThe") + ) + .execute(); +// [END substr_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/subtract_function.js b/snippets/firestore-temp/test-firestore/subtract_function.js new file mode 100644 index 00000000..e350cdd0 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/subtract_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START subtract_function_modular] +const storeCredit = 7; +const result = await db.pipeline() + .collection("books") + .select(field("price").subtract(constant(storeCredit)).as("totalCost")) + .execute(); +// [END subtract_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sum_function.js b/snippets/firestore-temp/test-firestore/sum_function.js new file mode 100644 index 00000000..26c97bf0 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sum_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sum_function_modular] +const result = await db.pipeline() + .collection("cities") + .aggregate(field("population").sum().as("totalPopulation")) + .execute(); +// [END sum_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/timestamp_add.js b/snippets/firestore-temp/test-firestore/timestamp_add.js new file mode 100644 index 00000000..b0686a16 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/timestamp_add.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_add_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("createdAt").timestampAdd("day", 3653).as("expiresAt") + ) + .execute(); +// [END timestamp_add_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/timestamp_sub.js b/snippets/firestore-temp/test-firestore/timestamp_sub.js new file mode 100644 index 00000000..c62f6fb8 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/timestamp_sub.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_sub_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") + ) + .execute(); +// [END timestamp_sub_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/timestamp_unix_micros.js b/snippets/firestore-temp/test-firestore/timestamp_unix_micros.js new file mode 100644 index 00000000..e060f6ca --- /dev/null +++ b/snippets/firestore-temp/test-firestore/timestamp_unix_micros.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_unix_micros_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMicros().as("unixMicros") + ) + .execute(); +// [END timestamp_unix_micros_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/timestamp_unix_millis.js b/snippets/firestore-temp/test-firestore/timestamp_unix_millis.js new file mode 100644 index 00000000..690e5d8c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/timestamp_unix_millis.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_unix_millis_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMillis().as("unixMillis") + ) + .execute(); +// [END timestamp_unix_millis_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/timestamp_unix_seconds.js b/snippets/firestore-temp/test-firestore/timestamp_unix_seconds.js new file mode 100644 index 00000000..a272cae5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/timestamp_unix_seconds.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_unix_seconds_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixSeconds().as("unixSeconds") + ) + .execute(); +// [END timestamp_unix_seconds_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/to_lower.js b/snippets/firestore-temp/test-firestore/to_lower.js new file mode 100644 index 00000000..c40d0780 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/to_lower.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START to_lower_modular] +const result = await db.pipeline() + .collection("authors") + .select( + field("genre").toLower().equal("fantasy") + .as("isFantasy") + ) + .execute(); +// [END to_lower_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/to_upper.js b/snippets/firestore-temp/test-firestore/to_upper.js new file mode 100644 index 00000000..ec9b64d2 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/to_upper.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START to_upper_modular] +const result = await db.pipeline() + .collection("authors") + .select( + field("name").toUpper() + .as("uppercaseName") + ) + .execute(); +// [END to_upper_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/trim_function.js b/snippets/firestore-temp/test-firestore/trim_function.js new file mode 100644 index 00000000..c0bf58da --- /dev/null +++ b/snippets/firestore-temp/test-firestore/trim_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START trim_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("name").trim().as("whitespaceTrimmedName") + ) + .execute(); +// [END trim_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/union_stage.js b/snippets/firestore-temp/test-firestore/union_stage.js new file mode 100644 index 00000000..67637868 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/union_stage.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START union_stage_modular] +const results = await db.pipeline() + .collection("cities/SF/restaurants") + .where(field("type").equal("Chinese")) + .union(db.pipeline() + .collection("cities/NY/restaurants") + .where(field("type").equal("Italian"))) + .where(field("rating").greaterThanOrEqual(4.5)) + .sort(field("__name__").descending()) + .execute(); +// [END union_stage_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unix_micros_timestamp.js b/snippets/firestore-temp/test-firestore/unix_micros_timestamp.js new file mode 100644 index 00000000..fefc3c1f --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unix_micros_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unix_micros_timestamp_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") + ) + .execute(); +// [END unix_micros_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unix_millis_timestamp.js b/snippets/firestore-temp/test-firestore/unix_millis_timestamp.js new file mode 100644 index 00000000..ed5f260f --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unix_millis_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unix_millis_timestamp_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") + ) + .execute(); +// [END unix_millis_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unix_seconds_timestamp.js b/snippets/firestore-temp/test-firestore/unix_seconds_timestamp.js new file mode 100644 index 00000000..a5b8a674 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unix_seconds_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unix_seconds_timestamp_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") + ) + .execute(); +// [END unix_seconds_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_edge_cases.js b/snippets/firestore-temp/test-firestore/unnest_edge_cases.js new file mode 100644 index 00000000..383acb3e --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_edge_cases.js @@ -0,0 +1,21 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_edge_cases_modular] +// Input +// { identifier : 1, neighbors: [ "Alice", "Cathy" ] } +// { identifier : 2, neighbors: [] } +// { identifier : 3, neighbors: "Bob" } +const results = await db.pipeline() + .database() + .unnest(field("neighbors").as("unnestedNeighbors"), "index" ) + .execute(); + +// Output +// { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Alice", index: 0 } +// { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Cathy", index: 1 } +// { identifier: 3, neighbors: "Bob", index: null} +// [END unnest_edge_cases_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_stage.js b/snippets/firestore-temp/test-firestore/unnest_stage.js new file mode 100644 index 00000000..58390262 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_stage.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_stage_modular] +const results = await db.pipeline() + .database() + .unnest(field("arrayField").as("unnestedArrayField"), "index") + .execute(); +// [END unnest_stage_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/vector_length.js b/snippets/firestore-temp/test-firestore/vector_length.js new file mode 100644 index 00000000..6cf36022 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/vector_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START vector_length_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("embedding").vectorLength().as("vectorLength") + ) + .execute(); +// [END vector_length_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/xor_function.js b/snippets/firestore-temp/test-firestore/xor_function.js new file mode 100644 index 00000000..0c5906f3 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/xor_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START xor_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) + .as("matchesSearchFilters") + ) + .execute(); +// [END xor_function_modular] \ No newline at end of file