Firestore Pipeline অপারেশনের মাধ্যমে ডেটা পাওয়া

ব্যাকগ্রাউন্ড

পাইপলাইন অপারেশন Cloud Firestore-এর জন্য একটি নতুন কোয়েরি ইন্টারফেস প্রদান করে যা উন্নত কোয়েরি কার্যকারিতা এবং জটিল এক্সপ্রেশনকে সমর্থন করে। এটি min(...), max(...), substring(...), regex_match(...), এবং array_contains_all(...) সহ অনেক নতুন ফাংশন এবং জটিল ট্রান্সফর্মেশন সম্পাদন করার জন্য স্টেজ পরিচয় করিয়ে দেয়।

শুরু করা

ক্লায়েন্ট SDK ইনস্টল ও শুরু করতে, নিম্নলিখিত নির্দেশিকায় দেওয়া নির্দেশাবলী দেখুন:

সিনট্যাক্স

নিম্নলিখিত বিভাগগুলি পাইপলাইন অপারেশনের জন্য সিনট্যাক্সের একটি ওভারভিউ প্রদান করে।

ধারণা

পাইপলাইন অপারেশনের সাথে একটি উল্লেখযোগ্য পার্থক্য হল স্পষ্ট "স্টেজ" অর্ডারিংয়ের প্রবর্তন। এর ফলে আরও জটিল কোয়েরি প্রকাশ করা সম্ভব হয়। তবে, এটি কোর অপারেশন ব্যবহার করে বর্তমান কোয়েরি ইন্টারফেস থেকে একটি উল্লেখযোগ্য বিচ্যুতি যেখানে স্টেজের ক্রম বোঝানো হয়েছে। নিচে উল্লেখ করা পাইপলাইন অপারেশন সংক্রান্ত উদাহরণটি বিবেচনা করুন:

Web

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);
Swift
let 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)

Kotlin

val 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)

Java

Pipeline 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);
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

pipeline = (
    client.pipeline()
    .collection("cities")
    .where(Field.of("population").greater_than(100_000))
    .sort(Field.of("name").ascending())
    .limit(10)
)
Go
pipeline := client.Pipeline().
	Collection("cities").
	Where(firestore.FieldOf("population").GreaterThan(100000)).
	Sort(firestore.Orders(firestore.Ascending(firestore.FieldOf("name")))).
	Limit(10)

শুরু করা

পাইপলাইন অপারেশনের সিনট্যাক্স আগে থেকে থাকা Cloud Firestore কোয়েরির মতোই। শুরু করতে, আপনি নিম্নলিখিত কোড লিখে কোয়েরি ইনিশিয়ালাইজ করুন:

Web

const { getFirestore } = require("firebase/firestore");
const { execute } = require("firebase/firestore/pipelines");
const database = getFirestore(app, "enterprise");
const pipeline = database.pipeline();
Swift
let firestore = Firestore.firestore(database: "enterprise")
let pipeline = firestore.pipeline()

Kotlin

val firestore = Firebase.firestore("enterprise")
val pipeline = firestore.pipeline()

Java

FirebaseFirestore firestore = FirebaseFirestore.getInstance("enterprise");
PipelineSource pipeline = firestore.pipeline();
Python
firestore_client = firestore.client(default_app, "your-new-enterprise-database")
pipeline = firestore_client.pipeline()
Go
client, err := firestore.NewClientWithDatabase(ctx, projectID, databaseID)
if err != nil {
	fmt.Fprintf(w, "firestore.NewClientWithDatabase failed: %v", err)
	return err
}
defer client.Close()
pipeline := client.Pipeline().Collection("books")

গঠন

পাইপলাইন অপারেশন: স্টেজ, এক্সপ্রেশন ও ফাংশন এবং সাবকোয়েরি র‍্যাপার তৈরি করার সময় কিছু শব্দ বোঝা গুরুত্বপূর্ণ।

কোয়েরিতে বিভিন্ন পর্যায় ও এক্সপ্রেশন দেখানোর উদাহরণ

পর্যায়: একটি পাইপলাইনে এক বা একাধিক পর্যায় থাকতে পারে। যুক্তিগতভাবে, এগুলি কোয়েরি এক্সিকিউট করার জন্য নেওয়া ধাপের (বা স্টেজের) সিরিজকে দেখায়।

