Почему данные не возвращаются из mongodb? - PullRequest
0 голосов
/ 20 июня 2019

Я использую mongoid с 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 голосов
/ 21 июня 2019

Попробуйте запустить его через rails console, также попробуйте отключить кэширование для отладки.

Вы также можете попробовать сначала преобразовать его в массив.

@quotes = Quote.all.to_a

...