Questions tagged [mongoid4]

Version 4 of Mongoid, an Object-Document-Mapper (ODM) for MongoDB with Ruby. Use this tag for questions relating to this specific version.

Version 4 of Mongoid (an Object-Document-Mapper (ODM) for MongoDB)), which is compatible with Rails 4. For a deeper description of Mongoid, see the main Mongoid tag.

Primary tag:

Homepage

104 questions
14
votes
2 answers

setting mongoid hash field values

I'm using Mongoid in a Rails project (both 4.0.x), and I've got a document with a hash field that stores some schema-less data. class Thing field :name, type: String field :mass, type: Integer field :info, type: Hash end With this setup, I…
Bee
  • 14,277
  • 6
  • 35
  • 49
8
votes
1 answer

How to switch MongoDB database on the fly while using db.collection.insert()?

I have a multi-domain Rails 4 app where the request.domain of the http request determines which functionality I expose a given visitor to. Each domain in my app should be served by its own MongoDB database. E.g. domain1.com is served by…
Cjoerg
  • 1,271
  • 3
  • 21
  • 63
5
votes
0 answers

Mongoid, Update parent and child at the same time

I have the following model structure: a Banner embeds_many Slides and each Slide embeds_many contents class Banner include Mongoid::Document embeds_many :slides accepts_nested_attributes_for :slides, allow_destroy: true end class Slide …
4
votes
2 answers

How to get a spec test passing for model with enum field type - Mongoid

EDIT: As per @max suggestion, I'm changing my model to use enum instead, however I can't test it for default state: it { is_expected.to validate_inclusion_of(:status).to_allow("draft", "published") } Works fine with the following code in the…
WagnerMatosUK
  • 4,309
  • 7
  • 56
  • 95
3
votes
1 answer

Rails 4: Use MySql and MongoDB together

I'm trying to make an application in rails 4 using MongoDB(mongoid) and MySQL together. But I'm not able to set it up. I'm following the steps below: rails new myapp -d mysql Then added these lines to Gemfile: gem "mongoid" gem "bson_ext" bundle…
Regius
  • 70
  • 1
  • 9
3
votes
2 answers

How to query multiple ids in Mongoid?

If I have a few IDs, say [1,2,3] Is it possible to query them all at once in Mongoid? Such as: User.where({ id: [1,2,3]}) or something similar?
kidcapital
  • 5,064
  • 9
  • 46
  • 68
3
votes
1 answer

Mongoid batch collection insert: how to get the ids of the newly created items?

As explained here, it's fairly easy to batch insert an array of new documents into a MongoDB collection: batch = [{:name => "mongodb"}, {:name => "mongoid"}] Article.collection.insert(batch) What I don't find easy though is how to retrieve the…
lucke84
  • 4,516
  • 3
  • 37
  • 58
3
votes
1 answer

Mongoid has_and_belongs_to_many without '_ids' suffix?

In the following example: class Band include Mongoid::Document has_and_belongs_to_many :tags end class Tag include Mongoid::Document field :name, type: String has_and_belongs_to_many :bands end The objects are stored like this: # The…
3
votes
1 answer

Mongoid 'or' and 'in' - querying multiple arrays

I have a document in Mongo with two array fields: field :app_usernames, type: Array field :email_addresses, type: Array I'd like to create a function which take an array of usernames and an array of email addresses to search…
JimmyP
  • 115
  • 12
3
votes
1 answer

Mongoid 4 update array attribute in a document

What is the mistake in this code for not be able to update an array within a document? Model class Foo include Mongoid::Document include Mongoid::Timestamps::Created field :myarray, type: Array end Controller def add_item @foo =…
Fran b
  • 3,016
  • 6
  • 38
  • 65
3
votes
1 answer

setting default hash keys for mongoid hash field

I have a Mongoid model with a hash field. This Mongoid model has subclasses using single collection inheritance. Now I want to set different default hash keys for each subclass of the main model. The main model class Sport include…
brg
  • 3,915
  • 8
  • 37
  • 66
2
votes
1 answer

Bitnami Mongodb ReplicaSet Set Issue on MS Azure - Secondary node late response

I have created Mongodb with Replica Set on Azure. I have rails application hitting replica set on public ip. My replica set have 1 primary and 2 secondary node. I am facing extreme lag in response from secondary nodes but very fast response from…
2
votes
1 answer

Model.find is not working with mongoid

I am not able find the record using Model.find(id). Food.find('548210e8d5a81037af06b2d6') => Mongoid::Errors::DocumentNotFound But when I try find the same record using column name , I will return same record. Food.where({name:"Aloo Matar"…
Hare Ram
  • 743
  • 2
  • 7
  • 19
2
votes
1 answer

Rails deployment application version specific update script

I am deploying regular updates for my Rails application with Capistrano and I keep a version number as a string in a global constant. I would like to be a able to deploy/run a simple script only once for a specific update, that will perform some…
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
2
votes
0 answers

How to enforce a belong_to only if the user is of a certain type (using Enum, Mongoid)

I'm using Mongoid and Mongoid-Enum. Here's my User model snippet: enum :role, [:admin, :artist, :customer], :validate => true, :default => :admin For my Voice model I wrote a test (using an example in Mongoid-rspec website) such as: it {…
WagnerMatosUK
  • 4,309
  • 7
  • 56
  • 95
1
2 3 4 5 6 7