এক্সপ্রেশন: স্টেজ প্রায়ই এক্সপ্রেশন গ্রহণ করে, যা আপনাকে আরও জটিল কোয়েরি প্রকাশ করার অনুমতি দেয়। এক্সপ্রেশন সহজ হতে পারে এবং এতে eq("a", 1)-এর মতো একটি ফাংশন থাকতে পারে। এছাড়াও, আপনি and(eq("a", 1), eq("b", 2)).-এর মতো এক্সপ্রেশন নেস্ট করার মাধ্যমে আরও জটিল এক্সপ্রেশন প্রকাশ করতে পারেন

সাবকোয়েরি র‍্যাপার: array() এবং scalar()-এর মতো ফাংশন আপনাকে একটি স্টেজের মধ্যে এক্সপ্রেশন হিসেবে নেস্টেড পাইপলাইন এম্বেড করার অনুমতি দেয়।

ফিল্ড / কনস্ট্যান্ট / ভেরিয়েবল

পাইপলাইন অপারেশনে জটিল এক্সপ্রেশন কাজ করে। তাই, কোনও ভ্যালু ফিল্ড, কনস্ট্যান্ট নাকি ভেরিয়েবল তা আলাদা করে বোঝা গুরুত্বপূর্ণ।

ফিল্ড বলতে ডকুমেন্টের মধ্যে থাকা ডেটা বোঝায় এবং কনস্ট্যান্ট কোনও এক্সপ্রেশনের আর্গুমেন্ট হিসেবে যেকোনও ভ্যালু নির্দিষ্ট করার অনুমতি দেয়, ভেরিয়েবল প্রসেস করা হচ্ছে এমন ডকুমেন্টের পরিবর্তে কোয়েরি এক্সিকিউশনের স্কোপে থাকা অস্থায়ী ভ্যালু সংজ্ঞায়িত ও ব্যবহার করার অনুমতি দেয়। নিচে এইসব ধারণার ওভারভিউ দেওয়া হল, কোয়েরি এক্সিকিউশনের সময় কীভাবে ভেরিয়েবল পড়তে ও লিখতে হয় সেই সম্পর্কে আরও জানতে let(...) স্টেজ দেখুন।

ফিল্ড ধ্রুবক ভেরিয়েবল
উদ্দেশ্য ডকুমেন্টে ফিল্ড অ্যাক্সেস বা স্টোর করা নির্দিষ্ট ভ্যালু উল্লেখ করুন পাইপলাইন এক্সিকিউশনের সময় সাময়িক ভ্যালু ব্যবহার করুন
SDK ব্যবহার field("name") constant("val") variable("name")
সুযোগ বর্তমান ডকুমেন্টের লোকাল গ্লোবাল গ্লোবাল থেকে পাইপলাইন এবং সাব-পাইপলাইন
অনির্দিষ্ট রেফারেন্স absent-এ মূল্যায়ন করে প্রযোজ্য নয় রানটাইম সমস্যা জেনারেট করে

উদাহরণ:

Web

const pipeline = db.pipeline()
  .collection("cities")
  .where(field("name").equal(constant("Toronto")));
Swift
let pipeline = db.pipeline()
  .collection("cities")
  .where(Field("name").equal(Constant("Toronto")))

Kotlin

val pipeline = db.pipeline()
    .collection("cities")
    .where(field("name").equal(constant("Toronto")))

Java

Pipeline pipeline = db.pipeline()
    .collection("cities")
    .where(field("name").equal(constant("Toronto")));
Python
from google.cloud.firestore_v1.pipeline_expressions import Field, Constant

pipeline = (
    client.pipeline()
    .collection("cities")
    .where(Field.of("name").equal(Constant.of("Toronto")))
)
Go
pipeline := client.Pipeline().Collection("cities").
	Where(firestore.FieldOf("name").Equal(firestore.ConstantOf("Toronto")))

স্টেজ

ইনপুট স্টেজ

ইনপুট স্টেজ হল কোনও কোয়েরির প্রথম স্টেজ। আপনি যে ডকুমেন্টের সেট নিয়ে কোয়েরি করছেন, এটি তার প্রাথমিক সেট নির্ধারণ করে। পাইপলাইন অপারেশনের জন্য, এটি মূলত আগে থেকে থাকা কোয়েরির মতোই, যেখানে বেশিরভাগ কোয়েরি collection(...) অথবা collection_group(...) স্টেজের মাধ্যমে শুরু হয়। দুটি নতুন ইনপুট স্টেজ হল database() এবং documents(...) যেখানে database() ডেটাবেসে সব ডকুমেন্ট ফিরিয়ে আনার অনুমতি দেয়, যেখানে documents(...) ব্যাচ রিডের মতো কাজ করে।

