为什么从mongodb不返回任何数据?

时间:2019-06-20 16:58:04

标签: ruby-on-rails ruby mongodb mongoid

我正在将Moongoid与Rails API配合使用, 我可以毫无问题地加载数据库,但是当我尝试获取一些数据时,不会返回任何数据

这是直接在控制台上进行的简单测试

(byebug) Quote.where(author: 'Albert Einstein').as_json
MONGODB | localhost:27017 | webcrawler_development.find | STARTED | 
{"find"=>"quotes", "filter"=>{"author"=>"Albert Einstein"}, "lsid"=>{"id"=> 
<BSON::Binary:0x18099040 type=uuid data=0x8e36fdaf1b9d42ce...>}}
MONGODB | localhost:27017 | webcrawler_development.find | SUCCEEDED | 0.001s
[]

我的控制器索引

class QuotesController < ApplicationController
  before_action :mongoid_cache, only: [:index]
  before_action :clear_quotes, only: [:load]

  def index
    @quotes = Quote.all
    render json: @quotes
  end

我的模特

class Quote
  include Mongoid::Document
  field :quote, type: String
  field :author, type: String
  field :author_about, type: String
  field :tags, type: Array, default: []
end

这是正在数据库中加载的数据

MONGODB | localhost:27017 | webcrawler_development.insert | SUCCEEDED | 0.001s
MONGODB | localhost:27017 | webcrawler_development.insert | STARTED | {"insert"=>"quotes", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x33015520 type=uuid data=0x212cf678e1f641d2...>}, "documents"=>[{"_id"=>BSON::ObjectId('5d0bbd126362390df4342038'), "tags"=>["abilities", "choices"], "quote"=>"“It is our choices, Har...
MONGODB | localhost:27017 | webcrawler_development.insert | SUCCEEDED | 0.000s
MONGODB | localhost:27017 | webcrawler_development.insert | STARTED | {"insert"=>"quotes", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x33015520 type=uuid data=0x212cf678e1f641d2...>}, "documents"=>[{"_id"=>BSON::ObjectId('5d0bbd126362390df4342039'), "tags"=>["inspirational", "life", "live", "miracle", "miracles"], "...
MONGODB | localhost:27017 | webcrawler_development.insert | SUCCEEDED | 0.000s
MONGODB | localhost:27017 | webcrawler_development.insert | STARTED | {"insert"=>"quotes", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x33015520 type=uuid data=0x212cf678e1f641d2...>}, "documents"=>[{"_id"=>BSON::ObjectId('5d0bbd126362390df434203a'), "tags"=>["aliteracy", "books", "classic", "humor"], "quote"=>"“The ...
MONGODB | localhost:27017 | webcrawler_development.insert | SUCCEEDED | 0.000s
MONGODB | localhost:27017 | webcrawler_development.insert | STARTED | {"insert"=>"quotes", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x33015520 type=uuid data=0x212cf678e1f641d2...>}, "documents"=>[{"_id"=>BSON::ObjectId('5d0bbd126362390df434203b'), "tags"=>["be-yourself", "inspirational"], "quote"=>"“Imperfection i...
MONGODB | localhost:27017 | webcrawler_development.insert | SUCCEEDED | 0.000s
MONGODB | localhost:27017 | webcrawler_development.insert | STARTED | {"insert"=>"quotes", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x33015520 type=uuid data=0x212cf678e1f641d2...>}, "documents"=>[{"_id"=>BSON::ObjectId('5d0bbd126362390df434203c'), "tags"=>["adulthood", "success", "value"], "quote"=>"“Try not to be...
MONGODB | localhost:27017 | webcrawler_development.insert | SUCCEEDED | 0.000s
MONGODB | localhost:27017 | webcrawler_development.insert | STARTED | {"insert"=>"quotes", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x33015520 type=uuid data=0x212cf678e1f641d2...>}, "documents"=>[{"_id"=>BSON::ObjectId('5d0bbd126362390df434203d'), "tags"=>["life", "love"], "quote"=>"“It is better to be hated for w...
MONGODB | localhost:27017 | webcrawler_development.insert | SUCCEEDED | 0.001s
MONGODB | localhost:27017 | webcrawler_development.insert | STARTED | {"insert"=>"quotes", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x33015520 type=uuid data=0x212cf678e1f641d2...>}, "documents"=>[{"_id"=>BSON::ObjectId('5d0bbd126362390df434203e'), "tags"=>["edison", "failure", "inspirational", "paraphrased"], "quo...
MONGODB | localhost:27017 | webcrawler_development.insert | SUCCEEDED | 0.000s
MONGODB | localhost:27017 | webcrawler_development.insert | STARTED | {"insert"=>"quotes", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x33015520 type=uuid data=0x212cf678e1f641d2...>}, "documents"=>[{"_id"=>BSON::ObjectId('5d0bbd126362390df434203f'), "tags"=>["misattributed-eleanor-roosevelt"], "quote"=>"“A woman is ...
MONGODB | localhost:27017 | webcrawler_development.insert | SUCCEEDED | 0.000s
MONGODB | localhost:27017 | webcrawler_development.insert | STARTED | {"insert"=>"quotes", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x33015520 type=uuid data=0x212cf678e1f641d2...>}, "documents"=>[{"_id"=>BSON::ObjectId('5d0bbd126362390df4342040'), "tags"=>["humor", "obvious", "simile"], "quote"=>"“A day without su...
MONGODB | localhost:27017 | webcrawler_development.insert | SUCCEEDED | 0.001s
Completed 204 No Content in 11147ms

这是直接在mongodb上进行的同一搜索

> db.quotes.find({author: 'Albert Einstein'})
{ "_id" : ObjectId("5d0bbd126362390df4342037"), "tags" : [ "change", "deep-thoughts", "thinking", "world" ], "quote" : "“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”", "author" : "Albert Einstein", "author_about" : "/author/Albert-Einstein" }
{ "_id" : ObjectId("5d0bbd126362390df4342039"), "tags" : [ "inspirational", "life", "live", "miracle", "miracles" ], "quote" : "“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”", "author" : "Albert Einstein", "author_about" : "/author/Albert-Einstein" }
{ "_id" : ObjectId("5d0bbd126362390df434203c"), "tags" : [ "adulthood", "success", "value" ], "quote" : "“Try not to become a man of success. Rather become a man of value.”", "author" : "Albert Einstein", "author_about" : "/author/Albert-Einstein" }

1 个答案:

答案 0 :(得分:0)

尝试通过rails console运行它,也尝试禁用缓存来调试它。

您也可以尝试先将其转换为数组。

@quotes = Quote.all.to_a