Questions tagged [mongoid]

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

Mongoid (pronounced mann-goyd) is an Object-Document-Mapper (ODM) for MongoDB written in Ruby. Mongoid as of version 5 is under the control and guidance of MongoDB as the official Ruby ODM.

The philosophy of Mongoid is to provide a familiar API to Ruby developers who have been using Active Record or Data Mapper, while leveraging the power of MongoDB's schema-less and performant document-based design, dynamic queries, and atomic modifier operations.

4728 questions
19
votes
6 answers

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 the default BSON ObjectID Mongo creates?
John
  • 5,835
  • 8
  • 28
  • 36
19
votes
5 answers

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 when running tests, everything I do does…
Alex
  • 4,367
  • 5
  • 29
  • 45
19
votes
4 answers

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 Model.where(category: "val1") and…
mrudult
  • 2,480
  • 3
  • 35
  • 54
19
votes
2 answers

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 from my heroku apps using MongoHQ. Thanks!
oma
  • 38,642
  • 11
  • 71
  • 99
18
votes
5 answers

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, 13 => 23 ... 30 => 20 # Goes over into previous…
ghstcode
  • 2,902
  • 1
  • 20
  • 30
18
votes
2 answers

Mongoid on RoR3: 1) how to return specific field on query? 2) what inverse_of is needed for?

Well, the title is self-explicative. But, let me elaborate a little better. First of all, I am using Mongoid, a Gem for using MongoDB with rails applications. #1) I have a large collection, which has information about a map. A map embeds a lot of…
FernandoH
  • 855
  • 1
  • 9
  • 17
18
votes
1 answer

Mongoid Scope order by syntax please

I'm using the latest mongoid... How do I do the mongoid equivalent of this active record named_scope: class Comment include Mongoid::Document include Mongoid::Timestamps embedded_in :post field :body, :type => String named_scope…
kinet
  • 1,790
  • 3
  • 20
  • 32
18
votes
2 answers

Aggregate with Mongoid

MongoDB has a new Aggregation Framework and I'm trying to figure out how to use it with Mongoid. It appears there is a branch of Moped with this functionality as discussed here. I've updated to MongoDB 2.2 and tried installing this branch of Moped…
neon
  • 2,811
  • 6
  • 30
  • 44
17
votes
2 answers

How do you do wildcard searches with Mongoid in a Ruby on Rails environment?

The Mongoid documentation only gives one example of doing a wildcard search: Person.where(first_name: /^d/i) This finds all people with the first name that starts with "d". What do the /^ and /i represent? How do I find all people with their first…
Goalie
  • 3,045
  • 5
  • 35
  • 46
17
votes
8 answers

Mongoid pagination

I tried @posts = Post.page(params[:page]).per_page(10) and @posts = Post.paginate(:page => 1, :per_page => 10) but neither method works undefined method `page' for Post:Class undefined method `paginate' for Post:Class How do you do…
Vyacheslav Loginov
  • 3,136
  • 5
  • 33
  • 49
17
votes
6 answers

Clean out, or reset test-database with Rspec and MongoID on Rails 3

When I run my rspec tests, many fail due to stale data in my mongodb database. AFAIK it is far better to test with a clean database. With mysql, I could run rake db:test:prepare to clean up the database. How can I clean nd/or re-seed the database…
berkes
  • 26,996
  • 27
  • 115
  • 206
17
votes
3 answers

mongoid update all documents with conditions

I have a model class Employee include Mongoid::Document field :first_name field :last_name field :address1 field :address2 field :salary end Now I need to update all Employee's salary to 10000 whose address1 is "Calgary" Now I tried…
Gagan
  • 4,278
  • 7
  • 46
  • 71
17
votes
1 answer

Does Mongoid have Map/Reduce?

I am using Ruby code to calculate sum from the array returned by Mongoid. But maybe using Map/Reduce can be faster, except I don't see any docs for Map Reduce on mongoid.org and Google for map reduce site:mongoid.org doesn't give any result either.…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
17
votes
2 answers

Need advice on MongoDB Schema for Chat App. Embedded vs Related Documents

I'm starting a MongoDB project just for kicks and as a chance to learn MongoDB/NoSQL schemas. It'll be a live chat app and the stack includes: Rails 3, Ruby 1.9.2, Devise, Mongoid/MongoDB, CarrierWave, Redis, JQuery. I'll be handling the live chat…
Marston A.
  • 1,153
  • 1
  • 12
  • 10
17
votes
3 answers

Could not connect to a primary node for replica set ]>

I'm following though with the RailsApp tutorial with Devise and Mongoid (http://railsapps.github.io/tutorial-rails-mongoid-devise.html) and am encountering the following error when I get to 'Rake db:seed' down at the 'Set Up a Database Seed File'…
B. L.
  • 433
  • 2
  • 4
  • 9