Web

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")
]));
Swift
var results: Pipeline.Snapshot

// Return all restaurants in San Francisco
results = try await db.pipeline().collection("cities/sf/restaurants").execute()

// Return all restaurants
results = try await db.pipeline().collectionGroup("restaurants").execute()

// Return all documents across all collections in the database (the entire database)
results = try await db.pipeline().database().execute()

// Batch read of 3 documents
results = try await db.pipeline().documents([
  db.collection("cities").document("SF"),
  db.collection("cities").document("DC"),
  db.collection("cities").document("NY")
]).execute()

Kotlin

var results: Task<Pipeline.Snapshot>

// Return all restaurants in San Francisco
results = db.pipeline().collection("cities/sf/restaurants").execute()

// Return all restaurants
results = db.pipeline().collectionGroup("restaurants").execute()

// Return all documents across all collections in the database (the entire database)
results = db.pipeline().database().execute()

// Batch read of 3 documents
results = db.pipeline().documents(
    db.collection("cities").document("SF"),
    db.collection("cities").document("DC"),
    db.collection("cities").document("NY")
).execute()

Java

Task<Pipeline.Snapshot> results;

// Return all restaurants in San Francisco
results = db.pipeline().collection("cities/sf/restaurants").execute();

// Return all restaurants
results = db.pipeline().collectionGroup("restaurants").execute();

// Return all documents across all collections in the database (the entire database)
results = db.pipeline().database().execute();

// Batch read of 3 documents
results = db.pipeline().documents(
    db.collection("cities").document("SF"),
    db.collection("cities").document("DC"),
    db.collection("cities").document("NY")
).execute();
Python
# Return all restaurants in San Francisco
results = client.pipeline().collection("cities/sf/restaurants").execute()

# Return all restaurants
results = client.pipeline().collection_group("restaurants").execute()

# Return all documents across all collections in the database (the entire database)
results = client.pipeline().database().execute()

# Batch read of 3 documents
results = (
    client.pipeline()
    .documents(
        client.collection("cities").document("SF"),
        client.collection("cities").document("DC"),
        client.collection("cities").document("NY"),
    )
    .execute()
)
Go
// Return all restaurants in San Francisco
results1, err := client.Pipeline().Collection("cities/sf/restaurants").Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

// Return all restaurants
results2, err := client.Pipeline().CollectionGroup("restaurants").Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

// Return all documents across all collections in the database (the entire database)
results3, err := client.Pipeline().Database().Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

// Batch read of 3 documents
results4, err := client.Pipeline().
	Documents([]*firestore.DocumentRef{
		client.Collection("cities").Doc("SF"),
		client.Collection("cities").Doc("DC"),
		client.Collection("cities").Doc("NY"),
	}).
	Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

অন্যান্য সব স্টেজের মতো, এইসব ইনপুট স্টেজের ফলাফলের ক্রম স্থির নয়। নির্দিষ্ট ক্রমের প্রয়োজন হলে, সবসময় sort(...) অপারেটর যোগ করতে হবে।

কোথায়

where(...) স্টেজটি আগের স্টেজ থেকে তৈরি হওয়া ডকুমেন্টের উপর স্ট্যান্ডার্ড ফিল্টার অপারেশন হিসেবে কাজ করে এবং বেশিরভাগ ক্ষেত্রে আগে থেকে থাকা কোয়েরির জন্য আগে থেকে থাকা "where" সিনট্যাক্সকে মিরর করে। যেসব ডকুমেন্টে প্রদত্ত এক্সপ্রেশন থেকে true ছাড়া অন্য কোনও ভ্যালু পাওয়া যায়, সেগুলি ফেরত পাঠানো ডকুমেন্টের তালিকা থেকে ফিল্টার করে বাদ দেওয়া হয়।

একাধিক where(...) স্টেটমেন্ট একসাথে চেইন করা যেতে পারে এবং and(...) এক্সপ্রেশন হিসেবে কাজ করে। যেমন, নিম্নলিখিত দুটি কোয়েরি লজিকের দিক থেকে একই এবং একটির পরিবর্তে অন্যটি ব্যবহার করা যেতে পারে।

