有没有人知道Mongoid是否内置了对别名字段名称的支持?我一直在运行一些测试,其中我有一个具有最少量字段的集合(7个字段).如果我使用描述性名称并加载实际数据,然后使用大大缩短的名称并加载相同的真实数据,我看到我的集合总大小减少了40%.在查看MongoDB(非Ruby)的其他驱动程序时,我发现其中一些驱动程序内置了支持,您可以根据描述性名称编写代码,但持久性足够智能,可以使用开发人员定义的别名.我只是想确定Mongoid是否有类似的东西.
我正在使用Rails 3和Mongoid.
我有一个Folder类,然后可以与其他User类共享
class Folder
has_one :owner
has_many :users
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建两个范围,一个可用于返回所有私人文件夹,另一个用于返回所有共享文件夹.有没有办法计算范围内的关联数量?
scope :personal, where(:users.count => 0) #Erroring on count...
scope :shared, where(:users.count.gt => 0) #Erroring on count...
Run Code Online (Sandbox Code Playgroud)
我已经考虑过构建方法,但我更喜欢使用范围,因为我希望将它们与其他范围链接起来.
我在基于Mongoid的类中包含了Mongoid :: Versioning模块.检查以前的"版本"或文档化身的最佳方法是什么?我希望能够看到它的历史.这可以是通过rails console或MongoDB shell.查看文档历史记录的最简单方法是什么?
我正在开发一个控制器,它创建一个具有多态belongs_to关联的模型.我现在要做的是找到它所属的模型如下:
def find_polymorphic_model(classes)
classes_names = classes.map { |c| c.name.underscore + '_id' }
params.select { |k, v| classes_names.include?(k) }.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
raise InvalidPolymorphicType
end
Run Code Online (Sandbox Code Playgroud)
其中classes是关联的有效类型数组.
这种方法的问题是我必须在控制器中记住我正在创建的模型允许哪些类型.
有没有办法找到某个多态的belongs_to关联允许哪些类型?或者我可能做错了,我不应该让多态控制器暴露而不将它嵌套在多态资源中(在路由器中)?
我也认为Rails延迟加载类可能存在问题,所以为了能够找到这个东西,我必须在初始化时显式加载所有模型.
validation activerecord ruby-on-rails polymorphic-associations mongoid
我正在尝试使用mongoid,但它会输出此错误:
未初始化的常量'Mongo'
这是我的代码:
require "mongoid"
Mongoid.configure do |config|
config.master = Mongo::Connection.new("localhost",27017).db("arthist")
end
class Artist
include Mongoid::Document
field :name, type: String
end
a = Artist.create(name: "hoge")
Run Code Online (Sandbox Code Playgroud)
你有什么主意吗?
我正在使用Mongoid.我有一个类似于以下内容的文档:
Pet:
foo: {bar: 1, foobar: 2}
another_attr: 1
Run Code Online (Sandbox Code Playgroud)
在这种情况下,最好使用Mongoid,如何查询条形值大于0的所有宠物,其中another_attr等于1?
我是mongodb&mongoid的新手..这是一个在某些pont上的mongoid 2上的rails应用程序,后来转移到了mongoid 3
我正在尝试运行迁移,其中一个具有以下内容
Assessment.collection.update({'result_access' => {'$exist' => false}}, {'$set' => {'result_access' => 'all'}}, {:multi => true})
Run Code Online (Sandbox Code Playgroud)
我不确定如何为mongoid 3更新这个
我有以下型号
城市(id,name,geo {lng,lat})
地理位置(LNG,LAT)
城市模型
class City
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :timezone, type: String
field :slug, type: String
belongs_to :region
belongs_to :country
embeds_one :geo_location
accepts_nested_attributes_for :geo_location
end
Run Code Online (Sandbox Code Playgroud)
地理位置模型
class GeoLocation
include Mongoid::Document
include Mongoid::Timestamps
field :lng, type: String
field :lat, type: String
embedded_in :city
end
Run Code Online (Sandbox Code Playgroud)
城市控制员
class CitiesController < ApplicationController
before_action :set_city, only: [:show, :edit, :update, :destroy]
# GET /cities
def index
@cities = City.all
end
# GET /cities/1
def show
end
# GET /cities/new …Run Code Online (Sandbox Code Playgroud) 我得到查询字符串的mongodb id.如何检查验证此ID?我想如果mongodb id无效重定向到另一个url
获取查询字符串:
if(count($_GET)>0 && $_GET['uid']){
//get id from string query
$query = array("_id" => new MongoId($_GET
['uid']));
$user = DB::findone('users',$query);
}else{
//redirect if not exist query string
header('location:'.ADMIN_URL.'/items/forbidden.php');
}
Run Code Online (Sandbox Code Playgroud)
请帮忙...谢谢
我在MongoDB上有一个Rails应用程序。我需要在状态网页上动态显示当前的MongoDB版本。我的客户知道它是否是很重要的3.0,3.2或3.4版本。
但是,我在任何地方都找不到此值。我正在pry调试连接详细信息,并且已经尝试过从Mongoid和Mongo类层次结构中使用任何相关的公共方法,但均未成功。
用纯MongoDB做到这一点的规范方法是db.version(),但是似乎没有办法用Mongoid发送该原始查询。