文档菜单
文档首页
/ / /
Mongoid
/

入门(Sinatra)

本页

  • 新应用
  • 创建 Git 仓库
  • 创建 Gemfile
  • 安装依赖
  • 本地运行 MongoDB
  • 使用 MongoDB Atlas
  • 基本应用
  • 运行应用
  • 现有应用

本节展示了如何使用 Mongoid 进行数据访问来创建新的 Sinatra 应用程序。对于不使用 Ruby on Rails 的其他 Ruby 应用程序,此过程类似。

此应用程序的完整源代码可在mongoid-demo GitHub 仓库中找到.

虽然不是必需的,但我们建议为您的应用程序创建一个 Git 仓库

git init blog
cd blog

在您按照本教程操作时提交您的更改。

创建一个名为Gemfile 的文件,并包含以下内容

source 'https://rubygems.org.cn'
gem 'sinatra'
gem 'mongoid'
gem 'puma'

运行以下命令来安装依赖项

gem install bundler
bundle install

此命令将生成一个名为 Gemfile.lock 的文件,我们建议将其提交到您的 Git 仓库。

要在本地使用 MongoDB 进行开发,请下载并安装 MongoDB

一旦 MongoDB 安装并运行,创建一个名为 config/mongoid.yml 的文件,指向您的部署。例如,如果您在默认端口上启动了独立的 mongod,则以下内容是合适的

development:
clients:
default:
database: blog_development
hosts:
- localhost:27017
options:
server_selection_timeout: 1

您不需要下载、安装和本地运行 MongoDB,而是可以创建一个免费的 MongoDB Atlas 帐户并在 Atlas 中创建一个免费 MongoDB 集群。一旦集群创建完成,请按照连接到集群页面中的说明操作,以获取 URI。使用 Ruby 驱动程序 2.5 或更高版本 格式。

创建一个名为 config/mongoid.yml 的文件,包含以下内容,将 URI 替换为您集群的实际 URI

development:
clients:
default:
uri: mongodb+srv://user:pass@yourcluster.mongodb.net/blog_development?retryWrites=true&w=majority
options:
server_selection_timeout: 5

创建一个名为 app.rb 的文件,并包含以下内容。首先,需要一些导入

require 'sinatra'
require 'mongoid'

加载 Mongoid 配置文件并配置 Mongoid。当使用 Rails 与 Mongoid 配合时,这会自动完成,但因为我们是在使用 Sinatra 与 Mongoid 配合,所以我们需要自己完成这一步

Mongoid.load!(File.join(File.dirname(__FILE__), 'config', 'mongoid.yml'))

现在我们可以定义一些模型

class Post
include Mongoid::Document
field :title, type: String
field :body, type: String
has_many :comments
end
class Comment
include Mongoid::Document
field :name, type: String
field :message, type: String
belongs_to :post
end

... 然后添加一些路由

get '/posts' do
Post.all.to_json
end
post '/posts' do
post = Post.create!(params[:post])
post.to_json
end
get '/posts/:post_id' do |post_id|
post = Post.find(post_id)
post.attributes.merge(
comments: post.comments,
).to_json
end
post '/posts/:post_id/comments' do |post_id|
post = Post.find(post_id)
comment = post.comments.create!(params[:comment])
{}.to_json
end

启动应用程序

bundle exec ruby app.rb

使用 curl 尝试一些请求

curl http://localhost:4567/posts
# => []
curl -d 'post[title]=hello&post[body]=hello+world' http://localhost:4567/posts
# => {"_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"},"body":"hello world","title":"hello"}
curl http://localhost:4567/posts
# => [{"_id":{"_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"},"body":"hello world","title":"hello"}]
curl -d 'comment[name]=David&comment[message]=I+like' http://localhost:4567/posts/5d8151ec96fb4f0ed5a7a03f/comments
# => {}
curl http://localhost:4567/posts/5d8151ec96fb4f0ed5a7a03f
# => {"_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"},"title":"hello","body":"hello world","comments":[{"_id":{"$oid":"5d8157ac96fb4f20c5e45c4d"},"message":"I like","name":"David","post_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"}}]}

要在现有的 Sinatra 应用程序中使用 Mongoid,步骤基本上与上面为新建应用程序提供的步骤相同

  1. mongoid 依赖项添加到 Gemfile 中。

  2. 创建一个 mongoid.yml 配置文件。

  3. 加载配置文件并在应用程序中配置 Mongoid。

  4. 定义 Mongoid 模型。

返回

教程