Class: Mongrep::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/mongrep/query.rb

Overview

A mongodb query object

Instance Method Summary collapse

Constructor Details

#initialize(query_hash = {}) ⇒ Query

Returns a new instance of Query.

Examples:

Query.new(name: 'test')

Parameters:

  • query_hash (Hash) (defaults to: {})

    A hash representing the query



8
9
10
# File 'lib/mongrep/query.rb', line 8

def initialize(query_hash = {})
  @query_hash = query_hash.to_h
end

Instance Method Details

#&(other) ⇒ Query

Combines two queries by merging their underlying query hashes

Examples:

first = Query.new(name: 'test', value: 5)
second = Query.new(value: 6)
(first & second).to_h #=> { name: 'test', value: 6 }

Parameters:

  • other (Query)

    The query to combine self with

Returns:

  • (Query)

    A new Query instance resulting in the combination of self and other



20
21
22
# File 'lib/mongrep/query.rb', line 20

def &(other)
  self.class.new(@query_hash.merge(other.to_h))
end

#and(query_hash) ⇒ Query

Combines self with the given query hash by using the MongoDB $and operator

Examples:

query.where(foo: { bar: 'foo' }).and(foo: { foo: 'bar' })

Parameters:

  • query_hash (Hash)

    The query hash to combine with the query

Returns:

  • (Query)

    A new Query resulting in the combination of the given query hash and the existing one



70
71
72
# File 'lib/mongrep/query.rb', line 70

def and(query_hash)
  self.class.new(:$and => [@query_hash, query_hash])
end

#or(query_hash) ⇒ Query

Combines self with the given query hash by using the MongoDB $or operator

Examples:

query.where(name: 'foo').or(name: 'bar')

Parameters:

  • query_hash (Hash)

    The query hash to combine with the query

Returns:

  • (Query)

    A new Query resulting in the combination of the given query hash and the existing one

See Also:



59
60
61
# File 'lib/mongrep/query.rb', line 59

def or(query_hash)
  self | self.class.new(query_hash)
end

#to_hHash

Returns The underlying query hash.

Returns:

  • (Hash)

    The underlying query hash



75
76
77
# File 'lib/mongrep/query.rb', line 75

def to_h
  @query_hash
end

#where(query_hash) ⇒ Query

Note:

This is mainly for usage in Repository#find using a block

Combines self with the given query hash by merging it to the existing one

Examples:

query.where(name: 'test')

Parameters:

  • query_hash (Hash)

    The query hash to merge into the query

Returns:

  • (Query)

    A new Query resulting in the combination of the given query hash and the existing one

See Also:



47
48
49
# File 'lib/mongrep/query.rb', line 47

def where(query_hash)
  self & self.class.new(query_hash)
end

#|(other) ⇒ Query

Combines two queries by using the MongoDB $or operator

Examples:

first = Query.new(name: 'foo')
second = Query.new(name: 'bar')
(first | second).to_h
#=> { :$or => [{ name: 'foo' }, { name: 'bar' }] }

Parameters:

  • other (Query)

    The query to combine self with

Returns:

  • (Query)

    A new Query instance resulting in the combination of self and other



33
34
35
# File 'lib/mongrep/query.rb', line 33

def |(other)
  self.class.new(:$or => [@query_hash, other.to_h])
end