Class: Google::Cloud::Firestore::FieldPath

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/google/cloud/firestore/field_path.rb

Overview

FieldPath

Represents a field path to the Firestore API. See Client#field_path.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

user_snap = firestore.doc("users/frank").get

nested_field_path = Google::Cloud::Firestore::FieldPath.new(
  :favorites, :food
)
user_snap.get(nested_field_path) #=> "Pizza"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*fields) ⇒ FieldPath

Creates a field path object representing a nested field for document data.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

user_snap = firestore.doc("users/frank").get

nested_field_path = Google::Cloud::Firestore::FieldPath.new(
  :favorites, :food
)
user_snap.get(nested_field_path) #=> "Pizza"

Parameters:

  • fields (String, Symbol, Array<String|Symbol>)

    One or more strings representing the path of the data to select. Each field must be provided separately.



63
64
65
66
67
68
# File 'lib/google/cloud/firestore/field_path.rb', line 63

def initialize *fields
  @fields = fields.flatten.map(&:to_s)
  @fields.each do |field|
    raise ArgumentError, "empty paths not allowed" if field.empty?
  end
end

Class Method Details

.document_idFieldPath

Creates a field path object representing the sentinel ID of a document. It can be used in queries to sort or filter by the document ID. See Client#document_id.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Get a collection reference
cities_col = firestore.col "cities"

# Create a query
query = cities_col.start_at("NYC").order(
  Google::Cloud::Firestore::FieldPath.document_id
)

query.get do |city|
  puts "#{city.document_id} has #{city[:population]} residents."
end

Returns:



140
141
142
# File 'lib/google/cloud/firestore/field_path.rb', line 140

def self.document_id
  new :__name__
end