diff --git a/firestore-next/package.json b/firestore-next/package.json index faff0996..f87855b5 100644 --- a/firestore-next/package.json +++ b/firestore-next/package.json @@ -6,7 +6,7 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^12.4.0", + "firebase": "eap-firestore-pipelines", "geofire-common": "^6.0.0" }, "devDependencies": { diff --git a/firestore-next/test.firestore.js b/firestore-next/test.firestore.js index 0300fb6f..1e42ec8a 100644 --- a/firestore-next/test.firestore.js +++ b/firestore-next/test.firestore.js @@ -2,7 +2,7 @@ // [SNIPPETS_SEPARATION enabled] const { expect } = require('chai'); -import { or } from "firebase/firestore"; +import { addDoc, or } from "firebase/firestore"; // [START city_custom_object] class City { @@ -1323,3 +1323,2052 @@ describe("firestore", () => { }); }); }); + +describe("firestore-pipelines", () => { + const { + Firestore, + Timestamp, + collection, + doc, + getFirestore, + orderBy, + query, + setDoc, + startAt, + } = require("firebase/firestore") + const { + Pipeline, + array, + field, + constant, + countAll, + sum, + average, + maximum, + AggregateFunction, + and, + or, + xor, + conditional, + like, + execute + } = require("firebase/firestore/pipelines"); + + let app; + /** @type {Firestore} */ let db; + + before(() => { + const { initializeApp } = require("firebase/app"); + const config = { + apiKey: "AIzaSyCM61mMr_iZnP1DzjT1PMB5vDGxfyWNM64", + authDomain: "firestore-snippets.firebaseapp.com", + projectId: "firestore-snippets" + }; + app = initializeApp(config); + db = getFirestore(app, "enterprise"); + }); + + async function stagesExpressionsExample() { + // [START stages_expressions_example] + const trailing30Days = constant(Timestamp.now().toMillis()) + .unixMillisToTimestamp() + .timestampSubtract("day", 30); + const snapshot = await execute(db.pipeline() + .collection("productViews") + .where(field("viewedAt").greaterThan(trailing30Days)) + .aggregate(field("productId").countDistinct().as("uniqueProductViews")) + ); + // [END stages_expressions_example] + console.log(snapshot); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/where + + async function createWhereData() { + // [START create_where_data] + await setDoc(doc(collection(db, "cities"), "SF"), + { name: "San Francisco", state: "CA", country: "USA", population: 870000 } + ); + + await setDoc(doc(collection(db, "cities"), "LA"), + { name: "Los Angeles", state: "CA", country: "USA", population: 3970000 } + ); + await setDoc(doc(collection(db, "cities"), "NY"), + { name: "New York", state: "NY", country: "USA", population: 8530000 } + ); + await setDoc(doc(collection(db, "cities"), "TOR"), + { name: "Toronto", state: null, country: "Canada", population: 2930000 } + ); + await setDoc(doc(collection(db, "cities"), "MEX"), + { name: "Mexico City", state: null, country: "Mexico", population: 9200000 } + ); + // [END create_where_data] + } + + async function whereEqualityExample() { + // [START where_equality_example] + const cities = await execute(db.pipeline() + .collection("cities") + .where(field("state").equal("CA"))); + // [END where_equality_example] + console.log(cities); + } + + async function whereMultipleStagesExample() { + // [START where_multiple_stages] + const cities = await execute(db.pipeline() + .collection("cities") + .where(field("location.country").equal("USA")) + .where(field("population").greaterThan(500000))); + // [END where_multiple_stages] + console.log(cities); + } + + async function whereComplexExample() { + // [START where_complex] + const cities = await execute(db.pipeline() + .collection("cities") + .where( + or( + like(field("name"), "San%"), + and( + field("location.state").charLength().greaterThan(7), + field("location.country").equal("USA") + ) + ) + )); + // [END where_complex] + console.log(cities); + } + + async function whereStageOrderExample() { + // [START where_stage_order] + const cities = await execute(db.pipeline() + .collection("cities") + .limit(10) + .where(field("location.country").equal("USA"))); + // [END where_stage_order] + console.log(cities); + } + + async function whereHavingExample() { + // [START where_having_example] + const cities = await execute(db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [field("population").sum().as("totalPopulation")], + groups: ["location.state"] + }) + .where(field("totalPopulation").greaterThan(10000000))); + // [END where_having_example] + console.log(cities); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/unnest + + async function unnestSyntaxExample() { + // [START unnest_syntax] + const userScore = await execute(db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt")); + // [END unnest_syntax] + console.log(userScore); + } + + async function unnestAliasIndexDataExample() { + // [START unnest_alias_index_data] + await addDoc(collection(db, "users"), {name: "foo", scores: [5, 4], userScore: 0}); + await addDoc(collection(db, "users"), {name: "bar", scores: [1, 3], attempt: 5}); + // [END unnest_alias_index_data] + } + + async function unnestAliasIndexExample() { + // [START unnest_alias_index] + const userScore = await execute(db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt")); + // [END unnest_alias_index] + console.log(userScore); + } + + async function unnestNonArrayDataExample() { + // [START unnest_nonarray_data] + await addDoc(collection(db, "users"), {name: "foo", scores: 1}); + await addDoc(collection(db, "users"), {name: "bar", scores: null}); + await addDoc(collection(db, "users"), {name: "qux", scores: {backupScores: 1}}); + // [END unnest_nonarray_data] + } + + async function unnestNonArrayExample() { + // [START unnest_nonarray] + const userScore = await execute(db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt")); + // [END unnest_nonarray] + console.log(userScore); + } + + async function unnestEmptyArrayDataExample() { + // [START unnest_empty_array_data] + await addDoc(collection(db, "users"), {name: "foo", scores: [5, 4]}); + await addDoc(collection(db, "users"), {name: "bar", scores: []}); + // [END unnest_empty_array_data] + } + + async function unnestEmptyArrayExample() { + // [START unnest_empty_array] + const userScore = await execute(db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt")); + // [END unnest_empty_array] + console.log(userScore); + } + + async function unnestPreserveEmptyArrayExample() { + // [START unnest_preserve_empty_array] + const userScore = await execute(db.pipeline() + .collection("users") + .unnest( + conditional( + field("scores").equal(array([])), + array([field("scores")]), + field("scores") + ).as("userScore"), + /* index_field= */ "attempt")); + // [END unnest_preserve_empty_array] + console.log(userScore); + } + + async function unnestNestedDataExample() { + // [START unnest_nested_data] + await addDoc(collection(db, "users"), { + name: "foo", + record: [ + { + scores: [5, 4], + avg: 4.5 + }, { + scores: [1, 3], + old_avg: 2 + } + ] + }); + // [END unnest_nested_data] + } + + async function unnestNestedExample() { + // [START unnest_nested] + const userScore = await execute(db.pipeline() + .collection("users") + .unnest(field("record").as("record")) + .unnest(field("record.scores").as("userScore"), /* index_field= */ "attempt")); + // [END unnest_nested] + console.log(userScore); + } + + // https://cloud.corp.google.com/firestore/docs/pipeline/stages/transformation/sample + + async function sampleSyntaxExample() { + // [START sample_syntax] + let sampled = await execute(db.pipeline() + .database() + .sample(50)); + + sampled = await execute(db.pipeline() + .database() + .sample({ percentage: 0.5 })); + // [END sample_syntax] + console.log(sampled); + } + + async function sampleDocumentsDataExample() { + // [START sample_documents_data] + await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francisco", state: "California"}); + await setDoc(doc(collection(db, "cities"), "NYC"), {name: "New York City", state: "New York"}); + await setDoc(doc(collection(db, "cities"), "CHI"), {name: "Chicago", state: "Illinois"}); + // [END sample_documents_data] + } + + async function sampleDocumentsExample() { + // [START sample_documents] + const sampled = await execute(db.pipeline() + .collection("cities") + .sample(1)); + // [END sample_documents] + console.log(sampled); + } + + async function sampleAllDocumentsExample() { + // [START sample_all_documents] + const sampled = await execute(db.pipeline() + .collection("cities") + .sample(5)); + // [END sample_all_documents] + console.log(sampled); + } + + async function samplePercentageDataExample() { + // [START sample_percentage_data] + await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francsico", state: "California"}); + await setDoc(doc(collection(db, "cities"), "NYC"), {name: "New York City", state: "New York"}); + await setDoc(doc(collection(db, "cities"), "CHI"), {name: "Chicago", state: "Illinois"}); + await setDoc(doc(collection(db, "cities"), "ATL"), {name: "Atlanta", state: "Georgia"}); + // [END sample_percentage_data] + } + + async function samplePercentageExample() { + // [START sample_percentage] + const sampled = await execute(db.pipeline() + .collection("cities") + .sample({ percentage: 0.5 })); + // [END sample_percentage] + console.log(sampled); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/sort + + async function sortSyntaxExample() { + // [START sort_syntax] + const results = await execute(db.pipeline() + .collection("cities") + .sort(field("population").ascending())); + // [END sort_syntax] + console.log(results); + } + + async function sortSyntaxExample2() { + // [START sort_syntax_2] + const results = await execute(db.pipeline() + .collection("cities") + .sort(field("name").charLength().ascending())); + // [END sort_syntax_2] + console.log(results); + } + + async function sortDocumentIDExample() { + // [START sort_document_id] + const results = await execute(db.pipeline() + .collection("cities") + .sort(field("country").ascending(), field("__name__").ascending())); + // [END sort_document_id] + console.log(results); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/select + + async function selectSyntaxExample() { + // [START select_syntax] + const names = await execute(db.pipeline() + .collection("cities") + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population" + )); + // [END select_syntax] + console.log(names); + } + + async function selectPositionDataExample() { + // [START select_position_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, location: {country: "Canada", province: "Ontario"} + }); + // [END select_position_data] + } + + async function selectPositionExample() { + // [START select_position] + const names = await execute(db.pipeline() + .collection("cities") + .where(field("location.country").equal("Canada")) + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population")); + // [END select_position] + console.log(names); + } + + async function selectBadPositionExample() { + // [START select_bad_position] + const names = await execute(db.pipeline() + .collection("cities") + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population") + .where(field("location.country").equal("Canada"))); + // [END select_bad_position] + console.log(names); + } + + async function selectNestedDataExample() { + // [START select_nested_data] + await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francisco", population: 800000, location: {country: "USA", state: "California"}, landmarks: ["Golden Gate Bridge", "Alcatraz"]}); + await setDoc(doc(collection(db, "cities"), "TO"), {name: "Toronto", population: 3000000, province: "ON", location: {country: "Canada", province: "Ontario"}, landmarks: ["CN Tower", "Casa Loma"]}); + await setDoc(doc(collection(db, "cities"), "AT"), {name: "Atlantis", population: null}); + // [END select_nested_data] + } + + async function selectNestedExample() { + // [START select_nested] + const locations = await execute(db.pipeline() + .collection("cities") + .select( + field("name").as("city"), + field("location.country").as("country"), + field("landmarks").arrayGet(0).as("topLandmark") + )); + // [END select_nested] + console.log(locations); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/remove_fields + + async function removeFieldsSyntaxExample() { + // [START remove_fields_syntax] + const results = await execute(db.pipeline() + .collection("cities") + .removeFields("population", "location.state")); + // [END remove_fields_syntax] + console.log(results); + } + + async function removeFieldsNestedDataExample() { + // [START remove_fields_nested_data] + await setDoc(doc(collection(db, "cities"), "SF"), { + name: "San Francisco", location: {country: "USA", state: "California"} + }); + await setDoc(doc(collection(db, "cities"), "TO"), { + name: "Toronto", location: {country: "Canada", province: "Ontario"} + }); + // [END remove_fields_nested_data] + } + + async function removeFieldsNestedExample() { + // [START remove_fields_nested] + const results = await execute(db.pipeline() + .collection("cities") + .removeFields("location.state")); + // [END remove_fields_nested] + console.log(results); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/limit + + async function limitSyntaxExample() { + // [START limit_syntax] + const results = await execute(db.pipeline() + .collection("cities") + .limit(10)); + // [END limit_syntax] + console.log(results); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/find_nearest + + async function findNearestSyntaxExample() { + // [START find_nearest_syntax] + const results = await execute(db.pipeline() + .collection("cities") + .findNearest({ + field: "embedding", + vectorValue: [1.5, 2.345], + distanceMeasure: "euclidean" + })); + // [END find_nearest_syntax] + } + + async function findNearestLimitExample() { + // [START find_nearest_limit] + const results = await execute(db.pipeline() + .collection("cities") + .findNearest({ + field: "embedding", + vectorValue: [1.5, 2.345], + distanceMeasure: "euclidean", + limit: 10 + })); + // [END find_nearest_limit] + console.log(results); + } + + async function findNearestDistanceDataExample() { + // [START find_nearest_distance_data] + await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francisco", embedding: [1.0, -1.0]}); + await setDoc(doc(collection(db, "cities"), "TO"), {name: "Toronto", embedding: [5.0, -10.0]}); + await setDoc(doc(collection(db, "cities"), "AT"), {name: "Atlantis", embedding: [2.0, -4.0]}); + // [END find_nearest_distance_data] + } + + async function findNearestDistanceExample() { + // [START find_nearest_distance] + const results = await execute(db.pipeline() + .collection("cities") + .findNearest({ + field: "embedding", + vectorValue: [1.3, 2.345], + distanceMeasure: "euclidean", + distanceField: "computedDistance", + })); + // [END find_nearest_distance] + console.log(results); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/offset + + async function offsetSyntaxExample() { + // [START offset_syntax] + const results = await execute(db.pipeline() + .collection("cities") + .offset(10)); + // [END offset_syntax] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/add_fields + + async function addFieldsSyntaxExample() { + // [START add_fields_syntax] + const results = await execute(db.pipeline() + .collection("users") + .addFields(field("firstName").stringConcat(" ", field("lastName")).as("fullName"))); + // [END add_fields_syntax] + } + + async function addFieldsOverlapExample() { + // [START add_fields_overlap] + const results = await execute(db.pipeline() + .collection("users") + .addFields(field("age").abs().as("age")) + .addFields(field("age").add(10).as("age"))); + // [END add_fields_overlap] + } + + async function addFieldsNestingExample() { + // [START add_fields_nesting] + const results = await execute(db.pipeline() + .collection("users") + .addFields(field("address.city").toLower().as("address.city"))); + // [END add_fields_nesting] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/input/collection + + async function collectionInputSyntaxExample() { + // [START collection_input_syntax] + const results = await execute(db.pipeline() + .collection("cities/SF/departments")); + // [END collection_input_syntax] + } + + async function collectionInputExampleData() { + // [START collection_input_data] + await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francsico", state: "California"}); + await setDoc(doc(collection(db, "cities"), "NYC"), {name: "New York City", state: "New York"}); + await setDoc(doc(collection(db, "cities"), "CHI"), {name: "Chicago", state: "Illinois"}); + await setDoc(doc(collection(db, "states"), "CA"), {name: "California"}); + // [END collection_input_data] + } + + async function collectionInputExample() { + // [START collection_input] + const results = await execute(db.pipeline() + .collection("cities") + .sort(field("name").ascending())); + // [END collection_input] + } + + async function subcollectionInputExampleData() { + // [START subcollection_input_data] + await setDoc(doc(collection(db, "cities/SF/departments"), "building"), + {name: "SF Building Deparment", employees: 750}); + await setDoc(doc(collection(db, "cities/NY/departments"), "building"), + {name: "NY Building Deparment", employees: 1000}); + await setDoc(doc(collection(db, "cities/CHI/departments"), "building"), + {name: "CHI Building Deparment", employees: 900}); + await setDoc(doc(collection(db, "cities/NY/departments"), "finance"), + {name: "NY Finance Deparment", employees: 1200}); + // [END subcollection_input_data] + } + + async function subcollectionInputExample() { + // [START subcollection_input] + const results = await execute(db.pipeline() + .collection("cities/NY/departments") + .sort(field("employees").ascending())); + // [END subcollection_input] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/input/collection_group + + async function collectionGroupInputSyntaxExample() { + // [START collection_group_input_syntax] + const results = await execute(db.pipeline() + .collectionGroup("departments")); + // [END collection_group_input_syntax] + } + + async function collectionGroupInputExampleData() { + // [START collection_group_data] + await setDoc(doc(collection(db, "cities/SF/departments"), "building"), + {name: "SF Building Deparment", employees: 750}); + await setDoc(doc(collection(db, "cities/NY/departments"), "building"), + {name: "NY Building Deparment", employees: 1000}); + await setDoc(doc(collection(db, "cities/CHI/departments"), "building"), + {name: "CHI Building Deparment", employees: 900}); + await setDoc(doc(collection(db, "cities/NY/departments"), "finance"), + {name: "NY Finance Deparment", employees: 1200}); + // [END collection_group_data] + } + + async function collectionGroupInputExample() { + // [START collection_group_input] + const results = await execute(db.pipeline() + .collectionGroup("departments") + .sort(field("employees").ascending())); + // [END collection_group_input] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/input/database + + async function databaseInputSyntaxExample() { + // [START database_syntax] + const results = await execute(db.pipeline() + .database()); + // [END database_syntax] + } + + async function databaseInputSyntaxExampleData() { + // [START database_input_data] + await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francsico", state: "California", population: 800000}); + await setDoc(doc(collection(db, "states"), "CA"), {name: "California", population: 39000000}); + await setDoc(doc(collection(db, "countries"), "USA"), {name: "United States of America", population: 340000000}); + // [END database_input_data] + } + + async function databaseInputExample() { + // [START database_input] + const results = await execute(db.pipeline() + .database() + .sort(field("population").ascending())); + // [END database_input] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/input/documents + + async function documentInputSyntaxExample() { + // [START document_input_syntax] + const results = await execute(db.pipeline() + .documents([ + doc(collection(db, "cities"), "SF"), + doc(collection(db, "cities"), "NYC")])); + // [END document_input_syntax] + } + + async function documentInputExampleData() { + // [START document_input_data] + await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francsico", state: "California"}); + await setDoc(doc(collection(db, "cities"), "NYC"), {name: "New York City", state: "New York"}); + await setDoc(doc(collection(db, "cities"), "CHI"), {name: "Chicago", state: "Illinois"}); + // [END document_input_data] + } + + async function documentInputExample() { + // [START document_input] + const results = await execute(db.pipeline() + .documents([ + doc(collection(db, "cities"), "SF"), + doc(collection(db, "cities"), "NYC")]) + .sort(field("name").ascending())); + // [END document_input] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/union + + async function unionSyntaxExample() { + // [START union_syntax] + const results = await execute(db.pipeline() + .collection("cities/SF/restaurants") + .union(db.pipeline().collection("cities/NYC/restaurants"))); + // [END union_syntax] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/aggregate + + async function aggregateSyntaxExample() { + // [START aggregate_syntax] + const cities = await execute(db.pipeline() + .collection("cities") + .aggregate( + countAll().as("total"), + average("population").as("averagePopulation") + )); + // [END aggregate_syntax] + } + + async function aggregateGroupSyntax() { + // [START aggregate_group_syntax] + const result = await execute(db.pipeline() + .collectionGroup("cities") + .aggregate({ + accumulators: [ + countAll().as("cities"), + field("population").sum().as("totalPopulation") + ], + groups: [field("location.state").as("state")] + })); + // [END aggregate_group_syntax] + } + + async function aggregateExampleData() { + // [START aggregate_data] + await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francisco", state: "CA", country: "USA", population: 870000}); + await setDoc(doc(collection(db, "cities"), "LA"), {name: "Los Angeles", state: "CA", country: "USA", population: 3970000}); + await setDoc(doc(collection(db, "cities"), "NY"), {name: "New York", state: "NY", country: "USA", population: 8530000}); + await setDoc(doc(collection(db, "cities"), "TOR"), {name: "Toronto", state: null, country: "Canada", population: 2930000}); + await setDoc(doc(collection(db, "cities"), "MEX"), {name: "Mexico City", state: null, country: "Mexico", population: 9200000}); + // [END aggregate_data] + } + + async function aggregateWithoutGroupExample() { + // [START aggregate_without_group] + const cities = await execute(db.pipeline() + .collection("cities") + .aggregate( + countAll().as("total"), + average("population").as("averagePopulation") + )); + // [END aggregate_without_group] + } + + async function aggregateGroupExample() { + // [START aggregate_group_example] + const cities = await execute(db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [ + countAll().as("numberOfCities"), + maximum("population").as("maxPopulation") + ], + groups: ["country", "state"] + })); + // [END aggregate_group_example] + } + + async function aggregateGroupComplexExample() { + // [START aggregate_group_complex] + const cities = await execute(db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [ + sum("population").as("totalPopulation") + ], + groups: [field("state").equal(null).as("stateIsNull")] + })); + // [END aggregate_group_complex] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/distinct + + async function distinctSyntaxExample() { + // [START distinct_syntax] + let cities = await execute(db.pipeline() + .collection("cities") + .distinct("country")); + + cities = await execute(db.pipeline() + .collection("cities") + .distinct( + field("state").toLower().as("normalizedState"), + field("country"))); + // [END distinct_syntax] + } + + async function distinctExampleData() { + // [START distinct_data] + await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francisco", state: "CA", country: "USA"}); + await setDoc(doc(collection(db, "cities"), "LA"), {name: "Los Angeles", state: "CA", country: "USA"}); + await setDoc(doc(collection(db, "cities"), "NY"), {name: "New York", state: "NY", country: "USA"}); + await setDoc(doc(collection(db, "cities"), "TOR"), {name: "Toronto", state: null, country: "Canada"}); + await setDoc(doc(collection(db, "cities"), "MEX"), {name: "Mexico City", state: null, country: "Mexico"}); + // [END distinct_data] + } + + async function distinctExample() { + // [START distinct_example] + const cities = await execute(db.pipeline() + .collection("cities") + .distinct("country")); + // [END distinct_example] + } + + async function distinctExpressionsExample() { + // [START distinct_expressions] + const cities = await execute(db.pipeline() + .collection("cities") + .distinct( + field("state").toLower().as("normalizedState"), + field("country"))); + // [END distinct_expressions] + } + + // old snippets + + async function basicRead() { + // [START basic_read] + const readDataPipeline = db.pipeline() + .collection("users"); + + // Execute the pipeline and handle the result + try { + const querySnapshot = await execute(readDataPipeline); + 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 { getFirestore } = require("firebase/firestore"); + const { execute } = require("firebase/firestore/pipelines"); + const database = getFirestore(app, "enterprise"); + 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 execute(db.pipeline().collection("cities/sf/restaurants")); + + // Return all restaurants + results = await execute(db.pipeline().collectionGroup("restaurants")); + + // Return all documents across all collections in the database (the entire database) + results = await execute(db.pipeline().database()); + + // Batch read of 3 documents + results = await execute(db.pipeline().documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "DC"), + doc(db, "cities", "NY") + ])); + // [END input_stages] + console.log(results); + } + + async function wherePipeline() { + // [START pipeline_where] + let results; + + results = await execute(db.pipeline().collection("books") + .where(field("rating").equal(5)) + .where(field("published").lessThan(1900)) + ); + + results = await execute(db.pipeline().collection("books") + .where(and(field("rating").equal(5), field("published").lessThan(1900))) + ); + // [END pipeline_where] + console.log(results); + } + + async function aggregateGroups() { + // [START aggregate_groups] + const results = await execute(db.pipeline() + .collection("books") + .aggregate( + field("rating").average().as("avg_rating") + ) + .distinct(field("genre")) + ); + // [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); + } +}); 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..fc7e87f1 --- /dev/null +++ b/firestore-temp/test.firestore.js @@ -0,0 +1,2102 @@ +// [SNIPPET_REGISTRY disabled] +// [SNIPPETS_SEPARATION enabled] + +const { expect } = require("chai"); + +describe("firestore-pipelines", () => { + const { + Firestore, + Timestamp + } = require("@google-cloud/firestore") + const { + Pipeline, + array, + arrayReverse, + average, + maximum, + sum, + field, + constant, + countAll, + AggregateFunction, + and, + like, + 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 queryExplainExample() { + // [START query_explain] + const q = db.collection("cities").where("country", "==", "USA"); + const options = { analyze: false }; + + const explainResults = await q.explain(options); + + const metrics = explainResults.metrics; + const plan = metrics.planSummary; + // [END query_explain] + } + + async function stagesExpressionsExample() { + // [START stages_expressions_example] + const trailing30Days = constant(Timestamp.now().toMillis()) + .unixMillisToTimestamp() + .timestampSubtract("day", 30); + const snapshot = await db.pipeline() + .collection("productViews") + .where(field("viewedAt").greaterThan(trailing30Days)) + .aggregate(field("productId").countDistinct().as("uniqueProductViews")) + .execute(); + // [END stages_expressions_example] + console.log(snapshot); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/where + + async function createWhereData() { + // [START create_where_data] + await db.collection("cities").doc("SF").set( + { name: "San Francisco", state: "CA", country: "USA", population: 870000 } + ); + await db.collection("cities").doc("LA").set( + { name: "Los Angeles", state: "CA", country: "USA", population: 3970000 } + ); + await db.collection("cities").doc("NY").set( + { name: "New York", state: "NY", country: "USA", population: 8530000 } + ); + await db.collection("cities").doc("TOR").set( + { name: "Toronto", state: null, country: "Canada", population: 2930000 } + ); + await db.collection("cities").doc("MEX").set( + { name: "Mexico City", state: null, country: "Mexico", population: 9200000 } + ); + // [END create_where_data] + } + + async function whereEqualityExample() { + // [START where_equality_example] + const cities = await db.pipeline() + .collection("cities") + .where(field("state").equal("CA")) + .execute(); + // [END where_equality_example] + console.log(cities); + } + + async function whereMultipleStagesExample() { + // [START where_multiple_stages] + const cities = await db.pipeline() + .collection("cities") + .where(field("location.country").equal("USA")) + .where(field("population").greaterThan(500000)) + .execute(); + // [END where_multiple_stages] + console.log(cities); + } + + async function whereComplexExample() { + // [START where_complex] + const cities = await db.pipeline() + .collection("cities") + .where( + or( + like(field("name"), "San%"), + and( + field("location.state").charLength().greaterThan(7), + field("location.country").equal("USA") + ) + ) + ).execute(); + // [END where_complex] + console.log(cities); + } + + async function whereStageOrderExample() { + // [START where_stage_order] + const cities = await db.pipeline() + .collection("cities") + .limit(10) + .where(field("location.country").equal("USA")) + .execute(); + // [END where_stage_order] + console.log(cities); + } + + async function whereHavingExample() { + // [START where_having_example] + const cities = await db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [field("population").sum().as("totalPopulation")], + groups: ["location.state"] + }) + .where(field("totalPopulation").greaterThan(10000000)) + .execute(); + // [END where_having_example] + console.log(cities); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/unnest + + async function unnestSyntaxExample() { + // [START unnest_syntax] + const userScore = await db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt") + .execute(); + // [END unnest_syntax] + console.log(userScore); + } + + async function unnestAliasIndexDataExample() { + // [START unnest_alias_index_data] + await db.collection("users").add({name: "foo", scores: [5, 4], userScore: 0}); + await db.collection("users").add({name: "bar", scores: [1, 3], attempt: 5}); + // [END unnest_alias_index_data] + } + + async function unnestAliasIndexExample() { + // [START unnest_alias_index] + const userScore = await db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt") + .execute(); + // [END unnest_alias_index] + console.log(userScore); + } + + async function unnestNonArrayDataExample() { + // [START unnest_nonarray_data] + await db.collection("users").add({name: "foo", scores: 1}); + await db.collection("users").add({name: "bar", scores: null}); + await db.collection("users").add({name: "qux", scores: {backupScores: 1}}); + // [END unnest_nonarray_data] + } + + async function unnestNonArrayExample() { + // [START unnest_nonarray] + const userScore = await db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt") + .execute(); + // [END unnest_nonarray] + console.log(userScore); + } + + async function unnestEmptyArrayDataExample() { + // [START unnest_empty_array_data] + await db.collection("users").add({name: "foo", scores: [5, 4]}); + await db.collection("users").add({name: "bar", scores: []}); + // [END unnest_empty_array_data] + } + + async function unnestEmptyArrayExample() { + // [START unnest_empty_array] + const userScore = await db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt") + .execute(); + // [END unnest_empty_array] + console.log(userScore); + } + + async function unnestPreserveEmptyArrayExample() { + // [START unnest_preserve_empty_array] + const userScore = await db.pipeline() + .collection("users") + .unnest( + conditional( + field("scores").equal(array([])), + array([field("scores")]), + field("scores") + ).as("userScore"), + /* index_field= */ "attempt") + .execute(); + // [END unnest_preserve_empty_array] + console.log(userScore); + } + + async function unnestNestedDataExample() { + // [START unnest_nested_data] + await db.collection("users").add({ + name: "foo", + record: [ + { + scores: [5, 4], + avg: 4.5 + }, { + scores: [1, 3], + old_avg: 2 + } + ] + }); + // [END unnest_nested_data] + } + + async function unnestNestedExample() { + // [START unnest_nested] + const userScore = await db.pipeline() + .collection("users") + .unnest(field("record").as("record")) + .unnest(field("record.scores").as("userScore"), /* index_field= */ "attempt") + .execute(); + // [END unnest_nested] + console.log(userScore); + } + + // https://cloud.corp.google.com/firestore/docs/pipeline/stages/transformation/sample + + async function sampleSyntaxExample() { + // [START sample_syntax] + let sampled = await db.pipeline() + .database() + .sample(50) + .execute(); + + sampled = await db.pipeline() + .database() + .sample({ percentage: 0.5 }) + .execute(); + // [END sample_syntax] + console.log(sampled); + } + + async function sampleDocumentsDataExample() { + // [START sample_documents_data] + await db.collection("cities").doc("SF").set({name: "San Francisco", state: "California"}); + await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"}); + await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"}); + // [END sample_documents_data] + } + + async function sampleDocumentsExample() { + // [START sample_documents] + const sampled = await db.pipeline() + .collection("cities") + .sample(1) + .execute(); + // [END sample_documents] + console.log(sampled); + } + + async function sampleAllDocumentsExample() { + // [START sample_all_documents] + const sampled = await db.pipeline() + .collection("cities") + .sample(5) + .execute(); + // [END sample_all_documents] + console.log(sampled); + } + + async function samplePercentageDataExample() { + // [START sample_percentage_data] + await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California"}); + await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"}); + await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"}); + await db.collection("cities").doc("ATL").set({name: "Atlanta", state: "Georgia"}); + // [END sample_percentage_data] + } + + async function samplePercentageExample() { + // [START sample_percentage] + const sampled = await db.pipeline() + .collection("cities") + .sample({ percentage: 0.5 }) + .execute(); + // [END sample_percentage] + console.log(sampled); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/sort + + async function sortSyntaxExample() { + // [START sort_syntax] + const results = await db.pipeline() + .collection("cities") + .sort(field("population").ascending()) + .execute(); + // [END sort_syntax] + console.log(results); + } + + async function sortSyntaxExample2() { + // [START sort_syntax_2] + const results = await db.pipeline() + .collection("cities") + .sort(field("name").charLength().ascending()) + .execute(); + // [END sort_syntax_2] + console.log(results); + } + + async function sortDocumentIDExample() { + // [START sort_document_id] + const results = await db.pipeline() + .collection("cities") + .sort(field("country").ascending(), field("__name__").ascending()) + .execute(); + // [END sort_document_id] + console.log(results); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/select + + async function selectSyntaxExample() { + // [START select_syntax] + const names = await db.pipeline() + .collection("cities") + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population" + ).execute(); + // [END select_syntax] + console.log(names); + } + + async function selectPositionDataExample() { + // [START select_position_data] + 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, location: {country: "Canada", province: "Ontario"} + }); + // [END select_position_data] + } + + async function selectPositionExample() { + // [START select_position] + const names = await db.pipeline() + .collection("cities") + .where(field("location.country").equal("Canada")) + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population") + .execute(); + // [END select_position] + console.log(names); + } + + async function selectBadPositionExample() { + // [START select_bad_position] + const names = await db.pipeline() + .collection("cities") + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population") + .where(field("location.country").equal("Canada")) + .execute(); + // [END select_bad_position] + console.log(names); + } + + async function selectNestedDataExample() { + // [START select_nested_data] + await db.collection("cities").doc("SF").set({name: "San Francisco", population: 800000, location: {country: "USA", state: "California"}, landmarks: ["Golden Gate Bridge", "Alcatraz"]}); + await db.collection("cities").doc("TO").set({name: "Toronto", population: 3000000, province: "ON", location: {country: "Canada", province: "Ontario"}, landmarks: ["CN Tower", "Casa Loma"]}); + await db.collection("cities").doc("AT").set({name: "Atlantis", population: null}); + // [END select_nested_data] + } + + async function selectNestedExample() { + // [START select_nested] + const locations = await db.pipeline() + .collection("cities") + .select( + field("name").as("city"), + field("location.country").as("country"), + field("landmarks").arrayGet(0).as("topLandmark") + ).execute(); + // [END select_nested] + console.log(locations); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/remove_fields + + async function removeFieldsSyntaxExample() { + // [START remove_fields_syntax] + const results = await db.pipeline() + .collection("cities") + .removeFields("population", "location.state") + .execute(); + // [END remove_fields_syntax] + console.log(results); + } + + async function removeFieldsNestedDataExample() { + // [START remove_fields_nested_data] + await db.collection("cities").doc("SF").set({ + name: "San Francisco", location: {country: "USA", state: "California"} + }); + await db.collection("cities").doc("TO").set({ + name: "Toronto", location: {country: "Canada", province: "Ontario"} + }); + // [END remove_fields_nested_data] + } + + async function removeFieldsNestedExample() { + // [START remove_fields_nested] + const results = await db.pipeline() + .collection("cities") + .removeFields("location.state") + .execute(); + // [END remove_fields_nested] + console.log(results); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/limit + + async function limitSyntaxExample() { + // [START limit_syntax] + const results = await db.pipeline() + .collection("cities") + .limit(10) + .execute(); + // [END limit_syntax] + console.log(results); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/find_nearest + + async function findNearestSyntaxExample() { + // [START find_nearest_syntax] + const results = await db.pipeline() + .collection("cities") + .findNearest({ + field: "embedding", + vectorValue: [1.5, 2.345], + distanceMeasure: "euclidean" + }) + .execute(); + // [END find_nearest_syntax] + } + + async function findNearestLimitExample() { + // [START find_nearest_limit] + const results = await db.pipeline() + .collection("cities") + .findNearest({ + field: "embedding", + vectorValue: [1.5, 2.345], + distanceMeasure: "euclidean", + limit: 10 + }) + .execute(); + // [END find_nearest_limit] + console.log(results); + } + + async function findNearestDistanceDataExample() { + // [START find_nearest_distance_data] + await db.collection("cities").doc("SF").set({name: "San Francisco", embedding: [1.0, -1.0]}); + await db.collection("cities").doc("TO").set({name: "Toronto", embedding: [5.0, -10.0]}); + await db.collection("cities").doc("AT").set({name: "Atlantis", embedding: [2.0, -4.0]}); + // [END find_nearest_distance_data] + } + + async function findNearestDistanceExample() { + // [START find_nearest_distance] + const results = await db.pipeline() + .collection("cities") + .findNearest({ + field: "embedding", + vectorValue: [1.3, 2.345], + distanceMeasure: "euclidean", + distanceField: "computedDistance", + }) + .execute(); + // [END find_nearest_distance] + console.log(results); + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/offset + + async function offsetSyntaxExample() { + // [START offset_syntax] + const results = await db.pipeline() + .collection("cities") + .offset(10) + .execute(); + // [END offset_syntax] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/add_fields + + async function addFieldsSyntaxExample() { + // [START add_fields_syntax] + const results = await db.pipeline() + .collection("users") + .addFields(field("firstName").stringConcat(" ", field("lastName")).as("fullName")) + .execute(); + // [END add_fields_syntax] + } + + async function addFieldsOverlapExample() { + // [START add_fields_overlap] + const results = await db.pipeline() + .collection("users") + .addFields(field("age").abs().as("age")) + .addFields(field("age").add(10).as("age")) + .execute(); + // [END add_fields_overlap] + } + + async function addFieldsNestingExample() { + // [START add_fields_nesting] + const results = await db.pipeline() + .collection("users") + .addFields(field("address.city").toLower().as("address.city")) + .execute(); + // [END add_fields_nesting] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/input/collection + + async function collectionInputSyntaxExample() { + // [START collection_input_syntax] + const results = await db.pipeline() + .collection("cities/SF/departments") + .execute(); + // [END collection_input_syntax] + } + + async function collectionInputExampleData() { + // [START collection_input_data] + await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California"}); + await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"}); + await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"}); + await db.collection("states").doc("CA").set({name: "California"}); + // [END collection_input_data] + } + + async function collectionInputExample() { + // [START collection_input] + const results = await db.pipeline() + .collection("cities") + .sort(field("name").ascending()) + .execute(); + // [END collection_input] + } + + async function subcollectionInputExampleData() { + // [START subcollection_input_data] + await db.collection("cities/SF/departments").doc("building") + .set({name: "SF Building Deparment", employees: 750}); + await db.collection("cities/NY/departments").doc("building") + .set({name: "NY Building Deparment", employees: 1000}); + await db.collection("cities/CHI/departments").doc("building") + .set({name: "CHI Building Deparment", employees: 900}); + await db.collection("cities/NY/departments").doc("finance") + .set({name: "NY Finance Deparment", employees: 1200}); + // [END subcollection_input_data] + } + + async function subcollectionInputExample() { + // [START subcollection_input] + const results = await db.pipeline() + .collection("cities/NY/departments") + .sort(field("employees").ascending()) + .execute(); + // [END subcollection_input] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/input/collection_group + + async function collectionGroupInputSyntaxExample() { + // [START collection_group_input_syntax] + const results = await db.pipeline() + .collectionGroup("departments") + .execute(); + // [END collection_group_input_syntax] + } + + async function collectionGroupInputExampleData() { + // [START collection_group_data] + await db.collection("cities/SF/departments").doc("building").set({name: "SF Building Deparment", employees: 750}); + await db.collection("cities/NY/departments").doc("building").set({name: "NY Building Deparment", employees: 1000}); + await db.collection("cities/CHI/departments").doc("building").set({name: "CHI Building Deparment", employees: 900}); + await db.collection("cities/NY/departments").doc("finance").set({name: "NY Finance Deparment", employees: 1200}); + // [END collection_group_data] + } + + async function collectionGroupInputExample() { + // [START collection_group_input] + const results = await db.pipeline() + .collectionGroup("departments") + .sort(field("employees").ascending()) + .execute(); + // [END collection_group_input] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/input/database + + async function databaseInputSyntaxExample() { + // [START database_syntax] + const results = await db.pipeline() + .database() + .execute(); + // [END database_syntax] + } + + async function databaseInputSyntaxExampleData() { + // [START database_input_data] + await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California", population: 800000}); + await db.collection("states").doc("CA").set({name: "California", population: 39000000}); + await db.collection("countries").doc("USA").set({name: "United States of America", population: 340000000}); + // [END database_input_data] + } + + async function databaseInputExample() { + // [START database_input] + const results = await db.pipeline() + .database() + .sort(field("population").ascending()) + .execute(); + // [END database_input] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/input/documents + + async function documentInputSyntaxExample() { + // [START document_input_syntax] + const results = await db.pipeline() + .documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("NY")]) + .execute(); + // [END document_input_syntax] + } + + async function documentInputExampleData() { + // [START document_input_data] + await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California"}); + await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"}); + await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"}); + // [END document_input_data] + } + + async function documentInputExample() { + // [START document_input] + const results = await db.pipeline() + .documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("NYC")]) + .sort(field("name").ascending()) + .execute(); + // [END document_input] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/union + + async function unionSyntaxExample() { + // [START union_syntax] + const results = await db.pipeline() + .collection("cities/SF/restaurants") + .union(db.pipeline().collection("cities/NYC/restaurants")) + .execute(); + // [END union_syntax] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/aggregate + + async function aggregateSyntaxExample() { + // [START aggregate_syntax] + const cities = await db.pipeline() + .collection("cities") + .aggregate( + countAll().as("total"), + average("population").as("averagePopulation") + ).execute(); + // [END aggregate_syntax] + } + + async function aggregateGroupSyntax() { + // [START aggregate_group_syntax] + const result = await db.pipeline() + .collectionGroup("cities") + .aggregate({ + accumulators: [ + countAll().as("cities"), + field("population").sum().as("totalPopulation") + ], + groups: [field("location.state").as("state")] + }) + .execute(); + // [END aggregate_group_syntax] + } + + async function aggregateExampleData() { + // [START aggregate_data] + await db.collection("cities").doc("SF").set({name: "San Francisco", state: "CA", country: "USA", population: 870000}); + await db.collection("cities").doc("LA").set({name: "Los Angeles", state: "CA", country: "USA", population: 3970000}); + await db.collection("cities").doc("NY").set({name: "New York", state: "NY", country: "USA", population: 8530000}); + await db.collection("cities").doc("TOR").set({name: "Toronto", state: null, country: "Canada", population: 2930000}); + await db.collection("cities").doc("MEX").set({name: "Mexico City", state: null, country: "Mexico", population: 9200000}); + // [END aggregate_data] + } + + async function aggregateWithoutGroupExample() { + // [START aggregate_without_group] + const cities = await db.pipeline() + .collection("cities") + .aggregate( + countAll().as("total"), + average("population").as("averagePopulation") + ).execute(); + // [END aggregate_without_group] + } + + async function aggregateGroupExample() { + // [START aggregate_group_example] + const cities = await db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [ + countAll().as("numberOfCities"), + maximum("population").as("maxPopulation") + ], + groups: ["country", "state"] + }) + .execute(); + // [END aggregate_group_example] + } + + async function aggregateGroupComplexExample() { + // [START aggregate_group_complex] + const cities = await db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [ + sum("population").as("totalPopulation") + ], + groups: [field("state").equal(null).as("stateIsNull")] + }) + .execute(); + // [END aggregate_group_complex] + } + + // https://cloud.google.com/firestore/docs/pipeline/stages/transformation/distinct + + async function distinctSyntaxExample() { + // [START distinct_syntax] + let cities = await db.pipeline() + .collection("cities") + .distinct("country") + .execute(); + + cities = await db.pipeline() + .collection("cities") + .distinct( + field("state").toLower().as("normalizedState"), + field("country")) + .execute(); + // [END distinct_syntax] + } + + async function distinctExampleData() { + // [START distinct_data] + await db.collection("cities").doc("SF").set({name: "San Francisco", state: "CA", country: "USA"}); + await db.collection("cities").doc("LA").set({name: "Los Angeles", state: "CA", country: "USA"}); + await db.collection("cities").doc("NY").set({name: "New York", state: "NY", country: "USA"}); + await db.collection("cities").doc("TOR").set({name: "Toronto", state: null, country: "Canada"}); + await db.collection("cities").doc("MEX").set({name: "Mexico City", state: null, country: "Mexico"}); + // [END distinct_data] + } + + async function distinctExample() { + // [START distinct_example] + const cities = await db.pipeline() + .collection("cities") + .distinct("country") + .execute(); + // [END distinct_example] + } + + async function distinctExpressionsExample() { + // [START distinct_expressions] + const cities = await db.pipeline() + .collection("cities") + .distinct( + field("state").toLower().as("normalizedState"), + field("country")) + .execute(); + // [END distinct_expressions] + } + + // old snippets + + 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([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("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 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 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 = 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] + 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 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] + console.log(results); + } + + async function creatingIndexes() { + // [START query_example] + 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 db.pipeline() + .collection("books") + .where(like(field("category"), "%fantasy%")) + .execute(); + // [END sparse_index_example] + console.log(results); + } + + async function sparseIndexes2() { + // [START sparse_index_example_2] + 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 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] + console.log(results); + } + + async function pagination() { + // [START pagination_not_supported_preview] + // 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] + console.log(q); + console.log(pipeline); + } + + async function collectionStage() { + // [START collection_example] + 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 db.pipeline() + .collectionGroup("games") + .sort(field("name").ascending()) + .execute(); + // [END collection_group_example] + console.log(results); + } + + async function databaseStage() { + // [START database_example] + // Count all documents in the database + 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 db.pipeline() + .documents([ + 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 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] + + // [START full_replace] + const names = await db.pipeline() + .collection("cities") + .replaceWith(field("location")) + .execute(); + // [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 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] + 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 db.pipeline() + .database() + .sample({ percentage: 0.5 }) + .execute(); + // [END sample_percent] + console.log(results); + } + + async function unionStage() { + // [START union_stage] + 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] + console.log(results); + } + + async function unnestStage() { + // [START unnest_stage] + const results = await db.pipeline() + .database() + .unnest(field("arrayField").as("unnestedArrayField"), "index") + .execute(); + // [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 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] + console.log(results); + } + + async function countFunction() { + // [START count_function] + // 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] + console.log(countOfAll); + console.log(countField); + } + + async function countIfFunction() { + // [START count_if] + 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 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 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 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 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 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 db.pipeline() + .collection("books") + .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) + .execute(); + // [END add_function] + console.log(result); + } + + async function subtractFunction() { + // [START subtract_function] + const storeCredit = 7; + 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 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 db.pipeline() + .collection("books") + .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) + .execute(); + // [END divide_function] + console.log(result); + } + + async function modFunction() { + // [START mod_function] + const displayCapacity = 1000; + const result = await db.pipeline() + .collection("books") + .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) + .execute(); + // [END mod_function] + console.log(result); + } + + async function ceilFunction() { + // [START ceil_function] + const booksPerShelf = 100; + 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 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 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 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 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 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 db.pipeline() + .collection("books") + .select(field("rating").ln().as("lnRating")) + .execute(); + // [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 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 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 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 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 db.pipeline() + .collection("books") + .select(field("genre").arrayLength().as("genreCount")) + .execute(); + // [END array_length] + console.log(result); + } + + async function arrayReverseSnippet() { + // [START array_reverse] + const result = await db.pipeline() + .collection("books") + .select(arrayReverse(field("genre")).as("reversedGenres")) + .execute(); + // [END array_reverse] + console.log(result); + } + + async function equalFunction() { + // [START equal_function] + 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 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 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 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 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 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 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 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 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 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 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 db.pipeline() + .collection("books") + .select( + field("tags").arrayConcat([ + 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 db.pipeline() + .collection("books") + .select( + field("name").trim().as("whitespaceTrimmedName") + ) + .execute(); + // [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 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 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 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 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 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 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 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 db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixSeconds().as("unixSeconds") + ) + .execute(); + // [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 db.pipeline() + .collection("books") + .select( + field("embedding").cosineDistance(sampleVector).as("cosineDistance") + ) + .execute(); + // [END cosine_distance] + console.log(result); + } + + async function dotProductFunction() { + // [START dot_product] + 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] + console.log(result); + } + + async function euclideanDistanceFunction() { + // [START euclidean_distance] + 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] + console.log(result); + } + + async function vectorLengthFunction() { + // [START vector_length] + const result = await db.pipeline() + .collection("books") + .select( + field("embedding").vectorLength().as("vectorLength") + ) + .execute(); + // [END vector_length] + console.log(result); + } +}); diff --git a/snippets/firestore-next/test-firestore/add_fields_nesting.js b/snippets/firestore-next/test-firestore/add_fields_nesting.js new file mode 100644 index 00000000..a469113d --- /dev/null +++ b/snippets/firestore-next/test-firestore/add_fields_nesting.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START add_fields_nesting_modular] +const results = await execute(db.pipeline() + .collection("users") + .addFields(field("address.city").toLower().as("address.city"))); +// [END add_fields_nesting_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/add_fields_overlap.js b/snippets/firestore-next/test-firestore/add_fields_overlap.js new file mode 100644 index 00000000..113450b8 --- /dev/null +++ b/snippets/firestore-next/test-firestore/add_fields_overlap.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START add_fields_overlap_modular] +const results = await execute(db.pipeline() + .collection("users") + .addFields(field("age").abs().as("age")) + .addFields(field("age").add(10).as("age"))); +// [END add_fields_overlap_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/add_fields_syntax.js b/snippets/firestore-next/test-firestore/add_fields_syntax.js new file mode 100644 index 00000000..24d3c144 --- /dev/null +++ b/snippets/firestore-next/test-firestore/add_fields_syntax.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START add_fields_syntax_modular] +const results = await execute(db.pipeline() + .collection("users") + .addFields(field("firstName").stringConcat(" ", field("lastName")).as("fullName"))); +// [END add_fields_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/add_function.js b/snippets/firestore-next/test-firestore/add_function.js new file mode 100644 index 00000000..3483c5f3 --- /dev/null +++ b/snippets/firestore-next/test-firestore/add_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) +); +// [END add_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/aggregate_data.js b/snippets/firestore-next/test-firestore/aggregate_data.js new file mode 100644 index 00000000..847c35f4 --- /dev/null +++ b/snippets/firestore-next/test-firestore/aggregate_data.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francisco", state: "CA", country: "USA", population: 870000}); +await setDoc(doc(collection(db, "cities"), "LA"), {name: "Los Angeles", state: "CA", country: "USA", population: 3970000}); +await setDoc(doc(collection(db, "cities"), "NY"), {name: "New York", state: "NY", country: "USA", population: 8530000}); +await setDoc(doc(collection(db, "cities"), "TOR"), {name: "Toronto", state: null, country: "Canada", population: 2930000}); +await setDoc(doc(collection(db, "cities"), "MEX"), {name: "Mexico City", state: null, country: "Mexico", population: 9200000}); +// [END aggregate_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/aggregate_distinct.js b/snippets/firestore-next/test-firestore/aggregate_distinct.js new file mode 100644 index 00000000..27f893a0 --- /dev/null +++ b/snippets/firestore-next/test-firestore/aggregate_distinct.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .distinct( + field("author").toUpper().as("author"), + field("genre") + ) +); +// [END aggregate_distinct_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/aggregate_group_complex.js b/snippets/firestore-next/test-firestore/aggregate_group_complex.js new file mode 100644 index 00000000..4d2d7015 --- /dev/null +++ b/snippets/firestore-next/test-firestore/aggregate_group_complex.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_group_complex_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [ + sum("population").as("totalPopulation") + ], + groups: [field("state").equal(null).as("stateIsNull")] + })); +// [END aggregate_group_complex_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/aggregate_group_example.js b/snippets/firestore-next/test-firestore/aggregate_group_example.js new file mode 100644 index 00000000..cfc2cc75 --- /dev/null +++ b/snippets/firestore-next/test-firestore/aggregate_group_example.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_group_example_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [ + countAll().as("numberOfCities"), + maximum("population").as("maxPopulation") + ], + groups: ["country", "state"] + })); +// [END aggregate_group_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/aggregate_group_syntax.js b/snippets/firestore-next/test-firestore/aggregate_group_syntax.js new file mode 100644 index 00000000..bdb12820 --- /dev/null +++ b/snippets/firestore-next/test-firestore/aggregate_group_syntax.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_group_syntax_modular] +const result = await execute(db.pipeline() + .collectionGroup("cities") + .aggregate({ + accumulators: [ + countAll().as("cities"), + field("population").sum().as("totalPopulation") + ], + groups: [field("location.state").as("state")] + })); +// [END aggregate_group_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/aggregate_groups.js b/snippets/firestore-next/test-firestore/aggregate_groups.js new file mode 100644 index 00000000..6cc5f8bb --- /dev/null +++ b/snippets/firestore-next/test-firestore/aggregate_groups.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .aggregate( + field("rating").average().as("avg_rating") + ) + .distinct(field("genre")) +); +// [END aggregate_groups_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/aggregate_syntax.js b/snippets/firestore-next/test-firestore/aggregate_syntax.js new file mode 100644 index 00000000..2cd15e9a --- /dev/null +++ b/snippets/firestore-next/test-firestore/aggregate_syntax.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_syntax_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .aggregate( + countAll().as("total"), + average("population").as("averagePopulation") + )); +// [END aggregate_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/aggregate_without_group.js b/snippets/firestore-next/test-firestore/aggregate_without_group.js new file mode 100644 index 00000000..9170af7a --- /dev/null +++ b/snippets/firestore-next/test-firestore/aggregate_without_group.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_without_group_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .aggregate( + countAll().as("total"), + average("population").as("averagePopulation") + )); +// [END aggregate_without_group_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/and_function.js b/snippets/firestore-next/test-firestore/and_function.js new file mode 100644 index 00000000..f84865be --- /dev/null +++ b/snippets/firestore-next/test-firestore/and_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + and(field("rating").greaterThan(4), field("price").lessThan(10)) + .as("under10Recommendation") + ) +); +// [END and_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_concat.js b/snippets/firestore-next/test-firestore/array_concat.js new file mode 100644 index 00000000..05941622 --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_concat.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("genre").arrayConcat([field("subGenre")]).as("allGenres")) +); +// [END array_concat_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_contains.js b/snippets/firestore-next/test-firestore/array_contains.js new file mode 100644 index 00000000..5289aabc --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_contains.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) +); +// [END array_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_contains_all.js b/snippets/firestore-next/test-firestore/array_contains_all.js new file mode 100644 index 00000000..63e425a7 --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_contains_all.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAll([constant("fantasy"), constant("adventure")]) + .as("isFantasyAdventure") + ) +); +// [END array_contains_all_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_contains_any.js b/snippets/firestore-next/test-firestore/array_contains_any.js new file mode 100644 index 00000000..88d61b08 --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_contains_any.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) + .as("isMysteryOrFantasy") + ) +); +// [END array_contains_any_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_length.js b/snippets/firestore-next/test-firestore/array_length.js new file mode 100644 index 00000000..89d32f6e --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_length.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("genre").arrayLength().as("genreCount")) +); +// [END array_length_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_reverse.js b/snippets/firestore-next/test-firestore/array_reverse.js new file mode 100644 index 00000000..6f4be979 --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_reverse.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("genre").arrayReverse().as("reversedGenres")) +); +// [END array_reverse_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/avg_function.js b/snippets/firestore-next/test-firestore/avg_function.js new file mode 100644 index 00000000..bc9c9116 --- /dev/null +++ b/snippets/firestore-next/test-firestore/avg_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("cities") + .aggregate(field("population").average().as("averagePopulation")) +); +// [END avg_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/basic_read.js b/snippets/firestore-next/test-firestore/basic_read.js new file mode 100644 index 00000000..2789901c --- /dev/null +++ b/snippets/firestore-next/test-firestore/basic_read.js @@ -0,0 +1,20 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(readDataPipeline); + 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-next/test-firestore/byte_length.js b/snippets/firestore-next/test-firestore/byte_length.js new file mode 100644 index 00000000..12356ff6 --- /dev/null +++ b/snippets/firestore-next/test-firestore/byte_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("title").byteLength().as("titleByteLength") + ) +); +// [END byte_length_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/ceil_function.js b/snippets/firestore-next/test-firestore/ceil_function.js new file mode 100644 index 00000000..569a2a25 --- /dev/null +++ b/snippets/firestore-next/test-firestore/ceil_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("unsoldBooks").divide(constant(booksPerShelf)).ceil().as("requiredShelves") + ) +); +// [END ceil_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/char_length.js b/snippets/firestore-next/test-firestore/char_length.js new file mode 100644 index 00000000..2cf65650 --- /dev/null +++ b/snippets/firestore-next/test-firestore/char_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("title").charLength().as("titleCharLength") + ) +); +// [END char_length_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/collection_example.js b/snippets/firestore-next/test-firestore/collection_example.js new file mode 100644 index 00000000..f969634e --- /dev/null +++ b/snippets/firestore-next/test-firestore/collection_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("users/bob/games") + .sort(field("name").ascending()) + ); +// [END collection_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/collection_group_data.js b/snippets/firestore-next/test-firestore/collection_group_data.js new file mode 100644 index 00000000..d94354cc --- /dev/null +++ b/snippets/firestore-next/test-firestore/collection_group_data.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_group_data_modular] +await setDoc(doc(collection(db, "cities/SF/departments"), "building"), + {name: "SF Building Deparment", employees: 750}); +await setDoc(doc(collection(db, "cities/NY/departments"), "building"), + {name: "NY Building Deparment", employees: 1000}); +await setDoc(doc(collection(db, "cities/CHI/departments"), "building"), + {name: "CHI Building Deparment", employees: 900}); +await setDoc(doc(collection(db, "cities/NY/departments"), "finance"), + {name: "NY Finance Deparment", employees: 1200}); +// [END collection_group_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/collection_group_example.js b/snippets/firestore-next/test-firestore/collection_group_example.js new file mode 100644 index 00000000..d567ba48 --- /dev/null +++ b/snippets/firestore-next/test-firestore/collection_group_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collectionGroup("games") + .sort(field("name").ascending()) + ); +// [END collection_group_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/collection_group_input.js b/snippets/firestore-next/test-firestore/collection_group_input.js new file mode 100644 index 00000000..27508c5f --- /dev/null +++ b/snippets/firestore-next/test-firestore/collection_group_input.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_group_input_modular] +const results = await execute(db.pipeline() + .collectionGroup("departments") + .sort(field("employees").ascending())); +// [END collection_group_input_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/collection_group_input_syntax.js b/snippets/firestore-next/test-firestore/collection_group_input_syntax.js new file mode 100644 index 00000000..918d9ab7 --- /dev/null +++ b/snippets/firestore-next/test-firestore/collection_group_input_syntax.js @@ -0,0 +1,10 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_group_input_syntax_modular] +const results = await execute(db.pipeline() + .collectionGroup("departments")); +// [END collection_group_input_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/collection_input.js b/snippets/firestore-next/test-firestore/collection_input.js new file mode 100644 index 00000000..5c1c0858 --- /dev/null +++ b/snippets/firestore-next/test-firestore/collection_input.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_input_modular] +const results = await execute(db.pipeline() + .collection("cities") + .sort(field("name").ascending())); +// [END collection_input_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/collection_input_data.js b/snippets/firestore-next/test-firestore/collection_input_data.js new file mode 100644 index 00000000..5d07fdd5 --- /dev/null +++ b/snippets/firestore-next/test-firestore/collection_input_data.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_input_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francsico", state: "California"}); +await setDoc(doc(collection(db, "cities"), "NYC"), {name: "New York City", state: "New York"}); +await setDoc(doc(collection(db, "cities"), "CHI"), {name: "Chicago", state: "Illinois"}); +await setDoc(doc(collection(db, "states"), "CA"), {name: "California"}); +// [END collection_input_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/collection_input_syntax.js b/snippets/firestore-next/test-firestore/collection_input_syntax.js new file mode 100644 index 00000000..3ebb1e1a --- /dev/null +++ b/snippets/firestore-next/test-firestore/collection_input_syntax.js @@ -0,0 +1,10 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_input_syntax_modular] +const results = await execute(db.pipeline() + .collection("cities/SF/departments")); +// [END collection_input_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/cond_function.js b/snippets/firestore-next/test-firestore/cond_function.js new file mode 100644 index 00000000..245b94fd --- /dev/null +++ b/snippets/firestore-next/test-firestore/cond_function.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("tags").arrayConcat([ + field("pages").greaterThan(100) + .conditional(constant("longRead"), constant("shortRead")) + ]).as("extendedTags") + ) +); +// [END cond_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/cosine_distance.js b/snippets/firestore-next/test-firestore/cosine_distance.js new file mode 100644 index 00000000..a0ce40fc --- /dev/null +++ b/snippets/firestore-next/test-firestore/cosine_distance.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("embedding").cosineDistance(sampleVector).as("cosineDistance"))); +// [END cosine_distance_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/count_distinct.js b/snippets/firestore-next/test-firestore/count_distinct.js new file mode 100644 index 00000000..8feeb38b --- /dev/null +++ b/snippets/firestore-next/test-firestore/count_distinct.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .aggregate(field("author").countDistinct().as("unique_authors")) +); +// [END count_distinct_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/count_function.js b/snippets/firestore-next/test-firestore/count_function.js new file mode 100644 index 00000000..3a9b8d09 --- /dev/null +++ b/snippets/firestore-next/test-firestore/count_function.js @@ -0,0 +1,19 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/count_if.js b/snippets/firestore-next/test-firestore/count_if.js new file mode 100644 index 00000000..6303b8ec --- /dev/null +++ b/snippets/firestore-next/test-firestore/count_if.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .aggregate( + field("rating").greaterThan(4).countIf().as("filteredCount") + ) +); +// [END count_if_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/covered_query.js b/snippets/firestore-next/test-firestore/covered_query.js new file mode 100644 index 00000000..9f92d282 --- /dev/null +++ b/snippets/firestore-next/test-firestore/covered_query.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/create_where_data.js b/snippets/firestore-next/test-firestore/create_where_data.js new file mode 100644 index 00000000..aef4b389 --- /dev/null +++ b/snippets/firestore-next/test-firestore/create_where_data.js @@ -0,0 +1,24 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START create_where_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), + { name: "San Francisco", state: "CA", country: "USA", population: 870000 } +); + +await setDoc(doc(collection(db, "cities"), "LA"), + { name: "Los Angeles", state: "CA", country: "USA", population: 3970000 } +); +await setDoc(doc(collection(db, "cities"), "NY"), + { name: "New York", state: "NY", country: "USA", population: 8530000 } +); +await setDoc(doc(collection(db, "cities"), "TOR"), + { name: "Toronto", state: null, country: "Canada", population: 2930000 } +); +await setDoc(doc(collection(db, "cities"), "MEX"), + { name: "Mexico City", state: null, country: "Mexico", population: 9200000 } +); +// [END create_where_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/database_example.js b/snippets/firestore-next/test-firestore/database_example.js new file mode 100644 index 00000000..14426381 --- /dev/null +++ b/snippets/firestore-next/test-firestore/database_example.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .database() + .aggregate(countAll().as("total")) + ); +// [END database_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/database_input.js b/snippets/firestore-next/test-firestore/database_input.js new file mode 100644 index 00000000..2b98c2ca --- /dev/null +++ b/snippets/firestore-next/test-firestore/database_input.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START database_input_modular] +const results = await execute(db.pipeline() + .database() + .sort(field("population").ascending())); +// [END database_input_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/database_input_data.js b/snippets/firestore-next/test-firestore/database_input_data.js new file mode 100644 index 00000000..d32bf0a8 --- /dev/null +++ b/snippets/firestore-next/test-firestore/database_input_data.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START database_input_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francsico", state: "California", population: 800000}); +await setDoc(doc(collection(db, "states"), "CA"), {name: "California", population: 39000000}); +await setDoc(doc(collection(db, "countries"), "USA"), {name: "United States of America", population: 340000000}); +// [END database_input_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/database_syntax.js b/snippets/firestore-next/test-firestore/database_syntax.js new file mode 100644 index 00000000..3c35fe39 --- /dev/null +++ b/snippets/firestore-next/test-firestore/database_syntax.js @@ -0,0 +1,10 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START database_syntax_modular] +const results = await execute(db.pipeline() + .database()); +// [END database_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/distinct_data.js b/snippets/firestore-next/test-firestore/distinct_data.js new file mode 100644 index 00000000..d46b700d --- /dev/null +++ b/snippets/firestore-next/test-firestore/distinct_data.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START distinct_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francisco", state: "CA", country: "USA"}); +await setDoc(doc(collection(db, "cities"), "LA"), {name: "Los Angeles", state: "CA", country: "USA"}); +await setDoc(doc(collection(db, "cities"), "NY"), {name: "New York", state: "NY", country: "USA"}); +await setDoc(doc(collection(db, "cities"), "TOR"), {name: "Toronto", state: null, country: "Canada"}); +await setDoc(doc(collection(db, "cities"), "MEX"), {name: "Mexico City", state: null, country: "Mexico"}); +// [END distinct_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/distinct_example.js b/snippets/firestore-next/test-firestore/distinct_example.js new file mode 100644 index 00000000..973144c9 --- /dev/null +++ b/snippets/firestore-next/test-firestore/distinct_example.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START distinct_example_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .distinct("country")); +// [END distinct_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/distinct_expressions.js b/snippets/firestore-next/test-firestore/distinct_expressions.js new file mode 100644 index 00000000..36d68afa --- /dev/null +++ b/snippets/firestore-next/test-firestore/distinct_expressions.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START distinct_expressions_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .distinct( + field("state").toLower().as("normalizedState"), + field("country"))); +// [END distinct_expressions_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/distinct_syntax.js b/snippets/firestore-next/test-firestore/distinct_syntax.js new file mode 100644 index 00000000..1ff1564e --- /dev/null +++ b/snippets/firestore-next/test-firestore/distinct_syntax.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START distinct_syntax_modular] +let cities = await execute(db.pipeline() + .collection("cities") + .distinct("country")); + +cities = await execute(db.pipeline() + .collection("cities") + .distinct( + field("state").toLower().as("normalizedState"), + field("country"))); +// [END distinct_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/divide_function.js b/snippets/firestore-next/test-firestore/divide_function.js new file mode 100644 index 00000000..a4668a58 --- /dev/null +++ b/snippets/firestore-next/test-firestore/divide_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) +); +// [END divide_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/document_input.js b/snippets/firestore-next/test-firestore/document_input.js new file mode 100644 index 00000000..0c5a2b8f --- /dev/null +++ b/snippets/firestore-next/test-firestore/document_input.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START document_input_modular] +const results = await execute(db.pipeline() + .documents([ + doc(collection(db, "cities"), "SF"), + doc(collection(db, "cities"), "NYC")]) + .sort(field("name").ascending())); +// [END document_input_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/document_input_data.js b/snippets/firestore-next/test-firestore/document_input_data.js new file mode 100644 index 00000000..8b7f991c --- /dev/null +++ b/snippets/firestore-next/test-firestore/document_input_data.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START document_input_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francsico", state: "California"}); +await setDoc(doc(collection(db, "cities"), "NYC"), {name: "New York City", state: "New York"}); +await setDoc(doc(collection(db, "cities"), "CHI"), {name: "Chicago", state: "Illinois"}); +// [END document_input_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/document_input_syntax.js b/snippets/firestore-next/test-firestore/document_input_syntax.js new file mode 100644 index 00000000..2f05058a --- /dev/null +++ b/snippets/firestore-next/test-firestore/document_input_syntax.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START document_input_syntax_modular] +const results = await execute(db.pipeline() + .documents([ + doc(collection(db, "cities"), "SF"), + doc(collection(db, "cities"), "NYC")])); +// [END document_input_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/documents_example.js b/snippets/firestore-next/test-firestore/documents_example.js new file mode 100644 index 00000000..83a272a2 --- /dev/null +++ b/snippets/firestore-next/test-firestore/documents_example.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "DC"), + doc(db, "cities", "NY") + ]) +); +// [END documents_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/dot_product.js b/snippets/firestore-next/test-firestore/dot_product.js new file mode 100644 index 00000000..bf694e79 --- /dev/null +++ b/snippets/firestore-next/test-firestore/dot_product.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("embedding").dotProduct(sampleVector).as("dotProduct") + ) +); +// [END dot_product_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/ends_with.js b/snippets/firestore-next/test-firestore/ends_with.js new file mode 100644 index 00000000..98b95e7f --- /dev/null +++ b/snippets/firestore-next/test-firestore/ends_with.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("inventory/devices/laptops") + .select( + field("name").endsWith("16 inch") + .as("16InLaptops") + ) +); +// [END ends_with_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/eq_any.js b/snippets/firestore-next/test-firestore/eq_any.js new file mode 100644 index 00000000..d22004fb --- /dev/null +++ b/snippets/firestore-next/test-firestore/eq_any.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) + .as("matchesGenreFilters") + ) +); +// [END eq_any_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/equal_function.js b/snippets/firestore-next/test-firestore/equal_function.js new file mode 100644 index 00000000..94881396 --- /dev/null +++ b/snippets/firestore-next/test-firestore/equal_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("rating").equal(5).as("hasPerfectRating")) +); +// [END equal_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/euclidean_distance.js b/snippets/firestore-next/test-firestore/euclidean_distance.js new file mode 100644 index 00000000..a1c51f1c --- /dev/null +++ b/snippets/firestore-next/test-firestore/euclidean_distance.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") + ) +); +// [END euclidean_distance_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/exists_function.js b/snippets/firestore-next/test-firestore/exists_function.js new file mode 100644 index 00000000..b903fc68 --- /dev/null +++ b/snippets/firestore-next/test-firestore/exists_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("rating").exists().as("hasRating")) +); +// [END exists_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/exp_function.js b/snippets/firestore-next/test-firestore/exp_function.js new file mode 100644 index 00000000..670e5440 --- /dev/null +++ b/snippets/firestore-next/test-firestore/exp_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("rating").exp().as("expRating")) +); +// [END exp_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/field_or_constant.js b/snippets/firestore-next/test-firestore/field_or_constant.js new file mode 100644 index 00000000..e77c253a --- /dev/null +++ b/snippets/firestore-next/test-firestore/field_or_constant.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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-next/test-firestore/find_nearest_distance.js b/snippets/firestore-next/test-firestore/find_nearest_distance.js new file mode 100644 index 00000000..857b5ef9 --- /dev/null +++ b/snippets/firestore-next/test-firestore/find_nearest_distance.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START find_nearest_distance_modular] +const results = await execute(db.pipeline() +.collection("cities") +.findNearest({ + field: "embedding", + vectorValue: [1.3, 2.345], + distanceMeasure: "euclidean", + distanceField: "computedDistance", +})); +// [END find_nearest_distance_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/find_nearest_distance_data.js b/snippets/firestore-next/test-firestore/find_nearest_distance_data.js new file mode 100644 index 00000000..e2e8ff24 --- /dev/null +++ b/snippets/firestore-next/test-firestore/find_nearest_distance_data.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START find_nearest_distance_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francisco", embedding: [1.0, -1.0]}); +await setDoc(doc(collection(db, "cities"), "TO"), {name: "Toronto", embedding: [5.0, -10.0]}); +await setDoc(doc(collection(db, "cities"), "AT"), {name: "Atlantis", embedding: [2.0, -4.0]}); +// [END find_nearest_distance_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/find_nearest_limit.js b/snippets/firestore-next/test-firestore/find_nearest_limit.js new file mode 100644 index 00000000..66b4cf4a --- /dev/null +++ b/snippets/firestore-next/test-firestore/find_nearest_limit.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START find_nearest_limit_modular] +const results = await execute(db.pipeline() + .collection("cities") + .findNearest({ + field: "embedding", + vectorValue: [1.5, 2.345], + distanceMeasure: "euclidean", + limit: 10 + })); +// [END find_nearest_limit_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/find_nearest_syntax.js b/snippets/firestore-next/test-firestore/find_nearest_syntax.js new file mode 100644 index 00000000..34eb3d52 --- /dev/null +++ b/snippets/firestore-next/test-firestore/find_nearest_syntax.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START find_nearest_syntax_modular] +const results = await execute(db.pipeline() + .collection("cities") + .findNearest({ + field: "embedding", + vectorValue: [1.5, 2.345], + distanceMeasure: "euclidean" + })); +// [END find_nearest_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/floor_function.js b/snippets/firestore-next/test-firestore/floor_function.js new file mode 100644 index 00000000..f5602ea3 --- /dev/null +++ b/snippets/firestore-next/test-firestore/floor_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .addFields( + field("wordCount").divide(field("pages")).floor().as("wordsPerPage") + ) +); +// [END floor_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/full_replace.js b/snippets/firestore-next/test-firestore/full_replace.js new file mode 100644 index 00000000..3befe953 --- /dev/null +++ b/snippets/firestore-next/test-firestore/full_replace.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("cities") + .replaceWith(field("location")) +); +// [END full_replace_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/functions_example.js b/snippets/firestore-next/test-firestore/functions_example.js new file mode 100644 index 00000000..b1a80aeb --- /dev/null +++ b/snippets/firestore-next/test-firestore/functions_example.js @@ -0,0 +1,21 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/greater_or_equal.js b/snippets/firestore-next/test-firestore/greater_or_equal.js new file mode 100644 index 00000000..b4d7534a --- /dev/null +++ b/snippets/firestore-next/test-firestore/greater_or_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")) +); +// [END greater_or_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/greater_than.js b/snippets/firestore-next/test-firestore/greater_than.js new file mode 100644 index 00000000..41d1aa0c --- /dev/null +++ b/snippets/firestore-next/test-firestore/greater_than.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("rating").greaterThan(4).as("hasHighRating")) +); +// [END greater_than_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/initial_data.js b/snippets/firestore-next/test-firestore/initial_data.js new file mode 100644 index 00000000..f47803c5 --- /dev/null +++ b/snippets/firestore-next/test-firestore/initial_data.js @@ -0,0 +1,35 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START initial_data_modular] +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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/input_stages.js b/snippets/firestore-next/test-firestore/input_stages.js new file mode 100644 index 00000000..4b596653 --- /dev/null +++ b/snippets/firestore-next/test-firestore/input_stages.js @@ -0,0 +1,25 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline().collection("cities/sf/restaurants")); + +// Return all restaurants +results = await execute(db.pipeline().collectionGroup("restaurants")); + +// Return all documents across all collections in the database (the entire database) +results = await execute(db.pipeline().database()); + +// Batch read of 3 documents +results = await execute(db.pipeline().documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "DC"), + doc(db, "cities", "NY") +])); +// [END input_stages_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/less_or_equal.js b/snippets/firestore-next/test-firestore/less_or_equal.js new file mode 100644 index 00000000..8bb30179 --- /dev/null +++ b/snippets/firestore-next/test-firestore/less_or_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("rating").lessThanOrEqual(2).as("hasBadRating")) +); +// [END less_or_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/less_than.js b/snippets/firestore-next/test-firestore/less_than.js new file mode 100644 index 00000000..9d104ba6 --- /dev/null +++ b/snippets/firestore-next/test-firestore/less_than.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("published").lessThan(1923).as("isPublicDomainProbably")) +); +// [END less_than_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/like.js b/snippets/firestore-next/test-firestore/like.js new file mode 100644 index 00000000..d7bf9516 --- /dev/null +++ b/snippets/firestore-next/test-firestore/like.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("genre").like("%Fiction") + .as("anyFiction") + ) +); +// [END like_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/limit_syntax.js b/snippets/firestore-next/test-firestore/limit_syntax.js new file mode 100644 index 00000000..a4d69d02 --- /dev/null +++ b/snippets/firestore-next/test-firestore/limit_syntax.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START limit_syntax_modular] +const results = await execute(db.pipeline() + .collection("cities") + .limit(10)); +// [END limit_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/ln_function.js b/snippets/firestore-next/test-firestore/ln_function.js new file mode 100644 index 00000000..049e0bdd --- /dev/null +++ b/snippets/firestore-next/test-firestore/ln_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("rating").ln().as("lnRating")) +); +// [END ln_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/log_function.js b/snippets/firestore-next/test-firestore/log_function.js new file mode 100644 index 00000000..0ef8badd --- /dev/null +++ b/snippets/firestore-next/test-firestore/log_function.js @@ -0,0 +1,9 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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-next/test-firestore/map_get.js b/snippets/firestore-next/test-firestore/map_get.js new file mode 100644 index 00000000..098c1d5b --- /dev/null +++ b/snippets/firestore-next/test-firestore/map_get.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("awards").mapGet("pulitzer").as("hasPulitzerAward") + ) +); +// [END map_get_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/map_merge_overwrite.js b/snippets/firestore-next/test-firestore/map_merge_overwrite.js new file mode 100644 index 00000000..ee6fb613 --- /dev/null +++ b/snippets/firestore-next/test-firestore/map_merge_overwrite.js @@ -0,0 +1,9 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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-next/test-firestore/max_function.js b/snippets/firestore-next/test-firestore/max_function.js new file mode 100644 index 00000000..9ab828e6 --- /dev/null +++ b/snippets/firestore-next/test-firestore/max_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .aggregate(field("price").maximum().as("maximumPrice")) +); +// [END max_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/max_logical_function.js b/snippets/firestore-next/test-firestore/max_logical_function.js new file mode 100644 index 00000000..e884226f --- /dev/null +++ b/snippets/firestore-next/test-firestore/max_logical_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + 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_function.js b/snippets/firestore-next/test-firestore/min_function.js new file mode 100644 index 00000000..9f22c16c --- /dev/null +++ b/snippets/firestore-next/test-firestore/min_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .aggregate(field("price").minimum().as("minimumPrice")) +); +// [END min_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 new file mode 100644 index 00000000..91ae75a1 --- /dev/null +++ b/snippets/firestore-next/test-firestore/min_logical_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + 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/mod_function.js b/snippets/firestore-next/test-firestore/mod_function.js new file mode 100644 index 00000000..16bb7f8b --- /dev/null +++ b/snippets/firestore-next/test-firestore/mod_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) +); +// [END mod_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/multiply_function.js b/snippets/firestore-next/test-firestore/multiply_function.js new file mode 100644 index 00000000..d89430ff --- /dev/null +++ b/snippets/firestore-next/test-firestore/multiply_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("price").multiply(field("soldBooks")).as("revenue")) +); +// [END multiply_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/not_eq_any.js b/snippets/firestore-next/test-firestore/not_eq_any.js new file mode 100644 index 00000000..5b0c4149 --- /dev/null +++ b/snippets/firestore-next/test-firestore/not_eq_any.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) + .as("byExcludedAuthors") + ) +); +// [END not_eq_any_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/not_equal.js b/snippets/firestore-next/test-firestore/not_equal.js new file mode 100644 index 00000000..c8acc8d3 --- /dev/null +++ b/snippets/firestore-next/test-firestore/not_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("title").notEqual("1984").as("not1984")) +); +// [END not_equal_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 new file mode 100644 index 00000000..f6905f23 --- /dev/null +++ b/snippets/firestore-next/test-firestore/not_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("tags").arrayContains("nonfiction").not() + .as("isFiction") + ) +); +// [END not_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/offset_syntax.js b/snippets/firestore-next/test-firestore/offset_syntax.js new file mode 100644 index 00000000..fd7b8dc8 --- /dev/null +++ b/snippets/firestore-next/test-firestore/offset_syntax.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START offset_syntax_modular] +const results = await execute(db.pipeline() + .collection("cities") + .offset(10)); +// [END offset_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/or_function.js b/snippets/firestore-next/test-firestore/or_function.js new file mode 100644 index 00000000..035512d7 --- /dev/null +++ b/snippets/firestore-next/test-firestore/or_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) + .as("matchesSearchFilters") + ) +); +// [END or_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/pagination_not_supported_preview.js b/snippets/firestore-next/test-firestore/pagination_not_supported_preview.js new file mode 100644 index 00000000..02687926 --- /dev/null +++ b/snippets/firestore-next/test-firestore/pagination_not_supported_preview.js @@ -0,0 +1,39 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 = + 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/pipeline_concepts.js b/snippets/firestore-next/test-firestore/pipeline_concepts.js new file mode 100644 index 00000000..e93b2d9f --- /dev/null +++ b/snippets/firestore-next/test-firestore/pipeline_concepts.js @@ -0,0 +1,18 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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-next/test-firestore/pipeline_initialization.js b/snippets/firestore-next/test-firestore/pipeline_initialization.js new file mode 100644 index 00000000..d9b20bdf --- /dev/null +++ b/snippets/firestore-next/test-firestore/pipeline_initialization.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pipeline_initialization_modular] +import { getFirestore } from "firebase/firestore"; +import { execute } from "firebase/firestore/pipelines"; +const database = getFirestore(app, "enterprise"); +const pipeline = database.pipeline(); +// [END pipeline_initialization_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 new file mode 100644 index 00000000..7e7bd55e --- /dev/null +++ b/snippets/firestore-next/test-firestore/pipeline_where.js @@ -0,0 +1,18 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline().collection("books") + .where(field("rating").equal(5)) + .where(field("published").lessThan(1900)) +); + +results = await execute(db.pipeline().collection("books") + .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/pow_function.js b/snippets/firestore-next/test-firestore/pow_function.js new file mode 100644 index 00000000..1e45eea2 --- /dev/null +++ b/snippets/firestore-next/test-firestore/pow_function.js @@ -0,0 +1,27 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/query_example.js b/snippets/firestore-next/test-firestore/query_example.js new file mode 100644 index 00000000..0a47a09e --- /dev/null +++ b/snippets/firestore-next/test-firestore/query_example.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/regex_contains.js b/snippets/firestore-next/test-firestore/regex_contains.js new file mode 100644 index 00000000..22edc7e9 --- /dev/null +++ b/snippets/firestore-next/test-firestore/regex_contains.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("documents") + .select( + field("title").regexContains("Firestore (Enterprise|Standard)") + .as("isFirestoreRelated") + ) +); +// [END regex_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/regex_match.js b/snippets/firestore-next/test-firestore/regex_match.js new file mode 100644 index 00000000..84d1d77b --- /dev/null +++ b/snippets/firestore-next/test-firestore/regex_match.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("documents") + .select( + field("title").regexMatch("Firestore (Enterprise|Standard)") + .as("isFirestoreExactly") + ) +); +// [END regex_match_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/remove_fields_nested.js b/snippets/firestore-next/test-firestore/remove_fields_nested.js new file mode 100644 index 00000000..31982642 --- /dev/null +++ b/snippets/firestore-next/test-firestore/remove_fields_nested.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START remove_fields_nested_modular] +const results = await execute(db.pipeline() + .collection("cities") + .removeFields("location.state")); +// [END remove_fields_nested_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/remove_fields_nested_data.js b/snippets/firestore-next/test-firestore/remove_fields_nested_data.js new file mode 100644 index 00000000..791776a7 --- /dev/null +++ b/snippets/firestore-next/test-firestore/remove_fields_nested_data.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START remove_fields_nested_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), { + name: "San Francisco", location: {country: "USA", state: "California"} +}); +await setDoc(doc(collection(db, "cities"), "TO"), { + name: "Toronto", location: {country: "Canada", province: "Ontario"} +}); +// [END remove_fields_nested_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/remove_fields_syntax.js b/snippets/firestore-next/test-firestore/remove_fields_syntax.js new file mode 100644 index 00000000..4e9ffdc1 --- /dev/null +++ b/snippets/firestore-next/test-firestore/remove_fields_syntax.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START remove_fields_syntax_modular] +const results = await execute(db.pipeline() + .collection("cities") + .removeFields("population", "location.state")); +// [END remove_fields_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/round_function.js b/snippets/firestore-next/test-firestore/round_function.js new file mode 100644 index 00000000..94d85b63 --- /dev/null +++ b/snippets/firestore-next/test-firestore/round_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) + .aggregate(field("partialRevenue").sum().as("totalRevenue")) + ); +// [END round_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sample_all_documents.js b/snippets/firestore-next/test-firestore/sample_all_documents.js new file mode 100644 index 00000000..2268acda --- /dev/null +++ b/snippets/firestore-next/test-firestore/sample_all_documents.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_all_documents_modular] +const sampled = await execute(db.pipeline() + .collection("cities") + .sample(5)); +// [END sample_all_documents_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sample_documents.js b/snippets/firestore-next/test-firestore/sample_documents.js new file mode 100644 index 00000000..5ff0fe9f --- /dev/null +++ b/snippets/firestore-next/test-firestore/sample_documents.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_documents_modular] +const sampled = await execute(db.pipeline() + .collection("cities") + .sample(1)); +// [END sample_documents_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sample_documents_data.js b/snippets/firestore-next/test-firestore/sample_documents_data.js new file mode 100644 index 00000000..47eaff66 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sample_documents_data.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_documents_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francisco", state: "California"}); +await setDoc(doc(collection(db, "cities"), "NYC"), {name: "New York City", state: "New York"}); +await setDoc(doc(collection(db, "cities"), "CHI"), {name: "Chicago", state: "Illinois"}); +// [END sample_documents_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sample_example.js b/snippets/firestore-next/test-firestore/sample_example.js new file mode 100644 index 00000000..47873e55 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sample_example.js @@ -0,0 +1,25 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sample_percent.js b/snippets/firestore-next/test-firestore/sample_percent.js new file mode 100644 index 00000000..1c105a5f --- /dev/null +++ b/snippets/firestore-next/test-firestore/sample_percent.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .database() + .sample({ percentage: 0.5 }) +); +// [END sample_percent_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sample_percentage.js b/snippets/firestore-next/test-firestore/sample_percentage.js new file mode 100644 index 00000000..f766db87 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sample_percentage.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_percentage_modular] +const sampled = await execute(db.pipeline() + .collection("cities") + .sample({ percentage: 0.5 })); +// [END sample_percentage_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sample_percentage_data.js b/snippets/firestore-next/test-firestore/sample_percentage_data.js new file mode 100644 index 00000000..d3c24d5b --- /dev/null +++ b/snippets/firestore-next/test-firestore/sample_percentage_data.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_percentage_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francsico", state: "California"}); +await setDoc(doc(collection(db, "cities"), "NYC"), {name: "New York City", state: "New York"}); +await setDoc(doc(collection(db, "cities"), "CHI"), {name: "Chicago", state: "Illinois"}); +await setDoc(doc(collection(db, "cities"), "ATL"), {name: "Atlanta", state: "Georgia"}); +// [END sample_percentage_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sample_syntax.js b/snippets/firestore-next/test-firestore/sample_syntax.js new file mode 100644 index 00000000..b64136da --- /dev/null +++ b/snippets/firestore-next/test-firestore/sample_syntax.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_syntax_modular] +let sampled = await execute(db.pipeline() + .database() + .sample(50)); + +sampled = await execute(db.pipeline() + .database() + .sample({ percentage: 0.5 })); +// [END sample_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/select_bad_position.js b/snippets/firestore-next/test-firestore/select_bad_position.js new file mode 100644 index 00000000..c6bdaaab --- /dev/null +++ b/snippets/firestore-next/test-firestore/select_bad_position.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START select_bad_position_modular] +const names = await execute(db.pipeline() + .collection("cities") + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population") + .where(field("location.country").equal("Canada"))); +// [END select_bad_position_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/select_nested.js b/snippets/firestore-next/test-firestore/select_nested.js new file mode 100644 index 00000000..90fea9e6 --- /dev/null +++ b/snippets/firestore-next/test-firestore/select_nested.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START select_nested_modular] +const locations = await execute(db.pipeline() + .collection("cities") + .select( + field("name").as("city"), + field("location.country").as("country"), + field("landmarks").arrayGet(0).as("topLandmark") + )); +// [END select_nested_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/select_nested_data.js b/snippets/firestore-next/test-firestore/select_nested_data.js new file mode 100644 index 00000000..8d904af2 --- /dev/null +++ b/snippets/firestore-next/test-firestore/select_nested_data.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START select_nested_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), {name: "San Francisco", population: 800000, location: {country: "USA", state: "California"}, landmarks: ["Golden Gate Bridge", "Alcatraz"]}); +await setDoc(doc(collection(db, "cities"), "TO"), {name: "Toronto", population: 3000000, province: "ON", location: {country: "Canada", province: "Ontario"}, landmarks: ["CN Tower", "Casa Loma"]}); +await setDoc(doc(collection(db, "cities"), "AT"), {name: "Atlantis", population: null}); +// [END select_nested_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/select_position.js b/snippets/firestore-next/test-firestore/select_position.js new file mode 100644 index 00000000..964bec14 --- /dev/null +++ b/snippets/firestore-next/test-firestore/select_position.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START select_position_modular] +const names = await execute(db.pipeline() + .collection("cities") + .where(field("location.country").equal("Canada")) + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population")); +// [END select_position_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/select_position_data.js b/snippets/firestore-next/test-firestore/select_position_data.js new file mode 100644 index 00000000..cef93d86 --- /dev/null +++ b/snippets/firestore-next/test-firestore/select_position_data.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START select_position_data_modular] +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, location: {country: "Canada", province: "Ontario"} +}); +// [END select_position_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/select_syntax.js b/snippets/firestore-next/test-firestore/select_syntax.js new file mode 100644 index 00000000..c68cdac9 --- /dev/null +++ b/snippets/firestore-next/test-firestore/select_syntax.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START select_syntax_modular] +const names = await execute(db.pipeline() + .collection("cities") + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population" + )); +// [END select_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sort.js b/snippets/firestore-next/test-firestore/sort.js new file mode 100644 index 00000000..87326619 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sort.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ) +); +// [END sort_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sort_comparison.js b/snippets/firestore-next/test-firestore/sort_comparison.js new file mode 100644 index 00000000..9d0ff01e --- /dev/null +++ b/snippets/firestore-next/test-firestore/sort_comparison.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 = 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sort_document_id.js b/snippets/firestore-next/test-firestore/sort_document_id.js new file mode 100644 index 00000000..bec8f754 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sort_document_id.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sort_document_id_modular] +const results = await execute(db.pipeline() + .collection("cities") + .sort(field("country").ascending(), field("__name__").ascending())); +// [END sort_document_id_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sort_syntax.js b/snippets/firestore-next/test-firestore/sort_syntax.js new file mode 100644 index 00000000..5e012238 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sort_syntax.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sort_syntax_modular] +const results = await execute(db.pipeline() + .collection("cities") + .sort(field("population").ascending())); +// [END sort_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sparse_index_example.js b/snippets/firestore-next/test-firestore/sparse_index_example.js new file mode 100644 index 00000000..420cf8e1 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sparse_index_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .where(field("category").like("%fantasy%")) +); +// [END sparse_index_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sqrt_function.js b/snippets/firestore-next/test-firestore/sqrt_function.js new file mode 100644 index 00000000..15af33e5 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sqrt_function.js @@ -0,0 +1,27 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/stages_expressions_example.js b/snippets/firestore-next/test-firestore/stages_expressions_example.js new file mode 100644 index 00000000..ad5a259c --- /dev/null +++ b/snippets/firestore-next/test-firestore/stages_expressions_example.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START stages_expressions_example_modular] +const trailing30Days = constant(Timestamp.now().toMillis()) + .unixMillisToTimestamp() + .timestampSubtract("day", 30); +const snapshot = await execute(db.pipeline() + .collection("productViews") + .where(field("viewedAt").greaterThan(trailing30Days)) + .aggregate(field("productId").countDistinct().as("uniqueProductViews")) +); +// [END stages_expressions_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/starts_with.js b/snippets/firestore-next/test-firestore/starts_with.js new file mode 100644 index 00000000..04236279 --- /dev/null +++ b/snippets/firestore-next/test-firestore/starts_with.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("title").startsWith("The") + .as("needsSpecialAlphabeticalSort") + ) +); +// [END starts_with_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/str_concat.js b/snippets/firestore-next/test-firestore/str_concat.js new file mode 100644 index 00000000..f1d219da --- /dev/null +++ b/snippets/firestore-next/test-firestore/str_concat.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("title").stringConcat(" by ", field("author")) + .as("fullyQualifiedTitle") + ) +); +// [END str_concat_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/str_reverse.js b/snippets/firestore-next/test-firestore/str_reverse.js new file mode 100644 index 00000000..752a8f86 --- /dev/null +++ b/snippets/firestore-next/test-firestore/str_reverse.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("name").reverse().as("reversedName") + ) +); +// [END str_reverse_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/string_contains.js b/snippets/firestore-next/test-firestore/string_contains.js new file mode 100644 index 00000000..d8140aaa --- /dev/null +++ b/snippets/firestore-next/test-firestore/string_contains.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("articles") + .select( + field("body").stringContains("Firestore") + .as("isFirestoreRelated") + ) +); +// [END string_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/subcollection_input.js b/snippets/firestore-next/test-firestore/subcollection_input.js new file mode 100644 index 00000000..5cd1ecd3 --- /dev/null +++ b/snippets/firestore-next/test-firestore/subcollection_input.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START subcollection_input_modular] +const results = await execute(db.pipeline() + .collection("cities/NY/departments") + .sort(field("employees").ascending())); +// [END subcollection_input_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/subcollection_input_data.js b/snippets/firestore-next/test-firestore/subcollection_input_data.js new file mode 100644 index 00000000..708ff307 --- /dev/null +++ b/snippets/firestore-next/test-firestore/subcollection_input_data.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START subcollection_input_data_modular] +await setDoc(doc(collection(db, "cities/SF/departments"), "building"), + {name: "SF Building Deparment", employees: 750}); +await setDoc(doc(collection(db, "cities/NY/departments"), "building"), + {name: "NY Building Deparment", employees: 1000}); +await setDoc(doc(collection(db, "cities/CHI/departments"), "building"), + {name: "CHI Building Deparment", employees: 900}); +await setDoc(doc(collection(db, "cities/NY/departments"), "finance"), + {name: "NY Finance Deparment", employees: 1200}); +// [END subcollection_input_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/substr_function.js b/snippets/firestore-next/test-firestore/substr_function.js new file mode 100644 index 00000000..0b1733e2 --- /dev/null +++ b/snippets/firestore-next/test-firestore/substr_function.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .where(field("title").startsWith("The ")) + .select( + field("title").substring(4) + .as("titleWithoutLeadingThe") + ) +); +// [END substr_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/subtract_function.js b/snippets/firestore-next/test-firestore/subtract_function.js new file mode 100644 index 00000000..53a73a66 --- /dev/null +++ b/snippets/firestore-next/test-firestore/subtract_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select(field("price").subtract(constant(storeCredit)).as("totalCost")) +); +// [END subtract_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sum_function.js b/snippets/firestore-next/test-firestore/sum_function.js new file mode 100644 index 00000000..a77a5003 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sum_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("cities") + .aggregate(field("population").sum().as("totalPopulation")) +); +// [END sum_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/timestamp_add.js b/snippets/firestore-next/test-firestore/timestamp_add.js new file mode 100644 index 00000000..4dbef032 --- /dev/null +++ b/snippets/firestore-next/test-firestore/timestamp_add.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("documents") + .select( + field("createdAt").timestampAdd("day", 3653).as("expiresAt") + ) +); +// [END timestamp_add_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/timestamp_sub.js b/snippets/firestore-next/test-firestore/timestamp_sub.js new file mode 100644 index 00000000..459e0263 --- /dev/null +++ b/snippets/firestore-next/test-firestore/timestamp_sub.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("documents") + .select( + field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") + ) +); +// [END timestamp_sub_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/timestamp_unix_micros.js b/snippets/firestore-next/test-firestore/timestamp_unix_micros.js new file mode 100644 index 00000000..48862d27 --- /dev/null +++ b/snippets/firestore-next/test-firestore/timestamp_unix_micros.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMicros().as("unixMicros") + ) +); +// [END timestamp_unix_micros_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/timestamp_unix_millis.js b/snippets/firestore-next/test-firestore/timestamp_unix_millis.js new file mode 100644 index 00000000..ca8dbcf2 --- /dev/null +++ b/snippets/firestore-next/test-firestore/timestamp_unix_millis.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMillis().as("unixMillis") + ) +); +// [END timestamp_unix_millis_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/timestamp_unix_seconds.js b/snippets/firestore-next/test-firestore/timestamp_unix_seconds.js new file mode 100644 index 00000000..dfcc7423 --- /dev/null +++ b/snippets/firestore-next/test-firestore/timestamp_unix_seconds.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixSeconds().as("unixSeconds") + ) +); +// [END timestamp_unix_seconds_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/to_lower.js b/snippets/firestore-next/test-firestore/to_lower.js new file mode 100644 index 00000000..64f001e1 --- /dev/null +++ b/snippets/firestore-next/test-firestore/to_lower.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("authors") + .select( + field("genre").toLower().equal("fantasy") + .as("isFantasy") + ) +); +// [END to_lower_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/to_upper.js b/snippets/firestore-next/test-firestore/to_upper.js new file mode 100644 index 00000000..468f00d1 --- /dev/null +++ b/snippets/firestore-next/test-firestore/to_upper.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("authors") + .select( + field("name").toUpper() + .as("uppercaseName") + ) +); +// [END to_upper_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/trim_function.js b/snippets/firestore-next/test-firestore/trim_function.js new file mode 100644 index 00000000..64153138 --- /dev/null +++ b/snippets/firestore-next/test-firestore/trim_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("name").trim().as("whitespaceTrimmedName") + ) +); +// [END trim_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/union_stage.js b/snippets/firestore-next/test-firestore/union_stage.js new file mode 100644 index 00000000..db549a70 --- /dev/null +++ b/snippets/firestore-next/test-firestore/union_stage.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/union_syntax.js b/snippets/firestore-next/test-firestore/union_syntax.js new file mode 100644 index 00000000..e21e8291 --- /dev/null +++ b/snippets/firestore-next/test-firestore/union_syntax.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START union_syntax_modular] +const results = await execute(db.pipeline() + .collection("cities/SF/restaurants") + .union(db.pipeline().collection("cities/NYC/restaurants"))); +// [END union_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unix_micros_timestamp.js b/snippets/firestore-next/test-firestore/unix_micros_timestamp.js new file mode 100644 index 00000000..4fd69202 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unix_micros_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("documents") + .select( + field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") + ) +); +// [END unix_micros_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unix_millis_timestamp.js b/snippets/firestore-next/test-firestore/unix_millis_timestamp.js new file mode 100644 index 00000000..87f547fa --- /dev/null +++ b/snippets/firestore-next/test-firestore/unix_millis_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("documents") + .select( + field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") + ) +); +// [END unix_millis_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unix_seconds_timestamp.js b/snippets/firestore-next/test-firestore/unix_seconds_timestamp.js new file mode 100644 index 00000000..84b25c30 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unix_seconds_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("documents") + .select( + field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") + ) +); +// [END unix_seconds_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_alias_index.js b/snippets/firestore-next/test-firestore/unnest_alias_index.js new file mode 100644 index 00000000..f0369e65 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_alias_index.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_alias_index_modular] +const userScore = await execute(db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt")); +// [END unnest_alias_index_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_alias_index_data.js b/snippets/firestore-next/test-firestore/unnest_alias_index_data.js new file mode 100644 index 00000000..136ed92d --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_alias_index_data.js @@ -0,0 +1,10 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_alias_index_data_modular] +await addDoc(collection(db, "users"), {name: "foo", scores: [5, 4], userScore: 0}); +await addDoc(collection(db, "users"), {name: "bar", scores: [1, 3], attempt: 5}); +// [END unnest_alias_index_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_edge_cases.js b/snippets/firestore-next/test-firestore/unnest_edge_cases.js new file mode 100644 index 00000000..72d6bbce --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_edge_cases.js @@ -0,0 +1,21 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 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_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_empty_array.js b/snippets/firestore-next/test-firestore/unnest_empty_array.js new file mode 100644 index 00000000..b27566ec --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_empty_array.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_empty_array_modular] +const userScore = await execute(db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt")); +// [END unnest_empty_array_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_empty_array_data.js b/snippets/firestore-next/test-firestore/unnest_empty_array_data.js new file mode 100644 index 00000000..a679da6f --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_empty_array_data.js @@ -0,0 +1,10 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_empty_array_data_modular] +await addDoc(collection(db, "users"), {name: "foo", scores: [5, 4]}); +await addDoc(collection(db, "users"), {name: "bar", scores: []}); +// [END unnest_empty_array_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_nested.js b/snippets/firestore-next/test-firestore/unnest_nested.js new file mode 100644 index 00000000..c5649590 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_nested.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_nested_modular] +const userScore = await execute(db.pipeline() + .collection("users") + .unnest(field("record").as("record")) + .unnest(field("record.scores").as("userScore"), /* index_field= */ "attempt")); +// [END unnest_nested_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_nested_data.js b/snippets/firestore-next/test-firestore/unnest_nested_data.js new file mode 100644 index 00000000..6b499ed7 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_nested_data.js @@ -0,0 +1,20 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_nested_data_modular] +await addDoc(collection(db, "users"), { + name: "foo", + record: [ + { + scores: [5, 4], + avg: 4.5 + }, { + scores: [1, 3], + old_avg: 2 + } + ] +}); +// [END unnest_nested_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_nonarray.js b/snippets/firestore-next/test-firestore/unnest_nonarray.js new file mode 100644 index 00000000..e9a548f0 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_nonarray.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_nonarray_modular] +const userScore = await execute(db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt")); +// [END unnest_nonarray_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_nonarray_data.js b/snippets/firestore-next/test-firestore/unnest_nonarray_data.js new file mode 100644 index 00000000..4e9f9e16 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_nonarray_data.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_nonarray_data_modular] + await addDoc(collection(db, "users"), {name: "foo", scores: 1}); + await addDoc(collection(db, "users"), {name: "bar", scores: null}); + await addDoc(collection(db, "users"), {name: "qux", scores: {backupScores: 1}}); +// [END unnest_nonarray_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_preserve_empty_array.js b/snippets/firestore-next/test-firestore/unnest_preserve_empty_array.js new file mode 100644 index 00000000..412a9003 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_preserve_empty_array.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_preserve_empty_array_modular] +const userScore = await execute(db.pipeline() + .collection("users") + .unnest( + conditional( + field("scores").equal(array([])), + array([field("scores")]), + field("scores") + ).as("userScore"), + /* index_field= */ "attempt")); +// [END unnest_preserve_empty_array_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_stage.js b/snippets/firestore-next/test-firestore/unnest_stage.js new file mode 100644 index 00000000..ded031b6 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_stage.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .database() + .unnest(field("arrayField").as("unnestedArrayField"), "index") +); +// [END unnest_stage_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_syntax.js b/snippets/firestore-next/test-firestore/unnest_syntax.js new file mode 100644 index 00000000..544b1cc6 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_syntax.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_syntax_modular] +const userScore = await execute(db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt")); +// [END unnest_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/vector_length.js b/snippets/firestore-next/test-firestore/vector_length.js new file mode 100644 index 00000000..5eb0bf0e --- /dev/null +++ b/snippets/firestore-next/test-firestore/vector_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + field("embedding").vectorLength().as("vectorLength") + ) +); +// [END vector_length_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/where_complex.js b/snippets/firestore-next/test-firestore/where_complex.js new file mode 100644 index 00000000..eb9ff5bd --- /dev/null +++ b/snippets/firestore-next/test-firestore/where_complex.js @@ -0,0 +1,19 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START where_complex_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .where( + or( + like(field("name"), "San%"), + and( + field("location.state").charLength().greaterThan(7), + field("location.country").equal("USA") + ) + ) + )); +// [END where_complex_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/where_equality_example.js b/snippets/firestore-next/test-firestore/where_equality_example.js new file mode 100644 index 00000000..2bcce94e --- /dev/null +++ b/snippets/firestore-next/test-firestore/where_equality_example.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START where_equality_example_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .where(field("state").equal("CA"))); +// [END where_equality_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/where_having_example.js b/snippets/firestore-next/test-firestore/where_having_example.js new file mode 100644 index 00000000..8d7d4363 --- /dev/null +++ b/snippets/firestore-next/test-firestore/where_having_example.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START where_having_example_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [field("population").sum().as("totalPopulation")], + groups: ["location.state"] + }) + .where(field("totalPopulation").greaterThan(10000000))); +// [END where_having_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/where_multiple_stages.js b/snippets/firestore-next/test-firestore/where_multiple_stages.js new file mode 100644 index 00000000..5a15288d --- /dev/null +++ b/snippets/firestore-next/test-firestore/where_multiple_stages.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START where_multiple_stages_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .where(field("location.country").equal("USA")) + .where(field("population").greaterThan(500000))); +// [END where_multiple_stages_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/where_stage_order.js b/snippets/firestore-next/test-firestore/where_stage_order.js new file mode 100644 index 00000000..0f3de3e2 --- /dev/null +++ b/snippets/firestore-next/test-firestore/where_stage_order.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START where_stage_order_modular] +const cities = await execute(db.pipeline() + .collection("cities") + .limit(10) + .where(field("location.country").equal("USA"))); +// [END where_stage_order_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 new file mode 100644 index 00000000..f4523929 --- /dev/null +++ b/snippets/firestore-next/test-firestore/xor_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/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 execute(db.pipeline() + .collection("books") + .select( + xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) + .as("matchesSearchFilters") + ) +); +// [END xor_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/add_fields_nesting.js b/snippets/firestore-temp/test-firestore/add_fields_nesting.js new file mode 100644 index 00000000..f99caa1c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/add_fields_nesting.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_fields_nesting_modular] +const results = await db.pipeline() + .collection("users") + .addFields(field("address.city").toLower().as("address.city")) + .execute(); +// [END add_fields_nesting_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/add_fields_overlap.js b/snippets/firestore-temp/test-firestore/add_fields_overlap.js new file mode 100644 index 00000000..ca2d8371 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/add_fields_overlap.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 add_fields_overlap_modular] +const results = await db.pipeline() + .collection("users") + .addFields(field("age").abs().as("age")) + .addFields(field("age").add(10).as("age")) + .execute(); +// [END add_fields_overlap_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/add_fields_syntax.js b/snippets/firestore-temp/test-firestore/add_fields_syntax.js new file mode 100644 index 00000000..da51f603 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/add_fields_syntax.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_fields_syntax_modular] +const results = await db.pipeline() + .collection("users") + .addFields(field("firstName").stringConcat(" ", field("lastName")).as("fullName")) + .execute(); +// [END add_fields_syntax_modular] \ No newline at end of file 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_data.js b/snippets/firestore-temp/test-firestore/aggregate_data.js new file mode 100644 index 00000000..c69a3419 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/aggregate_data.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 aggregate_data_modular] +await db.collection("cities").doc("SF").set({name: "San Francisco", state: "CA", country: "USA", population: 870000}); +await db.collection("cities").doc("LA").set({name: "Los Angeles", state: "CA", country: "USA", population: 3970000}); +await db.collection("cities").doc("NY").set({name: "New York", state: "NY", country: "USA", population: 8530000}); +await db.collection("cities").doc("TOR").set({name: "Toronto", state: null, country: "Canada", population: 2930000}); +await db.collection("cities").doc("MEX").set({name: "Mexico City", state: null, country: "Mexico", population: 9200000}); +// [END aggregate_data_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_group_complex.js b/snippets/firestore-temp/test-firestore/aggregate_group_complex.js new file mode 100644 index 00000000..2bce3c17 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/aggregate_group_complex.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 aggregate_group_complex_modular] +const cities = await db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [ + sum("population").as("totalPopulation") + ], + groups: [field("state").equal(null).as("stateIsNull")] + }) + .execute(); +// [END aggregate_group_complex_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/aggregate_group_example.js b/snippets/firestore-temp/test-firestore/aggregate_group_example.js new file mode 100644 index 00000000..7472c323 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/aggregate_group_example.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 aggregate_group_example_modular] +const cities = await db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [ + countAll().as("numberOfCities"), + maximum("population").as("maxPopulation") + ], + groups: ["country", "state"] + }) + .execute(); +// [END aggregate_group_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/aggregate_group_syntax.js b/snippets/firestore-temp/test-firestore/aggregate_group_syntax.js new file mode 100644 index 00000000..d90114aa --- /dev/null +++ b/snippets/firestore-temp/test-firestore/aggregate_group_syntax.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 aggregate_group_syntax_modular] +const result = await db.pipeline() + .collectionGroup("cities") + .aggregate({ + accumulators: [ + countAll().as("cities"), + field("population").sum().as("totalPopulation") + ], + groups: [field("location.state").as("state")] + }) + .execute(); +// [END aggregate_group_syntax_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/aggregate_syntax.js b/snippets/firestore-temp/test-firestore/aggregate_syntax.js new file mode 100644 index 00000000..ef0b2b75 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/aggregate_syntax.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 aggregate_syntax_modular] +const cities = await db.pipeline() + .collection("cities") + .aggregate( + countAll().as("total"), + average("population").as("averagePopulation") + ).execute(); +// [END aggregate_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/aggregate_without_group.js b/snippets/firestore-temp/test-firestore/aggregate_without_group.js new file mode 100644 index 00000000..8163ebbf --- /dev/null +++ b/snippets/firestore-temp/test-firestore/aggregate_without_group.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 aggregate_without_group_modular] +const cities = await db.pipeline() + .collection("cities") + .aggregate( + countAll().as("total"), + average("population").as("averagePopulation") + ).execute(); +// [END aggregate_without_group_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_data.js b/snippets/firestore-temp/test-firestore/collection_group_data.js new file mode 100644 index 00000000..8cb33872 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/collection_group_data.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_data_modular] +await db.collection("cities/SF/departments").doc("building").set({name: "SF Building Deparment", employees: 750}); +await db.collection("cities/NY/departments").doc("building").set({name: "NY Building Deparment", employees: 1000}); +await db.collection("cities/CHI/departments").doc("building").set({name: "CHI Building Deparment", employees: 900}); +await db.collection("cities/NY/departments").doc("finance").set({name: "NY Finance Deparment", employees: 1200}); +// [END collection_group_data_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/collection_group_input.js b/snippets/firestore-temp/test-firestore/collection_group_input.js new file mode 100644 index 00000000..02f3c396 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/collection_group_input.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_input_modular] +const results = await db.pipeline() + .collectionGroup("departments") + .sort(field("employees").ascending()) + .execute(); +// [END collection_group_input_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/collection_group_input_syntax.js b/snippets/firestore-temp/test-firestore/collection_group_input_syntax.js new file mode 100644 index 00000000..5d641f56 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/collection_group_input_syntax.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 collection_group_input_syntax_modular] +const results = await db.pipeline() + .collectionGroup("departments") + .execute(); +// [END collection_group_input_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/collection_input.js b/snippets/firestore-temp/test-firestore/collection_input.js new file mode 100644 index 00000000..e5cb6021 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/collection_input.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_input_modular] +const results = await db.pipeline() + .collection("cities") + .sort(field("name").ascending()) + .execute(); +// [END collection_input_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/collection_input_data.js b/snippets/firestore-temp/test-firestore/collection_input_data.js new file mode 100644 index 00000000..639f9637 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/collection_input_data.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_input_data_modular] +await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California"}); +await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"}); +await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"}); +await db.collection("states").doc("CA").set({name: "California"}); +// [END collection_input_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/collection_input_syntax.js b/snippets/firestore-temp/test-firestore/collection_input_syntax.js new file mode 100644 index 00000000..17cc7812 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/collection_input_syntax.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 collection_input_syntax_modular] +const results = await db.pipeline() + .collection("cities/SF/departments") + .execute(); +// [END collection_input_syntax_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/create_where_data.js b/snippets/firestore-temp/test-firestore/create_where_data.js new file mode 100644 index 00000000..2e0a9fb6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/create_where_data.js @@ -0,0 +1,23 @@ +// 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 create_where_data_modular] +await db.collection("cities").doc("SF").set( + { name: "San Francisco", state: "CA", country: "USA", population: 870000 } +); +await db.collection("cities").doc("LA").set( + { name: "Los Angeles", state: "CA", country: "USA", population: 3970000 } +); +await db.collection("cities").doc("NY").set( + { name: "New York", state: "NY", country: "USA", population: 8530000 } +); +await db.collection("cities").doc("TOR").set( + { name: "Toronto", state: null, country: "Canada", population: 2930000 } +); +await db.collection("cities").doc("MEX").set( + { name: "Mexico City", state: null, country: "Mexico", population: 9200000 } +); +// [END create_where_data_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/database_input.js b/snippets/firestore-temp/test-firestore/database_input.js new file mode 100644 index 00000000..08449e89 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/database_input.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 database_input_modular] +const results = await db.pipeline() + .database() + .sort(field("population").ascending()) + .execute(); +// [END database_input_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/database_input_data.js b/snippets/firestore-temp/test-firestore/database_input_data.js new file mode 100644 index 00000000..e4a0d6b7 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/database_input_data.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 database_input_data_modular] +await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California", population: 800000}); +await db.collection("states").doc("CA").set({name: "California", population: 39000000}); +await db.collection("countries").doc("USA").set({name: "United States of America", population: 340000000}); +// [END database_input_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/database_syntax.js b/snippets/firestore-temp/test-firestore/database_syntax.js new file mode 100644 index 00000000..68fb1d9c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/database_syntax.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 database_syntax_modular] +const results = await db.pipeline() + .database() + .execute(); +// [END database_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/distinct_data.js b/snippets/firestore-temp/test-firestore/distinct_data.js new file mode 100644 index 00000000..856a145d --- /dev/null +++ b/snippets/firestore-temp/test-firestore/distinct_data.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 distinct_data_modular] +await db.collection("cities").doc("SF").set({name: "San Francisco", state: "CA", country: "USA"}); +await db.collection("cities").doc("LA").set({name: "Los Angeles", state: "CA", country: "USA"}); +await db.collection("cities").doc("NY").set({name: "New York", state: "NY", country: "USA"}); +await db.collection("cities").doc("TOR").set({name: "Toronto", state: null, country: "Canada"}); +await db.collection("cities").doc("MEX").set({name: "Mexico City", state: null, country: "Mexico"}); +// [END distinct_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/distinct_example.js b/snippets/firestore-temp/test-firestore/distinct_example.js new file mode 100644 index 00000000..6059027a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/distinct_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 distinct_example_modular] +const cities = await db.pipeline() + .collection("cities") + .distinct("country") + .execute(); +// [END distinct_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/distinct_expressions.js b/snippets/firestore-temp/test-firestore/distinct_expressions.js new file mode 100644 index 00000000..f8a04163 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/distinct_expressions.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 distinct_expressions_modular] +const cities = await db.pipeline() + .collection("cities") + .distinct( + field("state").toLower().as("normalizedState"), + field("country")) + .execute(); +// [END distinct_expressions_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/distinct_syntax.js b/snippets/firestore-temp/test-firestore/distinct_syntax.js new file mode 100644 index 00000000..6583ce15 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/distinct_syntax.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 distinct_syntax_modular] +let cities = await db.pipeline() + .collection("cities") + .distinct("country") + .execute(); + +cities = await db.pipeline() + .collection("cities") + .distinct( + field("state").toLower().as("normalizedState"), + field("country")) + .execute(); +// [END distinct_syntax_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/document_input.js b/snippets/firestore-temp/test-firestore/document_input.js new file mode 100644 index 00000000..06eca346 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/document_input.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 document_input_modular] +const results = await db.pipeline() + .documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("NYC")]) + .sort(field("name").ascending()) + .execute(); +// [END document_input_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/document_input_data.js b/snippets/firestore-temp/test-firestore/document_input_data.js new file mode 100644 index 00000000..5a4053d3 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/document_input_data.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 document_input_data_modular] +await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California"}); +await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"}); +await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"}); +// [END document_input_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/document_input_syntax.js b/snippets/firestore-temp/test-firestore/document_input_syntax.js new file mode 100644 index 00000000..a5e7333c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/document_input_syntax.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 document_input_syntax_modular] +const results = await db.pipeline() + .documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("NY")]) + .execute(); +// [END document_input_syntax_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/find_nearest_distance.js b/snippets/firestore-temp/test-firestore/find_nearest_distance.js new file mode 100644 index 00000000..b32c3507 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/find_nearest_distance.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 find_nearest_distance_modular] +const results = await db.pipeline() +.collection("cities") +.findNearest({ + field: "embedding", + vectorValue: [1.3, 2.345], + distanceMeasure: "euclidean", + distanceField: "computedDistance", +}) +.execute(); +// [END find_nearest_distance_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/find_nearest_distance_data.js b/snippets/firestore-temp/test-firestore/find_nearest_distance_data.js new file mode 100644 index 00000000..61e2a6bb --- /dev/null +++ b/snippets/firestore-temp/test-firestore/find_nearest_distance_data.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 find_nearest_distance_data_modular] +await db.collection("cities").doc("SF").set({name: "San Francisco", embedding: [1.0, -1.0]}); +await db.collection("cities").doc("TO").set({name: "Toronto", embedding: [5.0, -10.0]}); +await db.collection("cities").doc("AT").set({name: "Atlantis", embedding: [2.0, -4.0]}); +// [END find_nearest_distance_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/find_nearest_limit.js b/snippets/firestore-temp/test-firestore/find_nearest_limit.js new file mode 100644 index 00000000..9abed66e --- /dev/null +++ b/snippets/firestore-temp/test-firestore/find_nearest_limit.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 find_nearest_limit_modular] +const results = await db.pipeline() + .collection("cities") + .findNearest({ + field: "embedding", + vectorValue: [1.5, 2.345], + distanceMeasure: "euclidean", + limit: 10 + }) + .execute(); +// [END find_nearest_limit_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/find_nearest_syntax.js b/snippets/firestore-temp/test-firestore/find_nearest_syntax.js new file mode 100644 index 00000000..86689c9d --- /dev/null +++ b/snippets/firestore-temp/test-firestore/find_nearest_syntax.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 find_nearest_syntax_modular] +const results = await db.pipeline() + .collection("cities") + .findNearest({ + field: "embedding", + vectorValue: [1.5, 2.345], + distanceMeasure: "euclidean" + }) + .execute(); +// [END find_nearest_syntax_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/limit_syntax.js b/snippets/firestore-temp/test-firestore/limit_syntax.js new file mode 100644 index 00000000..05591a0f --- /dev/null +++ b/snippets/firestore-temp/test-firestore/limit_syntax.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 limit_syntax_modular] +const results = await db.pipeline() + .collection("cities") + .limit(10) + .execute(); +// [END limit_syntax_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/offset_syntax.js b/snippets/firestore-temp/test-firestore/offset_syntax.js new file mode 100644 index 00000000..98ae580c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/offset_syntax.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 offset_syntax_modular] +const results = await db.pipeline() + .collection("cities") + .offset(10) + .execute(); +// [END offset_syntax_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..db7f2081 --- /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/query_explain.js b/snippets/firestore-temp/test-firestore/query_explain.js new file mode 100644 index 00000000..7c721b0c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/query_explain.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_explain_modular] +const q = db.collection("cities").where("country", "==", "USA"); +const options = { analyze: false }; + +const explainResults = await q.explain(options); + +const metrics = explainResults.metrics; +const plan = metrics.planSummary; +// [END query_explain_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/remove_fields_nested.js b/snippets/firestore-temp/test-firestore/remove_fields_nested.js new file mode 100644 index 00000000..6571a227 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/remove_fields_nested.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 remove_fields_nested_modular] +const results = await db.pipeline() + .collection("cities") + .removeFields("location.state") + .execute(); +// [END remove_fields_nested_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/remove_fields_nested_data.js b/snippets/firestore-temp/test-firestore/remove_fields_nested_data.js new file mode 100644 index 00000000..ae30e5f1 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/remove_fields_nested_data.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 remove_fields_nested_data_modular] +await db.collection("cities").doc("SF").set({ + name: "San Francisco", location: {country: "USA", state: "California"} +}); +await db.collection("cities").doc("TO").set({ + name: "Toronto", location: {country: "Canada", province: "Ontario"} +}); +// [END remove_fields_nested_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/remove_fields_syntax.js b/snippets/firestore-temp/test-firestore/remove_fields_syntax.js new file mode 100644 index 00000000..86088b71 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/remove_fields_syntax.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 remove_fields_syntax_modular] +const results = await db.pipeline() + .collection("cities") + .removeFields("population", "location.state") + .execute(); +// [END remove_fields_syntax_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_all_documents.js b/snippets/firestore-temp/test-firestore/sample_all_documents.js new file mode 100644 index 00000000..466a5f78 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sample_all_documents.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 sample_all_documents_modular] +const sampled = await db.pipeline() + .collection("cities") + .sample(5) + .execute(); +// [END sample_all_documents_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sample_documents.js b/snippets/firestore-temp/test-firestore/sample_documents.js new file mode 100644 index 00000000..32cb446b --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sample_documents.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 sample_documents_modular] +const sampled = await db.pipeline() + .collection("cities") + .sample(1) + .execute(); +// [END sample_documents_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sample_documents_data.js b/snippets/firestore-temp/test-firestore/sample_documents_data.js new file mode 100644 index 00000000..a2fdea8a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sample_documents_data.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 sample_documents_data_modular] +await db.collection("cities").doc("SF").set({name: "San Francisco", state: "California"}); +await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"}); +await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"}); +// [END sample_documents_data_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/sample_percentage.js b/snippets/firestore-temp/test-firestore/sample_percentage.js new file mode 100644 index 00000000..416c9718 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sample_percentage.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 sample_percentage_modular] +const sampled = await db.pipeline() + .collection("cities") + .sample({ percentage: 0.5 }) + .execute(); +// [END sample_percentage_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sample_percentage_data.js b/snippets/firestore-temp/test-firestore/sample_percentage_data.js new file mode 100644 index 00000000..8ed643e5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sample_percentage_data.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 sample_percentage_data_modular] +await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California"}); +await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"}); +await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"}); +await db.collection("cities").doc("ATL").set({name: "Atlanta", state: "Georgia"}); +// [END sample_percentage_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sample_syntax.js b/snippets/firestore-temp/test-firestore/sample_syntax.js new file mode 100644 index 00000000..209e0c6d --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sample_syntax.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 sample_syntax_modular] +let sampled = await db.pipeline() + .database() + .sample(50) + .execute(); + +sampled = await db.pipeline() + .database() + .sample({ percentage: 0.5 }) + .execute(); +// [END sample_syntax_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/select_bad_position.js b/snippets/firestore-temp/test-firestore/select_bad_position.js new file mode 100644 index 00000000..e46f6aab --- /dev/null +++ b/snippets/firestore-temp/test-firestore/select_bad_position.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 select_bad_position_modular] +const names = await db.pipeline() + .collection("cities") + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population") + .where(field("location.country").equal("Canada")) + .execute(); +// [END select_bad_position_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/select_nested.js b/snippets/firestore-temp/test-firestore/select_nested.js new file mode 100644 index 00000000..df108c41 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/select_nested.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 select_nested_modular] +const locations = await db.pipeline() + .collection("cities") + .select( + field("name").as("city"), + field("location.country").as("country"), + field("landmarks").arrayGet(0).as("topLandmark") + ).execute(); +// [END select_nested_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/select_nested_data.js b/snippets/firestore-temp/test-firestore/select_nested_data.js new file mode 100644 index 00000000..20ef575a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/select_nested_data.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 select_nested_data_modular] +await db.collection("cities").doc("SF").set({name: "San Francisco", population: 800000, location: {country: "USA", state: "California"}, landmarks: ["Golden Gate Bridge", "Alcatraz"]}); +await db.collection("cities").doc("TO").set({name: "Toronto", population: 3000000, province: "ON", location: {country: "Canada", province: "Ontario"}, landmarks: ["CN Tower", "Casa Loma"]}); +await db.collection("cities").doc("AT").set({name: "Atlantis", population: null}); +// [END select_nested_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/select_position.js b/snippets/firestore-temp/test-firestore/select_position.js new file mode 100644 index 00000000..332617bf --- /dev/null +++ b/snippets/firestore-temp/test-firestore/select_position.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 select_position_modular] +const names = await db.pipeline() + .collection("cities") + .where(field("location.country").equal("Canada")) + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population") + .execute(); +// [END select_position_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/select_position_data.js b/snippets/firestore-temp/test-firestore/select_position_data.js new file mode 100644 index 00000000..e27f17bd --- /dev/null +++ b/snippets/firestore-temp/test-firestore/select_position_data.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 select_position_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, location: {country: "Canada", province: "Ontario"} +}); +// [END select_position_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/select_syntax.js b/snippets/firestore-temp/test-firestore/select_syntax.js new file mode 100644 index 00000000..eb5b2606 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/select_syntax.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 select_syntax_modular] +const names = await db.pipeline() + .collection("cities") + .select( + field("name").stringConcat(", ", field("location.country")).as("name"), + "population" + ).execute(); +// [END select_syntax_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/sort_document_id.js b/snippets/firestore-temp/test-firestore/sort_document_id.js new file mode 100644 index 00000000..94b88a66 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sort_document_id.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 sort_document_id_modular] +const results = await db.pipeline() + .collection("cities") + .sort(field("country").ascending(), field("__name__").ascending()) + .execute(); +// [END sort_document_id_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sort_syntax.js b/snippets/firestore-temp/test-firestore/sort_syntax.js new file mode 100644 index 00000000..6561ef8a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sort_syntax.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 sort_syntax_modular] +const results = await db.pipeline() + .collection("cities") + .sort(field("population").ascending()) + .execute(); +// [END sort_syntax_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/stages_expressions_example.js b/snippets/firestore-temp/test-firestore/stages_expressions_example.js new file mode 100644 index 00000000..43e923c0 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/stages_expressions_example.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 stages_expressions_example_modular] +const trailing30Days = constant(Timestamp.now().toMillis()) + .unixMillisToTimestamp() + .timestampSubtract("day", 30); +const snapshot = await db.pipeline() + .collection("productViews") + .where(field("viewedAt").greaterThan(trailing30Days)) + .aggregate(field("productId").countDistinct().as("uniqueProductViews")) + .execute(); +// [END stages_expressions_example_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/subcollection_input.js b/snippets/firestore-temp/test-firestore/subcollection_input.js new file mode 100644 index 00000000..3206b2da --- /dev/null +++ b/snippets/firestore-temp/test-firestore/subcollection_input.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 subcollection_input_modular] +const results = await db.pipeline() + .collection("cities/NY/departments") + .sort(field("employees").ascending()) + .execute(); +// [END subcollection_input_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/subcollection_input_data.js b/snippets/firestore-temp/test-firestore/subcollection_input_data.js new file mode 100644 index 00000000..a7b47aa4 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/subcollection_input_data.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 subcollection_input_data_modular] +await db.collection("cities/SF/departments").doc("building") + .set({name: "SF Building Deparment", employees: 750}); +await db.collection("cities/NY/departments").doc("building") + .set({name: "NY Building Deparment", employees: 1000}); +await db.collection("cities/CHI/departments").doc("building") + .set({name: "CHI Building Deparment", employees: 900}); +await db.collection("cities/NY/departments").doc("finance") + .set({name: "NY Finance Deparment", employees: 1200}); +// [END subcollection_input_data_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/union_syntax.js b/snippets/firestore-temp/test-firestore/union_syntax.js new file mode 100644 index 00000000..7645dd8c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/union_syntax.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 union_syntax_modular] +const results = await db.pipeline() + .collection("cities/SF/restaurants") + .union(db.pipeline().collection("cities/NYC/restaurants")) + .execute(); +// [END union_syntax_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_alias_index.js b/snippets/firestore-temp/test-firestore/unnest_alias_index.js new file mode 100644 index 00000000..457e3e53 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_alias_index.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_alias_index_modular] +const userScore = await db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt") + .execute(); +// [END unnest_alias_index_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_alias_index_data.js b/snippets/firestore-temp/test-firestore/unnest_alias_index_data.js new file mode 100644 index 00000000..2f6134f9 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_alias_index_data.js @@ -0,0 +1,10 @@ +// 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_alias_index_data_modular] +await db.collection("users").add({name: "foo", scores: [5, 4], userScore: 0}); +await db.collection("users").add({name: "bar", scores: [1, 3], attempt: 5}); +// [END unnest_alias_index_data_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_empty_array.js b/snippets/firestore-temp/test-firestore/unnest_empty_array.js new file mode 100644 index 00000000..2f4453eb --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_empty_array.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_empty_array_modular] +const userScore = await db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt") + .execute(); +// [END unnest_empty_array_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_empty_array_data.js b/snippets/firestore-temp/test-firestore/unnest_empty_array_data.js new file mode 100644 index 00000000..43138c92 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_empty_array_data.js @@ -0,0 +1,10 @@ +// 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_empty_array_data_modular] +await db.collection("users").add({name: "foo", scores: [5, 4]}); +await db.collection("users").add({name: "bar", scores: []}); +// [END unnest_empty_array_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_nested.js b/snippets/firestore-temp/test-firestore/unnest_nested.js new file mode 100644 index 00000000..2ba3d502 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_nested.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 unnest_nested_modular] +const userScore = await db.pipeline() + .collection("users") + .unnest(field("record").as("record")) + .unnest(field("record.scores").as("userScore"), /* index_field= */ "attempt") + .execute(); +// [END unnest_nested_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_nested_data.js b/snippets/firestore-temp/test-firestore/unnest_nested_data.js new file mode 100644 index 00000000..2e397a12 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_nested_data.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 unnest_nested_data_modular] +await db.collection("users").add({ + name: "foo", + record: [ + { + scores: [5, 4], + avg: 4.5 + }, { + scores: [1, 3], + old_avg: 2 + } + ] +}); +// [END unnest_nested_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_nonarray.js b/snippets/firestore-temp/test-firestore/unnest_nonarray.js new file mode 100644 index 00000000..390f720c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_nonarray.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_nonarray_modular] +const userScore = await db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt") + .execute(); +// [END unnest_nonarray_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_nonarray_data.js b/snippets/firestore-temp/test-firestore/unnest_nonarray_data.js new file mode 100644 index 00000000..373d3ceb --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_nonarray_data.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 unnest_nonarray_data_modular] + await db.collection("users").add({name: "foo", scores: 1}); + await db.collection("users").add({name: "bar", scores: null}); + await db.collection("users").add({name: "qux", scores: {backupScores: 1}}); +// [END unnest_nonarray_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_preserve_empty_array.js b/snippets/firestore-temp/test-firestore/unnest_preserve_empty_array.js new file mode 100644 index 00000000..362cae39 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_preserve_empty_array.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 unnest_preserve_empty_array_modular] +const userScore = await db.pipeline() + .collection("users") + .unnest( + conditional( + field("scores").equal(array([])), + array([field("scores")]), + field("scores") + ).as("userScore"), + /* index_field= */ "attempt") + .execute(); +// [END unnest_preserve_empty_array_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/unnest_syntax.js b/snippets/firestore-temp/test-firestore/unnest_syntax.js new file mode 100644 index 00000000..59892236 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_syntax.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_syntax_modular] +const userScore = await db.pipeline() + .collection("users") + .unnest(field("scores").as("userScore"), /* index_field= */ "attempt") + .execute(); +// [END unnest_syntax_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/where_complex.js b/snippets/firestore-temp/test-firestore/where_complex.js new file mode 100644 index 00000000..283d125b --- /dev/null +++ b/snippets/firestore-temp/test-firestore/where_complex.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 where_complex_modular] +const cities = await db.pipeline() + .collection("cities") + .where( + or( + like(field("name"), "San%"), + and( + field("location.state").charLength().greaterThan(7), + field("location.country").equal("USA") + ) + ) + ).execute(); +// [END where_complex_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/where_equality_example.js b/snippets/firestore-temp/test-firestore/where_equality_example.js new file mode 100644 index 00000000..71555f51 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/where_equality_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 where_equality_example_modular] +const cities = await db.pipeline() + .collection("cities") + .where(field("state").equal("CA")) + .execute(); +// [END where_equality_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/where_having_example.js b/snippets/firestore-temp/test-firestore/where_having_example.js new file mode 100644 index 00000000..81b5f415 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/where_having_example.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 where_having_example_modular] +const cities = await db.pipeline() + .collection("cities") + .aggregate({ + accumulators: [field("population").sum().as("totalPopulation")], + groups: ["location.state"] + }) + .where(field("totalPopulation").greaterThan(10000000)) + .execute(); +// [END where_having_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/where_multiple_stages.js b/snippets/firestore-temp/test-firestore/where_multiple_stages.js new file mode 100644 index 00000000..77b1fb6f --- /dev/null +++ b/snippets/firestore-temp/test-firestore/where_multiple_stages.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 where_multiple_stages_modular] +const cities = await db.pipeline() + .collection("cities") + .where(field("location.country").equal("USA")) + .where(field("population").greaterThan(500000)) + .execute(); +// [END where_multiple_stages_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/where_stage_order.js b/snippets/firestore-temp/test-firestore/where_stage_order.js new file mode 100644 index 00000000..f24dfa7d --- /dev/null +++ b/snippets/firestore-temp/test-firestore/where_stage_order.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 where_stage_order_modular] +const cities = await db.pipeline() + .collection("cities") + .limit(10) + .where(field("location.country").equal("USA")) + .execute(); +// [END where_stage_order_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