I have an array of made up of type BSON::ObjectId and I want it to compare against some IDs as strings.
if my_array_of_BSON_ObjectIds.include?(#my_id_as_a_string)
# delete the item from the array
else
# add the item to the array as a BSON::ObjectId
end
This is not working as the types are different, can I turn my string into a BSON::ObjectId? If so, how?
Mongoid 2.x with 10gen's driver:
BSON::ObjectId.new('506144650ed4c08d84000001')
Mongoid 3 with moped:
Moped::BSON::ObjectId.from_string('506144650ed4c08d84000001')
Mongoid 4 (moped) / Mongoid 5/6/7 (mongo):
BSON::ObjectId.from_string('506144650ed4c08d84000001')
You can use BSON::ObjectId(#my_id_as_a_string) for representation your id as BSON::ObjectId
refs http://api.mongodb.org/ruby/current/BSON.html#ObjectId-class_method
collection.delete_one({"_id"=>BSON::ObjectId(params['id'])})
This worked for me and it deleted the record from the database successfully
There's a shorter way:
BSON::ObjectId("id_here")
but for your case it would be easier to simply map the objects to ids before the comparison:
if my_array_of_BSON_ObjectIds..map(&:to_s).include?(#my_id_as_a_string)
Related
Ie, given a dataset object ds = DB[:transactions].where{updated_at > 1.day.ago} - no funny joins and stuff going on - how could I fetch the table name (:transactions) ?
If you want the first table in the dataset, you can use ds.first_source.
If you want it as a string you can do:
ds.first_source_table.to_s
If you want a symbol, just omit .to_s
Based on the example provided, I would do something like this.
ds.klass.name
That will return a string with the name of your table.
$visitorphonenumber=DB::select('select phonenumber from visitortable where email=?',[Auth::user()->email]);
$booking=new bookingmodel();
$booking->visitorphonenumber=$visitorphonenumber;
This stores the value like [{"phonenumber":"9874589608"}] in database but I want to store only the 9874589608
I will add some sugar to above answer, use like
$visitorphonenumber->first()->phonenumber;
try
$visitorphonenumber=DB::select('select phonenumber from visitortable where email=?',[Auth::user()->email]);
$singlePhoneNumber=$visitorphonenumber->first();
I'm using Mongoid to access a MongoDB database, however I'm running into an odd problem. It seems like I can only query for records using find_by, as find will always return nil:
invoices = Invoice.find({})
p "invoices"
p invoices
puts ''
invoice = Invoice.find_by({ _id: <ObjectId> })
p "invoice"
p invoice
puts ''
The second query using find_by will return a single record. According to the documentation, find should be returning every record that satisfies the query.
Does anyone have an idea what could be causing this?
Be careful not to confuse the Moped syntax with the Mongoid syntax. For Mongoid, the docs describe the find method:
Find a document or multiple documents by their ids. Will raise an error by default if any of the ids do not match
If you really want every record, Invoice.all can do the trick. (Also be careful with your find_by method. The Mongoid syntax varies from mongo's a bit, so you don't have to have the curlies around your params.)
I am using the pg_array extension and sequel version 4.1.1.
I have added the extension like this:
Sequel::Database.extension :pg_array
I have created a column like this:
alter_table :emails do
add_column :references, "text[]", null: true
end
I can load and retrieve arrays into a postgress array column, just like working with normal arrays.
What is not clear from the above link is how do I execute a query based on the values in this array column.
For example, if one row in the emails table contained these values in the references column:
references
--------------------------------------------------------------------
{}
{5363f773bccf9_32123fe75c45e6f090953#Pauls-MacBook-Pro.local.mail}
How can I query the emails table to find a row that contains a references array value of the above value:
Email.where(references: ????)
Use the pg_array_ops extension:
Sequel.extension :pg_array_ops
Email.where(Sequel.pg_array_op(:references).contains('5363f773bccf9_32123fe75c45e6f090953#Pauls-MacBook-Pro.local.mail'))
Have you tried?
ref = '5363f773bccf9'
emails = Email.arel_table
Email.where( emails[ :references ].matches( "%#{ref}%" ))
Say I have a mongoid document which has a field :pairs with the type of hash. When I wan't to query on the hash like this:
Doc.where(:pairs=>{"field1"=>1})
I get results back because I have in pairs a field with value one. I also have values for more than 1. When I do the following, nil is returned:
Doc.where(:pairs=>{"field1"=>{"$gt"=>0}})
This doesn't seem to work, and I do have pairs with key field1 and values bigger than 0. Can anyone provide me info on why this doesn't work?
Just try: Dco.where(:pairs.gt => 0)
For more detail mongoid querying see following link.
http://mongoid.org/en/mongoid/docs/querying.html
Doc.where('pairs.field1' => {"$gt"=>1})