Web

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)))
);
Swift
var results: Pipeline.Snapshot

results = try await db.pipeline().collection("books")
  .where(Field("rating").equal(5))
  .where(Field("published").lessThan(1900))
  .execute()

results = try await db.pipeline().collection("books")
  .where(Field("rating").equal(5) && Field("published").lessThan(1900))
  .execute()

Kotlin

var results: Task<Pipeline.Snapshot>

results = db.pipeline().collection("books")
    .where(field("rating").equal(5))
    .where(field("published").lessThan(1900))
    .execute()

results = db.pipeline().collection("books")
    .where(Expression.and(field("rating").equal(5),
      field("published").lessThan(1900)))
    .execute()

Java

Task<Pipeline.Snapshot> results;

results = db.pipeline().collection("books")
    .where(field("rating").equal(5))
    .where(field("published").lessThan(1900))
    .execute();

results = db.pipeline().collection("books")
    .where(Expression.and(
        field("rating").equal(5),
        field("published").lessThan(1900)
    ))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import And, Field

results = (
    client.pipeline()
    .collection("books")
    .where(Field.of("rating").equal(5))
    .where(Field.of("published").less_than(1900))
    .execute()
)

results = (
    client.pipeline()
    .collection("books")
    .where(And(Field.of("rating").equal(5), Field.of("published").less_than(1900)))
    .execute()
)
Go
results1, err := client.Pipeline().
	Collection("books").
	Where(firestore.FieldOf("rating").Equal(5)).
	Where(firestore.FieldOf("published").LessThan(1900)).
	Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

results2, err := client.Pipeline().
	Collection("books").
	Where(firestore.And(
		firestore.FieldOf("rating").Equal(5),
		firestore.FieldOf("published").LessThan(1900),
	)).
	Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

ফিল্ড বেছে নিন / যোগ করুন ও সরান

select(...), add_fields(...), ও remove_fields(...) আপনাকে আগের ধাপ থেকে ফিরে আসা ফিল্ডগুলি পরিবর্তন করার অনুমতি দেয়। এই তিনটিকে সাধারণত প্রোজেকশন-স্টাইল স্টেজ বলা হয়।

select(...) এবং add_fields(...) আপনাকে ব্যবহারকারীর দেওয়া ফিল্ডের নামে এক্সপ্রেশনের ফলাফল নির্দিষ্ট করার অনুমতি দেয়। select(...) শুধুমাত্র নির্দিষ্ট ফিল্ডের নাম সহ ডকুমেন্ট রিটার্ন করবে, যেখানে add_fields(...) আগের স্টেজের স্কিমা এক্সটেন্ড করে (সম্ভাব্যভাবে অভিন্ন ফিল্ডের নাম সহ ভ্যালু ওভাররাইট করে)।

remove_fields(...)-এর মাধ্যমে আগের ধাপ থেকে সরিয়ে দেওয়ার জন্য ফিল্ডের সেট নির্দিষ্ট করা যায়। অস্তিত্ব নেই এমন ফিল্ডের নাম উল্লেখ করা হল নো-অপ।

নিচে ফেরত পাঠানোর জন্য ফিল্ড সীমাবদ্ধ করুন বিভাগটি দেখুন, তবে সাধারণত ক্লায়েন্টের জন্য প্রয়োজনীয় ফিল্ডে ফলাফল সীমাবদ্ধ করার জন্য এই ধরনের স্টেজ ব্যবহার করলে, বেশিরভাগ কোয়েরির জন্য খরচ ও লেটেন্সি কমাতে সাহায্য করে।

এগ্রিগেট / স্বতন্ত্র

aggregate(...) স্টেজে আপনি ইনপুট ডকুমেন্টের উপর একাধিক অ্যাগ্রিগেশন করতে পারবেন। ডিফল্ট হিসেবে, সব ডকুমেন্ট একসাথে অ্যাগ্রিগেট করা হয়, তবে একটি ঐচ্ছিক grouping আর্গুমেন্ট প্রদান করা যেতে পারে, যা ইনপুট ডকুমেন্টগুলিকে বিভিন্ন বাকেটে অ্যাগ্রিগেট করার অনুমতি দেয়।

Web

