Questions tagged [mongoid]

Mongoid is the official Ruby Object-Document-Mapper (ODM) for MongoDB.

Filter by
Sorted by
Tagged with
98 votes
3 answers
27k views

How to implement has_many :through relationships with Mongoid and mongodb?

Using this modified example from the Rails guides, how does one model a relational "has_many :through" association using mongoid? The challenge is that mongoid does not support has_many :through as ...
Mario Zigliotto's user avatar
87 votes
3 answers
43k views

Select where not null or empty in mongoid

I modified a model so it includes a new field, such as... field :url, :type => String I use activeadmin, so when I create a new entry @model.url is empty, and in entries created before changing ...
methodofaction's user avatar
85 votes
4 answers
99k views

Difference between "id" and "_id" fields in MongoDB

Is there any difference between using the field ID or _ID from a MongoDB document? I am asking this, because I usually use "_id", however I saw this sort({id:-1}) in the documentation: http://www....
Arthur Neves's user avatar
83 votes
14 answers
21k views

Mongoid or MongoMapper? [closed]

I have tried MongoMapper and it is feature complete (offering almost all AR functionality) but i was not very happy with the performance when using large datasets. Has anyone compared with Mongoid? ...
PanosJee's user avatar
  • 3,866
73 votes
2 answers
11k views

Using Active Record generators after Mongoid installation?

I'm using MongoDB via Mongoid integration, as well as ActiveRecord in a project. I would like to generate migrations for active record, and Mongoid is the default when I run. rails g migration Any ...
Allyl Isocyanate's user avatar
66 votes
3 answers
160k views

MongoDB: count the number of items in an array

I have a collection where every document in the collection has an array named foo that contains a set of embedded documents. Is there currently a trivial way in the MongoDB shell to count how many ...
randombits's user avatar
  • 47.8k
48 votes
7 answers
44k views

ERROR Original Error: ImageMagick/GraphicsMagick is not installed

I have an app that is using Imagemagick, but I had to rebuild my environment and now when I try and upload an avatar (which is why I am using Imagemagick), it keeps giving me the following error when ...
SupremeA's user avatar
  • 1,599
47 votes
5 answers
25k views

Mongoid OR query syntax

This must be asked alot but it is very poorly documented. There is no mention at http://mongoid.org/en/mongoid/docs/querying.html I'm trying to check whether a user exists (below is an AND query), ...
Tarang's user avatar
  • 75.6k
47 votes
3 answers
21k views

How can I disable MongoDB log messages in console?

I have this little test script: require 'mongo' mongo_client = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test') mongo_client[:collection].insert_one({a: 1}) An this is the console ...
raulmarcosl's user avatar
42 votes
6 answers
23k views

Finding mongoDB records in batches (using mongoid ruby adapter)

