नमूना

ब्यौरा

यह पिछले चरण के नतीजों से, बिना किसी क्रम के नमूने को लौटाता है.

इसके दो मोड हैं:

  • documents: इसमें n दस्तावेज़ों को रैंडम तरीके से चुना जाता है.
  • percent: इसमें n प्रतिशत दस्तावेज़ों को रैंडम तरीके से चुना जाता है.

उदाहरण

Web

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

// Get a sample of 100 documents in a database
results = try await db.pipeline()
  .database()
  .sample(count: 100)
  .execute()

// Randomly shuffle a list of 3 documents
results = try await db.pipeline()
  .documents([
    db.collection("cities").document("SF"),
    db.collection("cities").document("NY"),
    db.collection("cities").document("DC"),
  ])
  .sample(count: 3)
  .execute()

Kotlin

var results: Task<Pipeline.Snapshot>

// Get a sample of 100 documents in a database
results = db.pipeline()
    .database()
    .sample(100)
    .execute()

// Randomly shuffle a list of 3 documents
results = db.pipeline()
    .documents(
        db.collection("cities").document("SF"),
        db.collection("cities").document("NY"),
        db.collection("cities").document("DC")
    )
    .sample(3)
    .execute()

Java

Task<Pipeline.Snapshot> results;

// Get a sample of 100 documents in a database
results = db.pipeline()
    .database()
    .sample(100)
    .execute();

// Randomly shuffle a list of 3 documents
results = db.pipeline()
    .documents(
        db.collection("cities").document("SF"),
        db.collection("cities").document("NY"),
        db.collection("cities").document("DC")
    )
    .sample(3)
    .execute();
Python
# Get a sample of 100 documents in a database
results = client.pipeline().database().sample(100).execute()

# Randomly shuffle a list of 3 documents
results = (
    client.pipeline()
    .documents(
        client.collection("cities").document("SF"),
        client.collection("cities").document("NY"),
        client.collection("cities").document("DC"),
    )
    .sample(3)
    .execute()
)
Java
// Get a sample of 100 documents in a database
Pipeline.Snapshot results1 = firestore.pipeline().database().sample(100).execute().get();

// Randomly shuffle a list of 3 documents
Pipeline.Snapshot results2 =
    firestore
        .pipeline()
        .documents(
            firestore.collection("cities").document("SF"),
            firestore.collection("cities").document("NY"),
            firestore.collection("cities").document("DC"))
        .sample(3)
        .execute()
        .get();

मोड

दस्तावेज़ मोड

documents मोड, इनपुट में से n दस्तावेज़ों को रैंडम तरीके से चुनता है. इसमें हर दस्तावेज़ (और दस्तावेज़ों का क्रम) के चुने जाने की संभावना बराबर होती है. ऐसा करने के लिए, Cloud Firestore को सभी दस्तावेज़ों को स्कैन और प्रोसेस करना पड़ता है. इसलिए, यह एक महंगा ऑपरेशन हो सकता है.

उदाहरण के लिए, यहां दिए गए कलेक्शन के लिए:

Node.js

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

दस्तावेज़ मोड में, सैंपल स्टेज का इस्तेमाल करके इस कलेक्शन से नतीजों का बिना किसी क्रम वाला सबसेट वापस पाया जा सकता है.

Node.js

const sampled = await db.pipeline()
    .collection("/cities")
    .sample(1)
    .execute();

इस उदाहरण में, सिर्फ़ एक दस्तावेज़ को रैंडम तरीके से वापस पाया जाएगा.

  { name: "New York City", state: "New York" }

अगर दी गई संख्या, वापस पाए गए दस्तावेज़ों की कुल संख्या से ज़्यादा है, तो सभी दस्तावेज़ों को रैंडम क्रम में वापस पाया जाएगा.

Node.js

const sampled = await db.pipeline()
    .collection("/cities")
    .sample(5)
    .execute();

इससे ये दस्तावेज़ मिलेंगे:

  { name: "New York City", state: "New York" }
  { name: "Chicago", state: "Illinois" }
  { name: "San Francisco", state: "California" }

प्रतिशत मोड

percent मोड, इनपुट में से सभी दस्तावेज़ों के n प्रतिशत को चुनने की कोशिश करता है. इससे स्टेज में, करीब-करीब # documents * percent / 100 दस्तावेज़ मिलते हैं. documents मोड की तरह, Cloud Firestore यह पक्का करता है कि हर दस्तावेज़ के वापस पाए जाने की संभावना बराबर हो. इसके लिए, Cloud Firestore को सभी दस्तावेज़ों को स्कैन और प्रोसेस करना पड़ता है. इसलिए, यह एक महंगा ऑपरेशन हो सकता है. भले ही, नतीजों का सेट छोटा हो.

documents मोड के उलट, यहां क्रम रैंडम नहीं होता. इसके बजाय, दस्तावेज़ों का पहले से मौजूद क्रम बना रहता है. प्रतिशत में दिया गया इनपुट, 0.0 और 1.0 के बीच की डबल वैल्यू होनी चाहिए.

उदाहरण के लिए, यहां दिए गए कलेक्शन के लिए:

Node.js

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

प्रतिशत मोड में, सैंपल स्टेज का इस्तेमाल करके collection(...) स्टेज से (औसतन) 50% दस्तावेज़ वापस पाए जा सकते हैं.

Node.js

  const sampled = await db.pipeline()
    .collection("/cities")
    .sample({ percent: 0.5 })
    .execute();

इससे cities कलेक्शन से (औसतन) 50% दस्तावेज़ों का बिना किसी क्रम वाला सैंपल मिलेगा. यहां एक संभावित आउटपुट दिया गया है.

  { name: "New York City", state: "New York" }
  { name: "Chicago", state: "Illinois" }

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

क्लाइंट के उदाहरण

Web

// Get a sample of on average 50% of the documents in the database
const results = await execute(db.pipeline()
  .database()
  .sample({ percentage: 0.5 })
);
Swift
// Get a sample of on average 50% of the documents in the database
let results = try await db.pipeline()
  .database()
  .sample(percentage: 0.5)
  .execute()

Kotlin

// Get a sample of on average 50% of the documents in the database
val results = db.pipeline()
    .database()
    .sample(SampleStage.withPercentage(0.5))
    .execute()

Java

// Get a sample of on average 50% of the documents in the database
Task<Pipeline.Snapshot> results = db.pipeline()
    .database()
    .sample(SampleStage.withPercentage(0.5))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_stages import SampleOptions

# Get a sample of on average 50% of the documents in the database
results = (
    client.pipeline().database().sample(SampleOptions.percentage(0.5)).execute()
)
Java
// Get a sample of on average 50% of the documents in the database
Pipeline.Snapshot results =
    firestore.pipeline().database().sample(Sample.withPercentage(0.5)).execute().get();