const results = await execute(db.pipeline()
  .collection("books")
  .aggregate(
    field("rating").average().as("avg_rating")
  )
  .distinct(field("genre"))
);
Swift
let results = try await db.pipeline()
  .collection("books")
  .aggregate([
    Field("rating").average().as("avg_rating")
  ], groups: [
    Field("genre")
  ])
  .execute()

Kotlin

val results = db.pipeline()
    .collection("books")
    .aggregate(
        AggregateStage
            .withAccumulators(AggregateFunction.average("rating").alias("avg_rating"))
            .withGroups(field("genre"))
    )
    .execute()

Java

Task<Pipeline.Snapshot> results = db.pipeline()
    .collection("books")
    .aggregate(AggregateStage
        .withAccumulators(
            AggregateFunction.average("rating").alias("avg_rating"))
        .withGroups(field("genre")))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

results = (
    client.pipeline()
    .collection("books")
    .aggregate(
        Field.of("rating").average().as_("avg_rating"), groups=[Field.of("genre")]
    )
    .execute()
)
Go
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(
		firestore.Accumulators(firestore.Average("rating").As("avg_rating")),
		firestore.WithAggregateGroups("genre"),
	).
	Execute(ctx)

groupings উল্লেখ করা না থাকলে, এই পর্যায়ে শুধু একটি ডকুমেন্ট তৈরি হবে, অন্যথায় groupings ভ্যালুর প্রতিটি অনন্য কম্বিনেশনের জন্য একটি করে ডকুমেন্ট তৈরি হবে।

distinct(...) স্টেজ হল একটি সরলীকৃত অ্যাগ্রিগেশন অপারেটর যা কোনও অ্যাকুমুলেটর ছাড়াই অনন্য groupings তৈরি করার অনুমতি দেয়। অন্যান্য সব ক্ষেত্রে এটি aggregate(...)-এর মতোই কাজ করে। নিম্নলিখিত উদাহরণে দেখানো হয়েছে:

Web

const results = await execute(db.pipeline()
  .collection("books")
  .distinct(
    field("author").toUpper().as("author"),
    field("genre")
  )
);
Swift
let results = try await db.pipeline()
  .collection("books")
  .distinct([
    Field("author").toUpper().as("author"),
    Field("genre")
  ])
  .execute()

Kotlin

val results = db.pipeline()
    .collection("books")
    .distinct(
        field("author").toUpper().alias("author"),
        field("genre")
    )
    .execute()

Java

Task<Pipeline.Snapshot> results = db.pipeline()
    .collection("books")
    .distinct(
        field("author").toUpper().alias("author"),
        field("genre")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

results = (
    client.pipeline()
    .collection("books")
    .distinct(Field.of("author").to_upper().as_("author"), "genre")
    .execute()
)
Go
snapshot := client.Pipeline().
	Collection("books").
	Distinct(firestore.Fields(
		firestore.ToUpper(firestore.FieldOf("author")).As("author"),
		firestore.FieldOf("genre"),
	)).
	Execute(ctx)

ফাংশন

ফাংশন হল এক্সপ্রেশন ও জটিল কোয়েরি তৈরি করার বিল্ডিং ব্লক। উদাহরণ সহ ফাংশনের সম্পূর্ণ তালিকার জন্য, ফাংশন রেফারেন্স দেখুন। একবার মনে করিয়ে দেওয়ার জন্য, সাধারণ কোয়েরির স্ট্রাকচার বিবেচনা করুন:

কোয়েরিতে বিভিন্ন পর্যায় ও ফাংশন দেখানোর উদাহরণ

অনেক স্টেজে এক্সপ্রেশন গ্রহণ করা হয় যাতে এক বা একাধিক ফাংশন থাকে। সবচেয়ে সাধারণ ফাংশনের ব্যবহার where(...) এবং select(...) পর্যায়ে পাওয়া যাবে। দুটি প্রধান ধরনের ফাংশন আছে যেগুলি সম্পর্কে আপনার জানা উচিত:

Web

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"))
);
Swift
var results: Pipeline.Snapshot

// Type 1: Scalar (for use in non-aggregation stages)
// Example: Return the min store price for each book.
results = try await db.pipeline().collection("books")
  .select([
    Field("current").logicalMinimum(["updated"]).as("price_min")
  ])
  .execute()