Using rails 3 and mongoDB with the mongoid adapter, how can I batch finds to the mongo DB? I need to grab all the records in a particular mongo DB collection and index them in solr (initial index of ...
Dan L's user avatar
  • 4,361
41 votes
4 answers
22k views

Batch insert/update using Mongoid?

I googled and all others, but I didn't find the answer. The question is: Hi, how can I do batch insert with Mongoid to MongoDB?
millisami's user avatar
  • 10.1k
41 votes
1 answer
10k views

Difference between embeds_many and has_many in mongoid

Can anyone explain to me the difference between embeds_many and has_many in mongoid?
enRai's user avatar
  • 671
41 votes
3 answers
11k views

How can I see raw mongoDB queries with Mongoid

I followed this page to see mongoDB queries. As a result I could see Moped log. But I can't see raw mongoDB queries. How can I display MongoDB queries in the rails console/server I did like the below....
Teruki Shinohara's user avatar
40 votes
5 answers
32k views

Mongoid Rails 4 sort by asc or desc order created_at

I have a rails 4 app using Mongoid. I want to do something basic is display the book model I have by descending order according to the field created_at in the index view. In the controller ...
Chleo's user avatar
  • 935
40 votes
4 answers
51k views

What is the length maximum for a string data type in MongoDB used with Ruby?

I wasn't able to find any information on the max length for a string data type in MongoDB on the main mongodb.org site. I'm coming from a relational database background and there are usually max ...
Goalie's user avatar
  • 3,075
37 votes
2 answers
11k views

Factory Girl + Mongoid embedded documents in fixtures

Let’s say you have the following mongoid documents: class User include Mongoid::Document embeds_one :name end class UserName include Mongoid::Document field :first field :...
GTDev's user avatar
  • 5,498
36 votes
3 answers
34k views

Is it bad to change _id type in MongoDB to integer?

MongoDB uses ObjectId type for _id. Will it be bad if I make _id an incrementing integer? (With this gem, if you're interested)
just so's user avatar
  • 1,118
36 votes
3 answers
7k views

Mongoid 3 + Heroku (MongoHQ) cause Moped::Errors::OperationFailure

With Rails 3, after upgrading from Mongoid 2.x to Mongoid 3.x, my Heroku + MongoHQ setup stopped working. Funny thing is, that my development & test frameworks and my whole test suite passes just ...
herb's user avatar
  • 840
33 votes
3 answers
61k views

How to deal with the timezone issue when storing dates in utc using mongod?

I have a mongodb collection where each document has some attributes and a utc timestamp. I need to pull out data from the collection and use the aggregation framework because I use the data from the ...
Hrishi's user avatar
  • 7,128
33 votes
4 answers
8k views

Mongo ids leads to scary URLs

This might sound like a trivial question, but it is rather important for consumer facing apps What is the easiest way and most scalable way to map the scary mongo id onto a id that is friendly? xx....
meow's user avatar
  • 27.8k
32 votes
7 answers
23k views

How to query MongoDB directly from Ruby instead of using Mongoid?

I am writing a migration for a Rails application that uses MongoDB and Mongoid. My migration currently uses my models that use Mongoid to query and update records, but the performance is sub-par. I am ...
Andrew's user avatar
  • 233k
32 votes
3 answers
7k views

Mongoid finders not working?

I have set up a rails3+mongoid application and when I open the rails console, none of the finders seem to work - http://d.pr/FNzC User.all User.find(:all, :conditions => { first_name => "John" }...
Hutch's user avatar
  • 841
31 votes
4 answers
14k views

Retrieving array of ids in Mongoid

how do you retrieve an array of IDs in Mongoid? arr=["id1","id2"] User.where(:id=>arr) You can do this easily if you are retrieving another attribute User.where(:nickname.in=>["kk","ll"]) ...
meow's user avatar
  • 27.8k
31 votes
1 answer
21k views

Mongoid / Mongodb and querying embedded documents

I have Author and Book models. An Author has many embedded Books. Can I query the embedded Books, or do I have to fetch Authors first to get Books?
sewid's user avatar
  • 341
31 votes
3 answers
3k views

Rails, Mongoid & Unicorn config for Heroku

I am using Mongoid 3, with Rails 3.2.9 and Unicorn for production. Would like to setup a before_fork & after_fork for the connection to mongodb, found the following code for active record: ...
ismail's user avatar
  • 3,902
29 votes
5 answers
17k views

Rails 3: undefined method `page' for #<Array:0xafd0660>

I can't get past this. I know I've read there isn't a page method for arrays but what do I do? If I run Class.all in the console, it returns #, but if I run Class.all.page(1), I get the above error. ...
Chris Bolton's user avatar
  • 2,918
28 votes
3 answers
9k views

How to get the resultant array from a Mongoid::Criteria without an "each" block

Our application uses ajax very heavily and as a result of this we have statements like var items = #{@items.to_json} in all our views. Now @items is being set in the controller as @items=Item.all. The ...
Khaja Minhajuddin's user avatar
28 votes
1 answer
16k views

Query on Mongoid Hash Field

I want to query on a Hash field for a Mongoid Class. I'm not sure how I can do this with conditions? Here is an example: class Person include Mongoid::Document field :things, :type => Hash ...
JP Richardson's user avatar
27 votes
3 answers
8k views

Set default database connection Rails

My rails app has its own MySql database (and requires the mysql2 gem) but also needs to connect with an external MongoDB database for one particular model (and so I've included mongoid and bson_ext in ...
Chris's user avatar
  • 12.1k
27 votes
1 answer
9k views

Mongoid Scope Check If Array Field Contains Value

The solution for this question might look pretty simple, but I've been waving around with no answer. I'm using Mongoid in my Rails 4.1.4 app. I have a model which contains an Array field that will ...
Luis Crespo's user avatar
  • 1,620
26 votes
3 answers
17k views

MongoID Data Type for TEXT

Whats the best option to use for MongoID data type for the regular MongoDB TEXT data type. Wondering why MongoID doesnt have a data type TEXT. is it okay to use STRING type and store large amounts ...
Harsha M V's user avatar
  • 54.5k
26 votes
1 answer
8k views

What inverse_of does mean in mongoid?

What inverse_of does mean in mongoid associations? What I can get by using it instead of just association without it?
freemanoid's user avatar
  • 14.7k
26 votes
3 answers
10k views

Managing mongoid migrations

Can someone give me a short introduction to doing DB migrations in Rails using Mongoid? I'm particularly interested in lazy per document migrations. By this, I mean that whenever you read a document ...
Paul Biggar's user avatar
  • 28.2k
26 votes
4 answers
8k views

How can I use Mongoid and ActiveRecord in parallel in Rails 3?

I'm using rails 3, and began my application with ActiveRecord. Now, I have many models, and the relations are starting to get complicated, and some could be more simply expressed with a Document-...
noli's user avatar
  • 16k
23 votes
5 answers
18k views

Use LIKE/regex with variable in mongoid

I'm trying to find all documents whose text contains the word test. The below works fine: @tweets = Tweet.any_of({ :text => /.*test.*/ }) However, I want to be able to search for a user supplied ...
Hinchy's user avatar
  • 683
22 votes
5 answers
13k views

How to skip callbacks on Mongoid Documents?

My question is similar to this one How to skip ActiveRecord callbacks? but instead of AR I'm using Mongoid, It seems like that isn't implemented yet in the current version of Mongoid, so I'd like to ...
jpemberthy's user avatar
  • 7,523
22 votes
2 answers
17k views

Mongoid not in query

I have some trouble with mongoid: test "Test candidate" do User.create(:id => 1, :sex => User::Male, :country => 1, :city => 1) User.create(:id => 2, :sex => User::Female, ...
Mark Pegasov's user avatar
  • 5,179
22 votes
6 answers
5k views

Pros and cons of using callbacks for domain logic in Rails

What do you see as the pros and cons of using callbacks for domain logic? (I'm talking in the context of Rails and/or Ruby projects.) To start the discussion, I wanted to mention this quote from the ...
David J.'s user avatar
  • 32.2k
22 votes
5 answers
11k views

How to reference an embedded document in Mongoid?

Using Mongoid, let's say I have the following classes: class Map include Mongoid::Document embeds_many :locations end class Location include Mongoid::Document field :x_coord, :type => ...
Scott Brown's user avatar
22 votes
2 answers
34k views

Collection ID length in MongoDB

i am new to mongodb and stack overflow. I want to know why on mongodb collection ID is of 24 hex characters? what is importance of that?
ashish bandiwar's user avatar
22 votes
1 answer
3k views

Heroku & MongoHQ: ActionView::Template::Error (The operation: #<Moped::Protocol::Commands::Authenticate failed with error "auth fails")

I have a Rails 3 app with MongoID 3 deployed successfully to Heroku (using MongoHQ) -- but then something happened (cannot pinpoint what exactly it was) and the build started crashing on Heroku. So ...
herb's user avatar
  • 840
21 votes
3 answers
6k views

Mongo DB Design, embedding vs relationships

I'm building a simple accounting system where a user has many bills. Now I'm trying to decide if bills should be its own collection, or nested within the user. I'm leaning towards the former but I'...
brad's user avatar
  • 32.1k
20 votes
4 answers
13k views

How can I get all field names of the Mongoid Document?

I'm building backend system, as written in Iain Hecker's tutorial: http://iain.nl/backends-in-rails-3-1 and I try to adapt it to MongoDB with Mongoid. So when I need to write in backend/...
Yuri Sidorov's user avatar
20 votes
6 answers
31k views

Creating short, unique object id's in MongoDB

I'm making an app similar to instagram using Rails/Mongoid. I want a unique ID that I can use in a url like http://instagr.am/p/DJmU8/ What's the easiest way to do that? Can I derive such an ID from ...
John's user avatar
  • 5,855
20 votes
3 answers
13k views

Querying embedded objects in Mongoid/rails 3 ("Lower than", Min operators and sorting)

I am using rails 3 with mongoid. I have a collection of Stocks with an embedded collection of Prices : class Stock include Mongoid::Document field :name, :type => String field :code, :type =&...
mathieurip's user avatar
19 votes
4 answers
17k views

Mongoid query for array field

I have a category field of type Array in Mongoid. Ex. category: ["val1","val2","val3"] Now I want to query this Model with `category: ["val1","val2"] such that it returns me the merge of ...
mrudult's user avatar
  • 2,540
19 votes
2 answers
8k views

Getting actual array of results using Mongoid

With a regular ActiveRecord/SQL setup in Rails, in console when I execute commands *.where, *.all etc., I get back the actual array of record items. However, after switching to Mongoid, I instead get ...
Newy's user avatar
  • 39.6k
19 votes
2 answers
8k views

See existing indexes in MongoDB using mongoid

I'd like to see the existing indexes used by MongoDB. Can I do the equivalent of $ mongod > use my_db > db.system.indexes.find() using Mongoid? $ rails console > ? Would be convenient ...
oma's user avatar
  • 39.7k
19 votes
5 answers
6k views

How to do proper database testing (TDD) on Rails 3 using MongoDB and Mongoid

How would go about writing proper unit testing (and integration testing for that matter) using MongoDB through Mongoid on Rails ? I am asking, because to the opposite of using let's say SQLite3, even ...
Alex's user avatar
  • 4,447
18 votes
5 answers
26k views

Querying last 30 days date range with Mongoid and Ruby?

How do I go about querying a date range (of say the last 30 days from now) with Mongoid and Ruby? I need to end up with an array or hash like the following: { 15 => 300, 14 => 23, ...
ghstcode's user avatar
  • 2,902

1
2 3 4 5
95