कलेक्शन ग्रुप

ब्यौरा

यह सुविधा, किसी भी कलेक्शन के सभी दस्तावेज़ों को वापस लाती है. इसके लिए, कलेक्शन की आईडी की जानकारी देनी होती है. भले ही, वह किसी भी पैरंट कलेक्शन का हिस्सा हो.

उदाहरण

Web

const results = await execute(db.pipeline()
  .collectionGroup("games")
  .sort(field("name").ascending())
  );
Swift
let results = try await db.pipeline()
  .collectionGroup("games")
  .sort([Field("name").ascending()])
  .execute()

Kotlin

val results = db.pipeline()
    .collectionGroup("games")
    .sort(field("name").ascending())
    .execute()

Java

      Task<Pipeline.Snapshot> results = db.pipeline()
    .collectionGroup("games")
    .sort(field("name").ascending())
    .execute();
    
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

results = (
    client.pipeline()
    .collection_group("games")
    .sort(Field.of("name").ascending())
    .execute()
)
Java
Pipeline.Snapshot results =
    firestore
        .pipeline()
        .collectionGroup("games")
        .sort(ascending(field("name")))
        .execute()
        .get();
Go
snapshot := client.Pipeline().
	CollectionGroup("games").
	Sort(firestore.Orders(firestore.Ascending(firestore.FieldOf("name")))).
	Execute(ctx)

व्यवहार

collection_group(...) स्टेज का इस्तेमाल करने के लिए, इसे पाइपलाइन में पहले स्टेज के तौर पर शामिल करना होगा.

collection_group(...) स्टेज से मिले दस्तावेज़ों का क्रम तय नहीं होता. इसलिए, इस पर भरोसा नहीं किया जा सकता. Cloud Firestore क्वेरी को सबसे असरदार तरीके से चलाने की कोशिश करेगा. इससे स्कीमा या इंडेक्स कॉन्फ़िगरेशन के हिसाब से, क्रम में बदलाव हो सकता है. क्रम को तय करने के लिए, इसके बाद sort(...) स्टेज का इस्तेमाल किया जा सकता है.

उदाहरण के लिए, इन दस्तावेज़ों के लिए:

Node.js

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});

collection_group(...) स्टेज का इस्तेमाल करके, डेटाबेस में मौजूद सभी पैरंट कलेक्शन में, हर डिपार्टमेंट के कलेक्शन से दस्तावेज़ वापस लाए जा सकते हैं.

Node.js

const results = await db.pipeline()
  .collectionGroup("departments")
  .sort(field("employees").ascending())
  .execute();

इस क्वेरी से ये दस्तावेज़ मिलते हैं:

  { name: "SF Building Deparment", employees: 750 }
  { name: "CHI Building Deparment", employees: 900 }
  { name: "NY Building Deparment", employees: 1000 }
  { name: "NY Finance Deparment", employees: 1200 }