我的Thing模型有一系列主题和以下内容.
我想找到所有things的current_user跟随它Topic或它的"用户".
@things = Thing
.where(:user_id.in => current_user.following.map{ |u| u._id })
.where(:topic_id.in => current_user.topics.map{ |u| u._id })
Run Code Online (Sandbox Code Playgroud)
这样的事情.但这实际上不起作用.它只返回两个where条件之间常见事物的记录.
我想要的是返回2个where语句找到的所有记录.
谢谢
我如何写两个日期范围的查询?唯一的条件是必须通过一个查询检索此数据.谢谢.
UPD:或如何在一个集合中结合2个查询?不是数组
嗨,我有嵌入文档,但我无法通过id查询它们.我可以输出所有检查,并有正确的ID,但查询它们是不可能的.我有一个课程模型embeds_many:course_members和一个带有embedded_in的CourseMember模型:course,:inverse_of =>:course_members
我没有成功地试过这个:
puts Course.where("course_members.id" => params[:id])
Run Code Online (Sandbox Code Playgroud)
使用此代码,我可以访问rigth文档:
c = Course.where("course_members.accepted" => 2).all
c.each do |l|
l.course_members.each do |f|
puts f.inspect
end
end
Run Code Online (Sandbox Code Playgroud)
但是如何通过一个Mongoid查询获取我的数据?
我有一个应用程序,我使用mongodb作为数据库存储记录我正在使用的mongodb的ruby包装是mongoid
现在一切正常,直到我遇到上述错误
超过最大插入大小16,000,000字节
任何针脚都可以指出如何摆脱错误.
我正在运行一个没有配置文件的mongodb服务器(mongodb源文件没有提供配置)
谁能帮忙
我有一个查询条件,它查找要匹配的元素数组(tags_array):
User.all_in('watchlists.tags_array' => tags_array)
Run Code Online (Sandbox Code Playgroud)
我想的标准是不区分大小写的,这意味着我希望它匹配%w[Ruby Web Framework]以及%w[RUBY WEB FRAMEWORK]或%w[ruby web framework]等等...
这可能是通过mongoid还是我必须使用外部过滤技巧?
我是Mongo的新手.
我正在尝试查询日期范围.用户模型具有以下字段:
field :confirmed_at, :type => Time
Run Code Online (Sandbox Code Playgroud)
有人可以帮我写一个查询,以获得在一段时间内确认其帐户的用户吗?
这是我正在尝试的错误和查询:
> db.users.where(:confirmed_at.gte => Time.now, :confirmed_at.lte => 10.minutes.ago)
Tue May 29 17:45:37 SyntaxError: syntax error (shell):1
Model.where(:confirmed_at.gte => Time.now, :confirmed_at.lte => 10.minutes.ago)
Tue May 29 17:56:15 SyntaxError: syntax error (shell):1
Run Code Online (Sandbox Code Playgroud)
谢谢!
当rails_admin用于关联对象(如has_and_belongs_to)时,它会将对象的ID显示为关联.这对用户来说并不是很重要,所以我想更改它以显示相关对象的文本.
这可以解决吗?
这里有一个小例子:
第一种模式:
class Menu
include Mongoid::Document
field :date, type: Date
has_and_belongs_to_many :meal
end
Run Code Online (Sandbox Code Playgroud)
第二种模式:
class Meal
include Mongoid::Document
field :text, type: String
has_and_belongs_to_many :menu
end
Run Code Online (Sandbox Code Playgroud)
所以它显示了这样的事情:

但我很乐意看到饭菜的文字.
说 Object embeds_many searched_items
这是文件:
{"_id": { "$oid" : "5320028b6d756e1981460000" },
"searched_items": [
{
"_id": { "$oid" : "5320028b6d756e1981470000" },
"hotel_id": 127,
"room_info": [
{
"price": 10,
"amenity_ids": [
1,
2
]
},
{
"price": 160,
"amenity_ids": null
}
]
},
{
"_id": { "$oid" : "5320028b6d756e1981480000" },
"hotel_id": 161,
"room_info": [
{
"price": 400,
"amenity_ids": [4,5]
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想找到具有room_info.amenity_idsIN [2,3] 的"searching_items" .
我试过了
object.searched_items.where('room_info.amenity_ids' => [2, 3])
object.searched_items.where('room_info.amenity_ids' =>{'$in' => [2,3]}
没有运气
我最近将mongodb从2.4更新到2.6,并且aggregate()中的新内存限制导致我的聚合失败,并出现以下错误:
Moped::Errors::OperationFailure: The operation: #<Moped::Protocol::Command
@length=251
@request_id=6
@response_to=0
@op_code=2004
@flags=[:slave_ok]
@full_collection_name="items.$cmd"
@skip=0
@limit=-1
@selector={:aggregate=>"items", :pipeline=>[{"$group"=>{"_id"=>"$serial_number", "total"=>{"$sum"=>1}}}, {"$match"=>{"total"=>{"$gte"=>2}}}, {"$sort"=>{"total"=>-1}}, {"$limit"=>750000}]}
@fields=nil>
failed with error 16945: "exception: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in."
Run Code Online (Sandbox Code Playgroud)
所以,我试图在查询中传递allowDiskUse:true:
dupes = Item.collection.aggregate([{
'$group' => {'_id' => "$serial_number", 'total' => { "$sum" => 1 } } },
{ '$match' => { 'total' => { '$gte' => 2 } } },
{ '$sort' => {'total' => -1}},
{ '$limit' => …Run Code Online (Sandbox Code Playgroud) 我在Rails 4中使用Mongoid.是否有任何宝石可以从项目中生成ERD.
https://github.com/huangw/mongoid-erd-gem
我查看了这个页面,但无法理解如何使用它.任何帮助谢谢