【问题标题】:How to populate data into mongodb using rails?如何使用rails将数据填充到mongodb中?
【发布时间】:2011-12-22 10:25:53
【问题描述】:

我是 Rails 应用程序的新手。我使用 mongoid 与 mongodb 建立了联系。 我使用名为

的命令生成了 mongoid:migration
rails generate migration sample

它创造,

db\migrate\20111222081138_sample.rb 包含以下代码

class Sample < Mongoid::Migration
   def self.up
      sample.create(first_name: "Heinrich", last_name: "Heine")
   end    

   def self.down
   end
end
my questions are
1.why schema.rb is not present in db.
2.how to populate data into mongodb using rails
3.how to list db collections in rails
4.how to produce bson file into rails

【问题讨论】:

  • 你读过关于 MongoId 或 MongoDB 的文档吗?
  • 为 mongoid 迁移?我认为这里有问题。请查看 mongoid、mongodb 和 rails 的文档。正如 Simone 所指出的,最好先学习一项技术。

标签: ruby-on-rails mongodb mongoid


【解决方案1】:

MondoDB 是一个无模式数据库,这就是schema.rb 不存在的原因。

要查询、插入、更新或删除记录,请按照Mongoid documentation 中的说明进行操作。文档很全面,写得很好,值得一读。

另外,如果您完全是 Rails 新手,并且对 NoSQL 数据库几乎一无所知,那么最好从一种技术开始,然后只尝试使用关系数据库(如 SQLite 或 PostgreSQL)使用 Rails。

Rails ORM 教程的大部分内容是关于 ActiveRecord。试图同时处理几个新主题只会导致混乱。

【讨论】:

    【解决方案2】:

    我也遇到了这个问题,我是在 mongodb 中使用 rails 的新手。我找到的解决方案是手动创建 db/seeds.rb 。在那里添加虚拟数据并像往常一样运行 rails db:seed 。它对我来说很好用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 2021-04-25
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      相关资源
      最近更新 更多