Ruby-on-rails Mongodb:使用前需要了解什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2124274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongodb: What to know before using?
提问by Wayne Conrad
I'm starting a hobby (non-revenue) project using Ruby on Rails. I've done a fair amount of development in Rails using Postgresql, and I can make a pretty good imitation of normalized schema. However, Mongrodb looks shiny and new. What better for trying out something new than a hobby project?
我正在使用 Ruby on Rails 开始一个业余爱好(无收入)项目。我已经使用 Postgresql 在 Rails 中进行了大量开发,并且可以很好地模仿规范化模式。然而,Mongrodb 看起来光鲜亮丽。有什么比业余爱好项目更适合尝试新事物?
Think back to when you started using Mongodb. What techniques did you learn later that made you say, "If only I knew that when I started!" What plug-ins did you discover that you would have used right from the start, if only you had known? What references would you like to have had bookmarked?
回想一下您何时开始使用 Mongodb。你后来学到了什么技巧让你说:“要是我一开始就知道就好了!” 您发现哪些插件是您从一开始就会使用的,如果您知道的话?您希望为哪些参考文献添加书签?
采纳答案by Emily
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.
如果您打算将 MongoDB 与 Rails 一起使用,我肯定会支持 MongoMapper 的推荐。但是,我会警告您,除了几篇博客文章外,(到目前为止)没有任何文档。如果您不愿意深入研究源代码以了解其工作原理,那么它可能还不适合您。
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.
如果您在 Rails 之外工作,我建议您远离 MongoMapper。因为它将 MongoDB 处理成类似于我们对 SQL 支持的 ORM 的期望,它并没有真正让您很好地了解 MongoDB 背后的不同思想的力量和不同的想法。花一些时间尝试使用较低级别的 ruby 驱动程序,甚至在 javascript 控制台中。
The other thing I'd recommend, especially since you mentioned knowing how to normalize a schema, is notto 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.
我建议的另一件事,尤其是因为您提到知道如何规范化模式,现在不要将 MongoDB 视为数据库。在 MongoDB 中组织数据的方式与在关系数据库中的组织方式大不相同。尝试将其更多地视为存储和检索 Ruby 哈希的地方。你可以用 MongoDB 做一些关系性的事情,但我建议你在尝试围绕 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 MapReduceto get an idea of some of the unique advantages and strengths of a NoSQL database.
至于您应该查看哪些链接,我强烈建议您通读 MongoDB 站点上的所有内容。他们的文档非常好。特别是,查看高级查询、多键索引和MapReduce以了解 NoSQL 数据库的一些独特优势和优势。
回答by Nerian
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:
我和你几乎处于同一阶段。使用 MongoDB 启动一个新项目。我大约有 7 周的经验。这是我发现非常有用的:
Use Mongoid instead of Mongomapper
使用 Mongoid 而不是 Mongomapper
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.
文档非常好。认真的,优秀的。阅读所有文档大约需要 15 分钟,您将对 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.
明天,将发布新的 mongoid 主要版本的候选版本。它会带来很多有用的东西。
I am using Rails 3. To install the development version add this to your gem file:
我正在使用 Rails 3。要安装开发版本,请将其添加到您的 gem 文件中:
gem 'mongoid', "~>2.0.0.beta"
gem 'mongoid', "~>2.0.0.beta"
Current beta is 20, but as I said, tomorrow there is the release candidate.
当前测试版是 20,但正如我所说,明天有发布候选。
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.
另外我建议你也加入谷歌组。它的流量很低,人们非常愿意回答任何问题。例如,我向他们展示了我的第一个 DB 模型设计,他们给了我很多改进方法。Mongoid 的创建者也回答了您的问题。
In two words: Great community.
两个字:伟大的社区。
There is this plugin that enables you to use Machinist with mongo:
有这个插件可以让你在 mongo 中使用 Machinist:
https://github.com/nmerouze/machinist_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.
您可以将 Forgery 与 Machinist 一起使用。很棒的组合。
https://github.com/sevenwire/forgery
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.
还有一点我想说。我来自关系数据库世界,所以一开始听起来很奇怪:您可以将文件保存在 mongo 数据库中。
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 :)
事实上,这可能比我们过去管理它们更快。这是因为 mongo 支持分片。分片意味着您可以使用一组计算机来为 Mongo 数据库提供服务。它是无缝的。主从。因此,您可以从多台计算机提供一个文件,每台计算机发送一部分。它可以很好地扩展:)
This is done using GridFS. http://www.mongodb.org/display/DOCS/GridFS
这是使用 GridFS 完成的。http://www.mongodb.org/display/DOCS/GridFS
Mongoid supports that master-slave config.
Mongoid 支持主从配置。
Ask me if you need more information.
问我是否需要更多信息。
Edit:
编辑:
回答by user956584
1. Query its not case-sensitive
1.查询不区分大小写
eq
情商
"_id": "1da259c70fe3392c3b000002",
"name": "Dany"
array('name' => 'dany') :: results 0
array('name' => 'Dany') :: results 1
2. Last insert ID:
2. 最后插入ID:
$coll->insert($user, true);
echo (string) $user['_id'];
3. _id is a MongoId object
3. _id 是一个 MongoId 对象
Find by id:
按 id 查找:
$p->findOne(array('_id' => new MongoId( $UID )), array('proj'));
Show _id:
显示_id:
$coll['_id'] = ( string ) $coll['_id'];
Also _id is only unique per collection, can be different on other server
此外 _id 每个集合是唯一的,在其他服务器上可以不同
4. MongoDB has support for LIMIT,OFFSET,ORDER
4. MongoDB 支持 LIMIT,OFFSET,ORDER
for some advance sorting you can use Aggregation Framework.
对于一些高级排序,您可以使用聚合框架。
5. In SQL is Sql injection in MongoDB is Array injection
5.在SQL中是Sql注入在MongoDB中是数组注入
So when write some data use ( string ) or check is_array
所以当写一些数据使用(字符串)或检查 is_array
$req = (string) $range['name'];
6. HDD DOS
6.硬盘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.
默认 _POST 大小在 PHP 中为 8MB,在 Mongo 中每个文档有 16MB 的限制。所以想象一些用户在你的分析脚本中欺骗 eq USER_AGENT STRING,然后每个插入发送 16MB。
7. There was some problems in past with MongoDb but right now 3.0 is pretty awesome and stable.
7. MongoDb 过去存在一些问题,但现在 3.0 非常棒且稳定。
回答by Kris Krause
Here is a great beginner/introduction to MongoDb podcast from .NET Rocks -
这是 .NET Rocks 对 MongoDb 播客的一个很好的初学者/介绍 -
http://www.dotnetrocks.com/default.aspx?ShowNum=507
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 接受了采访……他在 MongoDb 项目中工作。哦,音质非常好。
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.
Mike Dirolf 是 10gen 的一名软件工程师,负责 MongoDB 项目。他主要研究 Python 和 Ruby 的客户端驱动程序,但也抽出时间谈论 MongoDB - 他曾在 EuroPython、Strange Loop Conf、RubyEnRails、RuPy 和 RubyConf 以及纽约市、伦敦、华盛顿的聚会小组中发表演讲DC和旧金山。
回答by t6d
You should definitively take a look into junemakers mongo-mapper: http://github.com/jnunemaker/mongomapperBut 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.
您绝对应该看看 junemakers mongo-mapper:http://github.com/jnunemaker/mongomapper但我也建议您使用纯 Ruby Mongo 驱动程序来了解一下 mongo-mapper 的工作原理。使用 Ruby 将一些数据放入 Mongo 数据库并不难。
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
我猜你已经找到了 Ruby Mongo 教程。以防万一,这里是链接:http: //www.mongodb.org/display/DOCS/Ruby+Tutorial

