Mongoid multiple atomic operations - ruby

I have a kind of Mongoid::Document in memory. I want to atomically inc and push in the same call. Mongoid::Criteria only exposes these operations individually.
my_model = SomeModel.find "foo"
Bad:
my_model.inc foo: 1
my_model.push bar: "b"
Good:
my_model.underlying_adapter.update "$inc" => {foo: 1}, "$push" => {bar: "b"}
The question is, how do I access that underlying adapter for a single instance of a Mongoid::Document?

You can use moped (the ruby adapter which mongoid uses) directly for this and other complex atomic operations which you want to achieve in a single query:
SomeModel.collection.find("_id" => "foo").update({
'$inc' => {"foo" => 1},
'$push' => {"bar" => "b"}})

I don't know what do you mean by atomically? timeless and without validation?
your_model.inc_field = your_model.inc_field + inc_value
your_model.push_field << pushable
your_model.timeless.save(validate: false)
P.S : Mongoid inc and push (and almost all other atomic operations) are at least 2 database hits (one read and one update).
Edit
If you want more advance query and command execution with mongoid you can use Moped (Mongoid uses Moped)
Moped driver documentation
Edit 2
It's limitation of mongoid (until current stable release - 3.1.6), refer to this issue - In version 4 of mongoid (not released yet) user can do single write operation atomically (and also chainable) Mongoid Changelog ISSUE#1344
From your code, I see you have model in memory it's at least 2 DB hit, 1 for read and 1 for write (even with chained atomic operations), Mongoid4 fixed that (1 DB hit with a single atomic operation, e.g : Band.where(name: "Depeche Mode").inc(likes: 10, followers: 20) or with document.atomically &:block syntax)
All write operations in MongoDB are atomic on the level of a single document. MongoDB Docs

I'm not a ruby expert, but maybe you can use find_and_modify

Related

Ruby on Rails ActiveRecord filter issue [duplicate]

I am working on an app that allows Members to take a survey (Member has a one to many relationship with Response). Response holds the member_id, question_id, and their answer.
The survey is submitted all or nothing, so if there are any records in the Response table for that Member they have completed the survey.
My question is, how do I re-write the query below so that it actually works? In SQL this would be a prime candidate for the EXISTS keyword.
def surveys_completed
members.where(responses: !nil ).count
end
You can use includes and then test if the related response(s) exists like this:
def surveys_completed
members.includes(:responses).where('responses.id IS NOT NULL')
end
Here is an alternative, with joins:
def surveys_completed
members.joins(:responses)
end
The solution using Rails 4:
def surveys_completed
members.includes(:responses).where.not(responses: { id: nil })
end
Alternative solution using activerecord_where_assoc:
This gem does exactly what is asked here: use EXISTS to to do a condition.
It works with Rails 4.1 to the most recent.
members.where_assoc_exists(:responses)
It can also do much more!
Similar questions:
How to query a model based on attribute of another model which belongs to the first model?
association named not found perhaps misspelled issue in rails association
Rails 3, has_one / has_many with lambda condition
Rails 4 scope to find parents with no children
Join multiple tables with active records
You can use SQL EXISTS keyword in elegant Rails-ish manner using Where Exists gem:
members.where_exists(:responses).count
Of course you can use raw SQL as well:
members.where("EXISTS" \
"(SELECT 1 FROM responses WHERE responses.member_id = members.id)").
count
You can also use a subquery:
members.where(id: Response.select(:member_id))
In comparison to something with includes it will not load the associated models (which is a performance benefit if you do not need them).
If you are on Rails 5 and above you should use left_joins. Otherwise a manual "LEFT OUTER JOINS" will also work. This is more performant than using includes mentioned in https://stackoverflow.com/a/18234998/3788753. includes will attempt to load the related objects into memory, whereas left_joins will build a "LEFT OUTER JOINS" query.
def surveys_completed
members.left_joins(:responses).where.not(responses: { id: nil })
end
Even if there are no related records (like the query above where you are finding by nil) includes still uses more memory. In my testing I found includes uses ~33x more memory on Rails 5.2.1. On Rails 4.2.x it was ~44x more memory compared to doing the joins manually.
See this gist for the test:
https://gist.github.com/johnathanludwig/96fc33fc135ee558e0f09fb23a8cf3f1
where.missing (Rails 6.1+)
Rails 6.1 introduces a new way to check for the absence of an association - where.missing.
Please, have a look at the following code snippet:
# Before:
Post.left_joins(:author).where(authors: { id: nil })
# After:
Post.where.missing(:author)
And this is an example of SQL query that is used under the hood:
Post.where.missing(:author)
# SELECT "posts".* FROM "posts"
# LEFT OUTER JOIN "authors" ON "authors"."id" = "posts"."author_id"
# WHERE "authors"."id" IS NULL
As a result, your particular case can be rewritten as follows:
def surveys_completed
members.where.missing(:response).count
end
Thanks.
Sources:
where.missing official docs.
Pull request.
Article from the Saeloun blog.
Notes:
where.associated - a counterpart for checking for the presence of an association is also available starting from Rails 7.
See offical docs and this answer.

