MongoMapper 保存不工作

发布于 2025-01-08 08:27:12 字数 3252 浏览 5 评论 0原文

不知道为什么这不起作用。这是我第一次使用 Mongo,但是从我读过的所有文档来看,这应该可行吗?有人知道我缺少什么吗?

require 'rubygems'                                                                                                                                                                                      
require 'sinatra/base' 
require 'mongo_mapper'                                                                                                                                                                                  

  mongo_server = '127.0.0.1'                                                                                                                                                       
  mongo_database = 'inone'                                                                                                                                                         

  MongoMapper.connection = Mongo::Connection.new(mongo_server)                                                                                                                                            
  MongoMapper.database = mongo_database                                                                                                                                                                   

  # DB model                                                                                                                                                                                              
  class URLstore                                                                                                                                                                                          
    include MongoMapper::Document                                                                                                                                                                         

    key :url_key, String                                                                                                                                                                                  
    key :url, String                                                                                                                                                                                      
  end                      

class URLnip < Sinatra::Base                                                                                                                                                                            
   get '/testmongo' do         
    nipurl = URLstore.new(:url_key => "abc", :url => "www.google.com")                                                                                                                                                                                                                                                                                                           
    nipurl.save  
  end
end

打开 Mongo 终端,我可以看到数据库已创建

> show dbs
inone   0.203125GB

,但这样做根本不会返回任何结果

> db.inone.find()
> 

,或者

> db.inone.find({url : 'www.google.com'})
> 

同样的事情不会返回任何结果。

No idea why this is not working. It's the first time I am working with Mongo, but from all the docs I have read, this should work? Anyone have any idea what I am missing?

require 'rubygems'                                                                                                                                                                                      
require 'sinatra/base' 
require 'mongo_mapper'                                                                                                                                                                                  

  mongo_server = '127.0.0.1'                                                                                                                                                       
  mongo_database = 'inone'                                                                                                                                                         

  MongoMapper.connection = Mongo::Connection.new(mongo_server)                                                                                                                                            
  MongoMapper.database = mongo_database                                                                                                                                                                   

  # DB model                                                                                                                                                                                              
  class URLstore                                                                                                                                                                                          
    include MongoMapper::Document                                                                                                                                                                         

    key :url_key, String                                                                                                                                                                                  
    key :url, String                                                                                                                                                                                      
  end                      

class URLnip < Sinatra::Base                                                                                                                                                                            
   get '/testmongo' do         
    nipurl = URLstore.new(:url_key => "abc", :url => "www.google.com")                                                                                                                                                                                                                                                                                                           
    nipurl.save  
  end
end

Opening the Mongo terminal I can see the DB get's created

> show dbs
inone   0.203125GB

but doing this brings back no results at all

> db.inone.find()
> 

or

> db.inone.find({url : 'www.google.com'})
> 

same thing nothing.

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

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

发布评论

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

评论(1

枕梦 2025-01-15 08:27:12

难道不应该是db.urlstore.find()吗?或者,MongoMapper 会改变这种情况。选择正确的数据库后,Mongo shell 中的 show collections 会显示什么?

Shouldn't that be db.urlstore.find()? Or however MongoMapper changes the case. What does show collections in a Mongo shell say after you select the right database?

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