// Type 2: Aggregation (for use in aggregate stages)
// Example: Return the min price of all books.
results = try await db.pipeline().collection("books")
  .aggregate([Field("price").minimum().as("min_price")])
  .execute()

Kotlin

var results: Task<Pipeline.Snapshot>

// Type 1: Scalar (for use in non-aggregation stages)
// Example: Return the min store price for each book.
results = db.pipeline().collection("books")
    .select(
        field("current").logicalMinimum("updated").alias("price_min")
    )
    .execute()

// Type 2: Aggregation (for use in aggregate stages)
// Example: Return the min price of all books.
results = db.pipeline().collection("books")
    .aggregate(AggregateFunction.minimum("price").alias("min_price"))
    .execute()

Java

Task<Pipeline.Snapshot> results;

// Type 1: Scalar (for use in non-aggregation stages)
// Example: Return the min store price for each book.
results = db.pipeline().collection("books")
    .select(
        field("current").logicalMinimum("updated").alias("price_min")
    )
    .execute();

// Type 2: Aggregation (for use in aggregate stages)
// Example: Return the min price of all books.
results = db.pipeline().collection("books")
    .aggregate(AggregateFunction.minimum("price").alias("min_price"))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

# Type 1: Scalar (for use in non-aggregation stages)
# Example: Return the min store price for each book.
results = (
    client.pipeline()
    .collection("books")
    .select(
        Field.of("current").logical_minimum(Field.of("updated")).as_("price_min")
    )
    .execute()
)

# Type 2: Aggregation (for use in aggregate stages)
# Example: Return the min price of all books.
results = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("price").minimum().as_("min_price"))
    .execute()
)
Go
// Type 1: Scalar (for use in non-aggregation stages)
// Example: Return the min store price for each book.
results1, err := client.Pipeline().
	Collection("books").
	Select(firestore.Fields(
		firestore.LogicalMinimum(firestore.FieldOf("current"), firestore.FieldOf("updated")).As("price_min"),
	)).
	Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

// Type 2: Aggregation (for use in aggregate stages)
// Example: Return the min price of all books.
results2, err := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.Minimum("price").As("min_price"),
	)).
	Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

সীমা

বেশিরভাগ ক্ষেত্রে Enterprise এডিশন কোয়েরির আকারের উপর কোনও সীমা আরোপ করে না। অন্যভাবে বলতে গেলে, IN বা OR কোয়েরিতে আপনি অল্প সংখ্যক ভ্যালুর মধ্যে সীমাবদ্ধ নন। পরিবর্তে, দুটি প্রাথমিক সীমা আছে যা আপনার জানা উচিত:

  • ডেডলাইন: ৬০ সেকেন্ড (Standard সংস্করণের মতোই)।
  • মেমরির ব্যবহার: কোয়েরি এক্সিকিউট করার সময় মেটেরিয়ালাইজড ডেটার পরিমাণের উপর ১২৮ MiB-এর সীমা।

সমস্যা

বিভিন্ন কারণে আপনার কোয়েরি কাজ নাও করতে পারে। এখানে সাধারণ সমস্যা এবং আপনি যে সম্পর্কিত পদক্ষেপ নিতে পারেন তার একটি লিঙ্ক দেওয়া হল:

সমস্যার কোড অ্যাকশন
DEADLINE_EXCEEDED আপনি যে কোয়েরিটি চালাচ্ছেন সেটি ৬০ সেকেন্ডের সময়সীমা অতিক্রম করেছে এবং অতিরিক্ত অপ্টিমাইজেশনের প্রয়োজন। পরামর্শের জন্য পারফর্ম্যান্স বিভাগ দেখুন। আপনি যদি সমস্যার মূল কারণ খুঁজে না পান, তাহলে টিমের সাথে যোগাযোগ করুন।
RESOURCE_EXHAUSTED আপনি যে কোয়েরিটি এক্সিকিউট করছেন সেটি মেমরির সীমা ছাড়িয়ে গেছে এবং এর জন্য অতিরিক্ত অপ্টিমাইজেশন প্রয়োজন। পরামর্শের জন্য পারফর্ম্যান্স বিভাগ দেখুন। আপনি যদি সমস্যার মূল কারণ খুঁজে না পান, তাহলে টিমের সাথে যোগাযোগ করুন।
INTERNAL সহায়তার জন্য টিমের সাথে যোগাযোগ করুন।