Ruby REDIS with EX AND NX

I am trying to implement a locking in our rails application server.
REDIS.setnx works fine if I want to acquire a lock forever. But I want to acquire lock with expiry, basically I want the lock to expire after certain duration so that lock will be free to be acquired again.
From REDIS's set documentation, I see it is possible. https://redis.io/commands/set
"The command SET resource-name anystring NX EX max-lock-time is a simple way to implement a locking system with Redis."
How to implement this in ruby.
Command :
REDIS = Redis.new(host: ENV['REDIS_HOST'], port: ENV['REDIS_PORT'])
REDIS.set "key", "value", "nx", "ex", 3600
throws error:
ArgumentError: wrong number of arguments (given 5, expected 2..3)
There is another way to do that, but it requires two REDIS calls.
if(REDIS.setnx "key", "value")
REDIS.setex "key", 3600, "value"
end
This method is not preferred. I am looking to a way to acquire REDIS lock with single REDIS call in ruby. Basically "SET resource-name anystring NX EX max-lock-time" equivalent in ruby.
Thanks,
Anshul
It looks like this was added in Redis(the gem) in v3.2.2, see PR 547.
It should be used like a flag rather than as a bare string, see test.
r.set("foo", "qux", :nx => true)
Which leads me to believe you should be able to do this:
r.set("foo", "qux", :nx => true, :ex => 2.hours)

How to do an upsert / push with mongoid / moped

I'm using Mongoid (v3) to access MongoDB, and want to perform this action:
db.sessionlogs.update(
{sessionid: '12345'}, /* selection criteria */
{'$push':{rows: "new set of data"}}, /* modification */
true /* upsert */
);
This works fine in the mongo shell. It's also exactly what I want since it's a single atomic operation which is important to me as I'm going to be calling it a lot. I don't want to have to do two operations -- a fetch and then an update. I've tried a bunch of things through mongoid, but can't get it to work.
How can I get MongoID out of the way and just send this command to MongoDB? I'm guessing there's some way to do this at the Moped level, but the documentation of that library is basically non-existent.
[Answer found while writing the question...]
criteria = Sessionlogs.collection.find(:sessionid => sessionid)
criteria.upsert("$push" => {"rows" => datarow})
Here is one way to do it:
session_log = SessionLog.new(session_id: '12345')
session_log.upsert
session_log.push(:rows, "new set of data")
Or another:
SessionLog.find_or_create_by(session_id: '12345').
push(:rows, "new set of data")
#push performs an atomic $push on the field. It is explained on the
Atomic Persistence page.
(Note: the examples use UpperCamelCase and snake_case as is Ruby convention.)
Don't go down to moped just yet, you can use find and modify operation to achieve the same thing (with all the default scope and inheritance goodies)
Sample to save an edge in a graph if not existed
edge = {source_id: session[:user_id],dest_id:product._id, name: edge_name}
ProductEdge.where(edge).find_and_modify(ProductEdge.new(edge).as_document,{upsert:true})

