簡體   English   中英

為什么從mongodb不返回任何數據?

[英]Why no data is returned from mongodb?

我將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" }

嘗試通過rails console運行它,也嘗試禁用緩存來調試它。

您也可以嘗試先將其轉換為數組。

@quotes = Quote.all.to_a

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM