使用前需要知道什么?

我正在开始一个业余爱好(非收入)项目使用 Ruby on Rails。我已经在 Rails 中使用 Postgreql 进行了相当多的开发,我可以很好地模仿规范化模式。然而,蒙格罗布看起来闪闪发光,焕然一新。还有什么比业余爱好项目更适合尝试新事物呢?

回想一下您开始使用 Mongodb 的时候。后来你学到了什么技巧,让你说: “要是我刚开始学的时候就知道该多好!”如果您知道的话,您会从一开始就使用哪些插件?你希望有哪些参考书签?

22922 次浏览

You should definitively take a look into junemakers mongo-mapper: http://github.com/jnunemaker/mongomapper But I also recommend you to play a little bit with the pure Ruby Mongo driver to see how mongo-mapper works under the hood. It isn't very hard to put some data into a Mongo database using Ruby.

I guess you already found the Ruby Mongo Tutorial. Just in case you didn't, here is the link: http://www.mongodb.org/display/DOCS/Ruby+Tutorial

I would definitely second the recommendation of MongoMapper if you're going to be using MongoDB with Rails. I will warn you, however, that there is (so far) no documentation other than a couple blog posts. If you're not comfortable digging into the source code to see how things work, it's probably not for you yet.

If you're working outside of Rails, I'd recommend staying away from MongoMapper. Because it's working MongoDB into something similar to what we expect from a SQL-backed ORM, it doesn't really give you a good idea of the power of and of the different thinking behind MongoDB. Spend some time playing around with the lower-level ruby driver, and even in the javascript console.

The other thing I'd recommend, especially since you mentioned knowing how to normalize a schema, is not to think of MongoDB as a database for now. The way you organize your data in MongoDB is very different that with a relational database. Try to think about it more as a place to store and retrieve Ruby hashes. You can do some relational things with MongoDB, but I'd recommend sticking with only self-contained documents while you're trying to wrap your head around NoSQL.

As for what links you should look at, I'd highly recommend reading through everything you can on the MongoDB site. Their documentation is very good. Particularly, take a look at the advanced queries, multikey indexes, and MapReduce to get an idea of some of the unique advantages and strengths of a NoSQL database.

Here is a great beginner/introduction to MongoDb podcast from .NET Rocks -

http://www.dotnetrocks.com/default.aspx?ShowNum=507

Mike Dirolf is intereviewed... he works on the MongoDb project. Oh, and the sound quality is excellent.

Mike Dirolf is a Software Engineer at 10gen, where he works on the MongoDB project. He mainly works on client drivers for Python and Ruby, but also takes time out to talk about MongoDB - he has presented at EuroPython, Strange Loop Conf, RubyEnRails, RuPy and RubyConf as well as at meetup groups in New York City, London, Washington D.C. and San Francisco.

I am at nearly the same stage that you are. Starting a new project with MongoDB. I am around 7 weeks of experience. This is what I have found very useful:

Use Mongoid instead of Mongomapper

http://mongoid.org/

The documentation is excellent. Seriously, excellent. It should take you about 15 min reading all the documentation and you will have a very exact idea of what you can do and cannot do with Mongoid.

Tomorrow, the release candidate for a new major version of mongoid will be released. It is going to brings a lot of useful things.

I am using Rails 3. To install the development version add this to your gem file:

gem 'mongoid', "~>2.0.0.beta"

Current beta is 20, but as I said, tomorrow there is the release candidate.

Also I sugest you to join the google group as well. It has low traffic and people are very willing to answer any question. For example I showed them my first DB Model design and they gave me many ways to improve that. The creator of Mongoid answer your questions too.

In two words: Great community.

There is this plugin that enables you to use Machinist with mongo:

https://github.com/nmerouze/machinist_mongo

Works pretty well.

gem 'machinist_mongo', :require => 'machinist/mongoid',
:git => 'http://github.com/nmerouze/machinist_mongo.git',
:branch => 'machinist2'

You can use Forgery with Machinist. Awesome mix.

https://github.com/sevenwire/forgery

Another thing I want to say. I come from a relation database world, so this sounded really weird to at the beginning: You can save files in a mongo database.

In fact, it could be faster than managing them as we used to do. This is because of mongo's support for sharding. Sharding means that you can use a cluster of computers to serve the Mongo Database. It is seamless. Master-slave. So you can serve a file from many computers, each sending a portion. It scales very well :)

This is done using GridFS. http://www.mongodb.org/display/DOCS/GridFS

Mongoid supports that master-slave config.

Ask me if you need more information.

Edit:

Also: http://railscasts.com/episodes/238-mongoid

1. Query its not case-sensitive

eq

"_id": "1da259c70fe3392c3b000002",
"name": "Dany"


array('name' => 'dany') :: results 0
array('name' => 'Dany') :: results 1

2. Last insert ID:

$coll->insert($user, true);
echo (string) $user['_id'];

3. _id is a MongoId object

Find by id:

$p->findOne(array('_id' => new MongoId( $UID  )), array('proj'));

Show _id:

$coll['_id'] = ( string ) $coll['_id'];

Also _id is only unique per collection, can be different on other server

4. MongoDB has support for LIMIT,OFFSET,ORDER

for some advance sorting you can use Aggregation Framework.

5. In SQL is Sql injection in MongoDB is Array injection

So when write some data use ( string ) or check is_array

$req = (string) $range['name'];

6. HDD DOS

default _POST size is 8MB in PHP, IN Mongo there are limit per document 16MB. So imagine Some user spoof eq USER_AGENT STRING in you analytic script and then send 16MB per single insert.

7. There was some problems in past with MongoDb but right now 3.0 is pretty awesome and stable.