How to fire raw MongoDB queries directly in Ruby

Is there any way that I can fire a raw mongo query directly in Ruby instead of converting them to the native Ruby objects?
I went through Ruby Mongo Tutorial, but I cannot find such a method anywhere.
If it were mysql, I would have fired a query something like this.
ActiveRecord::Base.connection.execute("Select * from foo")
My mongo query is a bit large and it is properly executing in the MongoDB console. What I want is to directly execute the same inside Ruby code.
Here's a (possibly) better mini-tutorial on how to get directly into the guts of your MongoDB. This might not solve your specific problem but it should get you as far as the MongoDB version of SELECT * FROM table.
First of all, you'll want a Mongo::Connection object. If
you're using MongoMapper then you can call the connection
class method on any of your MongoMapper models to get a connection
or ask MongoMapper for it directly:
connection = YourMongoModel.connection
connection = MongoMapper.connection
Otherwise I guess you'd use the from_uri constructor to build
your own connection.
Then you need to get your hands on a database, you can do this
using the array access notation, the db method, or get
the current one straight from MongoMapper:
db = connection['database_name'] # This does not support options.
db = connection.db('database_name') # This does support options.
db = MongoMapper.database # This should be configured like
# the rest of your app.
Now you have a nice shiny Mongo::DB instance in your hands.
But, you probably want a Collection to do anything interesting
and you can get that using either array access notation or the
collection method:
collection = db['collection_name']
collection = db.collection('collection_name')
Now you have something that behaves sort of like an SQL table so
you can count how many things it has or query it using find:
cursor = collection.find(:key => 'value')
cursor = collection.find({:key => 'value'}, :fields => ['just', 'these', 'fields'])
# etc.
And now you have what you're really after: a hot out of the oven Mongo::Cursor
that points at the data you're interested in. Mongo::Cursor is
an Enumerable so you have access to all your usual iterating
friends such as each, first, map, and one of my personal
favorites, each_with_object:
a = cursor.each_with_object([]) { |x, a| a.push(mangle(x)) }
There are also command and eval methods on Mongo::DB that might do what you want.
In case you are using mongoid you will find the answer to your question here.
If you're using Mongoid 3, it provides easy access to its MongoDB driver: Moped. Here's an example of accessing some raw data without using Models to access the data:
db = Mongoid::Sessions.default
# inserting a new document
collection = db[:collection_name]
collection.insert(name: 'my new document')
# finding a document
doc = collection.find(name: 'my new document').first
# "select * from collection"
collection.find.each do |document|
puts document.inspect
end

Count operation with parameters with mongodb ruby driver

I was trying to use the count() Mongodb feature (db.collection_name.count({data:value}) using the Ruby Driver. I tried using de collection.count method, but it don't accept any parameters.
I checked the collection.count() method docs, it only returns the total amount of objects in the collection, where is no way you can pass a "filter" parameter to this method.
Is it possible use the count() Mongodb feature with filter parameters in some other way?
Is it possible use the count() Mongodb feature with filter parameters in some other way?
From the shell (command-line), you can do the following:
db.collection.find({ data : value}).count()
Obviously, you'll have to do something similar with Ruby, but it should be pretty straightforward.
For those who just want the answer using the ruby driver:
# actual number of records
DB["posts"].find({"author" => "john"}).to_a.length
=> 48
# *total* number of documents in the collection (the query matching is ignored)
DB["posts"].count({"author" => "john"})
=> 1431
# more efficient way (using the count server-side command)
DB["posts"].find({"author" => "john"}).count
=> 1431
It's actually possible to use .count() as you do on the command line, it's just poorly documented.
Mongo command line:
db.User.find({"emails.5": {"$exists": true}}).count()
Mongo Ruby driver:
db.collection('User').count({:query => {"emails.5" => {"$exists" => true}}})
This and other .count() options are document here.

Resources