পারফরম্যান্স

Enterprise এডিশন ডেটাবেসে সবসময় ইন্ডেক্স থাকার প্রয়োজন নেই। এর অর্থ হল, কোনও কোয়েরি বর্তমান কোয়েরির তুলনায় বেশি লেটেন্সি দেখাতে পারে যা FAILED_PRECONDITION মিসিং ইনডেক্স এররের কারণে সাথে সাথে ব্যর্থ হবে। Pipeline অপারেশনের পারফর্ম্যান্স উন্নত করতে, আপনি কয়েকটি পদক্ষেপ নিতে পারেন।

ইনডেক্স তৈরি করুন

ব্যবহৃত ইন্ডেক্স

আপনার কোয়েরি কোনও ইন্ডেক্স দ্বারা পরিবেশিত হচ্ছে কিনা বা টেবিল স্ক্যানের মতো কম দক্ষ অপারেশনে ফিরে যাচ্ছে কিনা তা শনাক্ত করতে কোয়েরি ব্যাখ্যা আপনাকে সাহায্য করে। আপনার কোয়েরি যদি কোনও ইন্ডেক্স থেকে সম্পূর্ণভাবে পরিবেশিত না হয়, তাহলে আপনি নির্দেশাবলী অনুসরণ করে একটি ইন্ডেক্স তৈরি করতে পারেন।

ইন্ডেক্স তৈরি করা হচ্ছে

ইনডেক্স তৈরি করতে আপনি আগে থেকে থাকা ইনডেক্স ম্যানেজমেন্ট ডকুমেন্টেশন ফলো করতে পারেন। ইনডেক্স তৈরি করার আগে, Cloud Firestore-এ ইনডেক্স সহ সাধারণ পেশাদার পদ্ধতি সম্পর্কে নিজেকে পরিচিত করুন। আপনার কোয়েরি যাতে ইন্ডেক্স ব্যবহার করতে পারে তা নিশ্চিত করতে, নিম্নলিখিত ক্রমে ফিল্ড সহ ইন্ডেক্স তৈরি করার জন্য পেশাদার পদ্ধতি অনুসরণ করুন:

  1. সব ফিল্ড যা ইকুয়ালিটি ফিল্টারে ব্যবহার করা হবে (যেকোনও ক্রমে)
  2. যেসব ফিল্ডের ভিত্তিতে সাজানো হবে (একই ক্রমে)
  3. কোয়েরি কনস্ট্রেন্ট সিলেক্টিভিটির নিম্নক্রম অনুসারে রেঞ্জ বা ইনইকুয়ালিটি ফিল্টারে ব্যবহার করা হবে এমন ফিল্ড

যেমন, নিম্নলিখিত কোয়েরির জন্য,

Web

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())
);
Swift
let results = try 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()

Kotlin

val results = 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()

Java

Task<Pipeline.Snapshot> results = 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();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

results = (
    client.pipeline()
    .collection("books")
    .where(Field.of("published").less_than(1900))
    .where(Field.of("genre").equal("Science Fiction"))
    .where(Field.of("rating").greater_than(4.3))
    .sort(Field.of("published").descending())
    .execute()
)
Go
snapshot := client.Pipeline().
	Collection("books").
	Where(firestore.FieldOf("published").LessThan(1900)).
	Where(firestore.FieldOf("genre").Equal("Science Fiction")).
	Where(firestore.FieldOf("rating").GreaterThan(4.3)).
	Sort(firestore.Orders(firestore.Descending(firestore.FieldOf("published")))).
	Execute(ctx)

সাজেস্ট করা ইন্ডেক্স হল (genre [...], published DESC, avg_rating DESC).-এর জন্য books-এ একটি কালেকশন স্কোপ ইন্ডেক্স

ইনডেক্স ডেনসিটি

Cloud Firestore স্পার্স ও নন-স্পার্স ইন্ডেক্স সাপোর্ট করে। আরও তথ্যের জন্য, ইনডেক্স ডেনসিটি দেখুন।

কভার করা কোয়েরি + সেকেন্ডারি ইনডেক্স

Cloud Firestore সম্পূর্ণ ডকুমেন্ট ফেচ করা এড়িয়ে যেতে পারে এবং ফলাফল ইন্ডেক্স থেকে ফিরিয়ে দিতে পারে যদি সব ফিল্ড সেকেন্ডারি ইন্ডেক্সে উপস্থিত থাকে। এর ফলে সাধারণত লেটেন্সি (এবং খরচ) উল্লেখযোগ্যভাবে কমে যায়। নিচে দেওয়া স্যাম্পেল কোয়েরি ব্যবহার করে:

