如何使用rails将数据填充到mongodb中?

发布于 2024-12-22 09:12:01 字数 572 浏览 2 评论 0原文

我是 Rails 应用程序的新手。我使用 mongoid 与 mongodb 建立了连接。 命令生成了 mongoid:migration

rails generate migration sample

我使用名为it create 的

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
我的问题是
1.为什么数据库中不存在schema.rb。
2.如何使用rails将数据填充到mongodb中
3.如何在rails中列出数据库集合
4.如何将bson文件生成到rails中

I'm new to rails application. I got connection with mongodb using mongoid.
I generated the mongoid:migration using command called

rails generate migration sample

it creates,

db\migrate\20111222081138_sample.rb contains the following code

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小镇女孩 2024-12-29 09:12:01

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

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

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

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

MondoDB is a schema-less database, this is the reason why schema.rb is not present.

To query, insert, update or delete records, follow the instructions available in the Mongoid documentation. The documentation is comprehensive, well-written and it is worth a read.

Also, if you are completely new to Rails and you don't know almost anything about NoSQL databases, it's better if you start with one technology at time and you just try Rails with a relational database, such as SQLite or PostgreSQL.

The most part of Rails ORM tutorials is about ActiveRecord. Trying to approach several new topics at once just leads to confusion.

々眼睛长脚气 2024-12-29 09:12:01

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

I also encountered this problem I'm new on using rails with mongodb. The solution I found is to create db/seeds.rb manually. Add the dummy data there and run rails db:seed as usual. It works just fine for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文