class Easymongo::Query

Attributes

client[RW]

Public Class Methods

new(uri, options) click to toggle source

Set up connection

# File lib/easymongo/query.rb, line 19
def initialize(uri, options)
  self.client = Mongo::Client.new(uri, options)
end

Public Instance Methods

all(d = {}) click to toggle source

Get all

# File lib/easymongo/query.rb, line 85
def all(d = {})
  g!(d); cursor.to_a.map{|r| ed(r)}.tap{ c!}
end
count(d = {}) click to toggle source

Count

# File lib/easymongo/query.rb, line 90
def count(d = {})
  g!(d); cursor.count.tap{ c!}
end
fields(data, d = {}) click to toggle source

Fields

# File lib/easymongo/query.rb, line 65
def fields(data, d = {})
  g!(d); s[:cursor] = cursor.projection(data); self
end
first(d = {}) click to toggle source

Get first

# File lib/easymongo/query.rb, line 75
def first(d = {})
  g!(d); cursor.first.tap{|r| return ed(r) if r; c!}
end
get(data = {}) click to toggle source

Get values, store cursor in thread

# File lib/easymongo/query.rb, line 50
def get(data = {})
  s[:cursor] = client[coll].find(ids(data)); self
end
ids(data) click to toggle source

Make sure data is optimal

# File lib/easymongo/query.rb, line 108
def ids(data)

  # Just return if nothing to do
  return data if data and data.empty?

  # Support passing id as string
  data = {'_id' => data} if !data or data.is_a?(String)

  # Turn all keys to string
  data = data.stringify_keys

  # Convert id to _id for mongo
  data['_id'] = data.delete('id') if data['id']

  # Convert ids to BSON ObjectId
  data.each do |k, v|
    if v.is_a?(String) and v =~ /^[0-9a-fA-F]{24}$/
      data[k] = oid(v)
    end
  end

  # Return data
  data
end
last(d = {}) click to toggle source

Get last

# File lib/easymongo/query.rb, line 80
def last(d = {})
  g!(d); cursor.sort(:$natural => -1).first.tap{|r| return ed(r) if r; c!}
end
limit(n, d = {}) click to toggle source

Limit

# File lib/easymongo/query.rb, line 55
def limit(n, d = {})
  g!(d); s[:cursor] = cursor.limit(n.to_i); self
end
method_missing(name, *args, &block) click to toggle source

Set up collection, stored in the thread

# File lib/easymongo/query.rb, line 24
def method_missing(name, *args, &block)
  c!; s[:coll] = name; self
end
oid(v = nil) click to toggle source

Convert to BSON ObjectId or make a new one

# File lib/easymongo/query.rb, line 134
def oid(v = nil)
  return BSON::ObjectId.new if v.nil?; BSON::ObjectId.from_string(v) rescue v
end
rm(data) click to toggle source

Remove

# File lib/easymongo/query.rb, line 95
def rm(data)

  # Optimize data
  data = ids(data)

  # Delete doc
  result = client[coll].delete_one(data)

  # Return result
  Easymongo::Result.new(result).tap{ c!}
end
set(*args) click to toggle source

Set values

# File lib/easymongo/query.rb, line 29
def set(*args)

  # Insert, add oid
  data, values = args.size == 1 ? [{'_id' => oid}, *args] : args

  # Normalize attributes
  data, values = ids(data), ids(values)

  # Using set and unset so we don't store nil in the db
  options = {
    :$set => values.select{|k, v| !v.nil?}, :$unset => values.select{|k, v| v.nil?}
  }.delete_if{|k, v| v.empty?}

  # Update the collection
  result = client[coll].update_one(data, options, :upsert => true)

  # Return result
  Easymongo::Result.new(result)
end
skip(n, d = {}) click to toggle source

Skip

# File lib/easymongo/query.rb, line 60
def skip(n, d = {})
  g!(d); s[:cursor] = cursor.skip(n.to_i); self
end
sort(data, d = {}) click to toggle source

Sort

# File lib/easymongo/query.rb, line 70
def sort(data, d = {})
  g!(d); s[:cursor] = cursor.sort(data); self
end

Private Instance Methods

c!() click to toggle source

Clear request store

# File lib/easymongo/query.rb, line 144
def c!; s[:cursor] = s[:coll] = nil; end
coll() click to toggle source

Get the collection

# File lib/easymongo/query.rb, line 150
def coll; s[:coll]; end
cursor() click to toggle source

Get the cursor

# File lib/easymongo/query.rb, line 153
def cursor; s[:cursor]; end
ed(r) click to toggle source

Short cut for creating documents

# File lib/easymongo/query.rb, line 156
def ed(r); Easymongo::Document.new(r); end
g!(d = {}) click to toggle source

Run get if no cursor

# File lib/easymongo/query.rb, line 147
def g!(d = {}); get(d) unless cursor; end
s() click to toggle source

Get the request store

# File lib/easymongo/query.rb, line 141
def s; RequestStore.store; end