Web

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"))
);
Swift
let results = try await db.pipeline()
  .collection("books")
  .where(Field("category").like("%fantasy%"))
  .where(Field("title").exists())
  .where(Field("author").exists())
  .select([Field("title"), Field("author")])
  .execute()

Kotlin

val results = db.pipeline()
    .collection("books")
    .where(field("category").like("%fantasy%"))
    .where(field("title").exists())
    .where(field("author").exists())
    .select(field("title"), field("author"))
    .execute()

Java

Task<Pipeline.Snapshot> results = db.pipeline()
    .collection("books")
    .where(field("category").like("%fantasy%"))
    .where(field("title").exists())
    .where(field("author").exists())
    .select(field("title"), field("author"))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

results = (
    client.pipeline()
    .collection("books")
    .where(Field.of("category").like("%fantasy%"))
    .where(Field.of("title").exists())
    .where(Field.of("author").exists())
    .select("title", "author")
    .execute()
)
Go
snapshot := client.Pipeline().
	Collection("books").
	Where(firestore.FieldOf("category").Like("%fantasy%")).
	Where(firestore.FieldOf("title").FieldExists()).
	Where(firestore.FieldOf("author").FieldExists()).
	Select(firestore.Fields("title", "author")).
	Execute(ctx)

(category [...], title [...], author [...])-এর জন্য books-এ ডেটাবেসে আগে থেকেই কালেকশন স্কোপ ইন্ডেক্স থাকলে, এটি মূল ডকুমেন্ট থেকে কোনও কিছু ফেচ করা এড়াতে পারে। এই ক্ষেত্রে, ইনডেক্সে ক্রমের কোনও গুরুত্ব নেই, [...] ব্যবহার করে সেটি বোঝানো হয়।

ফেরত পাওয়ার জন্য ফিল্ড সীমাবদ্ধ করুন

ডিফল্ট হিসেবে, Cloud Firestore কোয়েরি ডকুমেন্টের সব ফিল্ড রিটার্ন করে, যা রিলেশনাল সিস্টেমে SELECT *-এর মতো। তবে, আপনার অ্যাপ্লিকেশনের শুধুমাত্র ফিল্ডের সাবসেট প্রয়োজন হলে, সার্ভার-সাইডে এই ফিল্টারিং পুশ করার জন্য select(...) বা restrict(...) স্টেজ ব্যবহার করা যেতে পারে। এটি উত্তরের সাইজ কমাবে (নেটওয়ার্ক ইগ্রেস খরচ কমাবে) এবং লেটেন্সি উন্নত করবে।

সমস্যার সমাধানের জন্য টুল

কোয়েরি ব্যাখ্যা

Query Explain আপনাকে এক্সিকিউশন মেট্রিক্স এবং ব্যবহৃত ইন্ডেক্স সম্পর্কে বিশদ বিবরণ দেখতে দেয়।

মেট্রিক

আগে থেকে থাকা Cloud Firestore মেট্রিক্সের সাথে পাইপলাইন অপারেশন সম্পূর্ণভাবে ইন্টিগ্রেট করা হয়েছে।

পরিচিত সমস্যা / সীমাবদ্ধতা

বিশেষ ইন্ডেক্স

পাইপলাইন অপারেশন এখনও আগে থেকে থাকা array-contains ও vector ইনডেক্সের ধরন সাপোর্ট করে না। এই ধরনের কোয়েরি শুধু বাতিল করার পরিবর্তে, Cloud Firestore অন্যান্য আগে থেকে থাকা ascending ও descending ইন্ডেক্স ব্যবহার করার চেষ্টা করবে। এর ফলে, এই ধরনের array_contains বা find_nearest এক্সপ্রেশন তাদের বর্তমান সমতুল্য এক্সপ্রেশনের চেয়ে ধীরগতির হবে বলে আশা করা হচ্ছে।

রিয়েল-টাইম ও অফলাইন সহায়তা

পাইপলাইন অপারেশনে রিয়েল-টাইম ও অফলাইন ক্ষমতা নেই।

এর পরে কী করতে হবে