我有一个包含嵌入文档的mongomapper文档,并希望复制它.
从本质上讲,我想要做的是这样的事情:
customer = Customer.find(params[:id])
new_customer = Customer.new
new_customer = customer
new_customer.save
Run Code Online (Sandbox Code Playgroud)
所以我想最终得到两个不同的mongomapper文档,但内容相同.
有什么想法应该怎么做?
我试图弄清楚如何为我正在处理的网站布置我的数据库.这是我的模特:
class User
include MongoMapper::Document
// keys here
many :items
many :likes
end
class Item
include MongoMapper::Document
key :name, String, :required => true
many :likes
end
class Like
include MongoMapper::EmbeddedDocument
key :user_id, String, :required => true
end
Run Code Online (Sandbox Code Playgroud)
我相信Like应该嵌入某个地方,但我很难选择一个,因为我想要摆脱它的功能.
user = User.first
user.likes // should print out all the Items he liked
item = Item.first
item.likes // so I can count how many people like that item
Run Code Online (Sandbox Code Playgroud)
虽然使用EmbeddedDocument时会出现问题,但是您会丢失find其他有用的方法,并且不能将它嵌入到两个模型中.所以只有它Item,我需要运行它(但不能):
item = Item.first
item.likes.find_by_user_id(User.first._id)
Run Code Online (Sandbox Code Playgroud)
find_by_user_id将抛出未定义的方法.所以如果我把它嵌入我的中User …
我是mongodb/mongomapper的新手,无法找到答案.
我有一个mongomapper类,包含以下字段
key :author_id, Integer
key :partecipant_ids, Array
Run Code Online (Sandbox Code Playgroud)
假设我有一个具有以下属性的"记录":
{ :author_id => 10, :partecipant_ids => [10,15,201] }
Run Code Online (Sandbox Code Playgroud)
我想要检索涉及id为15的partecipant的所有对象.我没有在文档中找到任何提及.
奇怪的是,以前我在做这个查询
MessageThread.where :partecipant_ids => [15]
Run Code Online (Sandbox Code Playgroud)
哪个工作,但在(或许)宝石/ mongodb版本的一些变化后它停止工作.不幸的是,我不知道我之前使用过哪个版本的mongodb和mongomapper.
我试图在MongoMapper支持的模型中使用maxDistance封装near查询.
我必须在查询语法中做一些愚蠢的事情.
模型
class Site
include MongoMapper::Document
key :id, Integer
key :name, String
key :location, Array
ensure_index [[:location, '2d']]
def self.nearest(center_point, range)
where(:location => {'$near' => center_point, '$maxDistance' => range}).all
end
end
Run Code Online (Sandbox Code Playgroud)
试图把距离一点200英里的所有东西都拿走......
Site.nearest([ - 122.0,44.0],200)
> Mongo::OperationFailure: geo values have to be numbers: {
> $maxDistance: 200, $near: [ -122.0, 44.0 ] } from
> /Library/Ruby/Gems/1.8/gems/mongo-1.6.1/lib/mongo/cursor.rb:144:in
> `next' from
> /Library/Ruby/Gems/1.8/gems/mongo-1.6.1/lib/mongo/cursor.rb:290:in
> `each' from
> /Library/Ruby/Gems/1.8/gems/mongo-1.6.1/lib/mongo/cursor.rb:308:in
> `to_a' from
> /Library/Ruby/Gems/1.8/gems/mongo-1.6.1/lib/mongo/cursor.rb:308:in
> `to_a' from
> /Library/Ruby/Gems/1.8/gems/plucky-0.4.4/lib/plucky/query.rb:74:in
> `all' from …Run Code Online (Sandbox Code Playgroud) 我mongo_mapper在Rails中使用MongoDB和gem,项目足够大.有什么办法可以将数据从Mongoid迁移到Postgresql吗?
我试图使用rails 4.0.0与mongo_mapper 0.12.0并获取该消息
Bundler could not find compatible versions for gem "activesupport":
In Gemfile:
mongo_mapper (~> 0.12.0) ruby depends on
activesupport (~> 3.0) ruby
rails (= 4.0.0) ruby depends on
activesupport (4.0.0)
Run Code Online (Sandbox Code Playgroud)
Rails 4和mongo_mapper仍然不兼容?
我有mongomapper协会的问题.我有一个类名User和其他名为Model的.用户有很多型号,但......
user = User.first
=> <User ...
user.models
=> []
Model.find_by_user_id(user.id.to_s)
=> <Model ...
Model.find_by_user_id(user.id.to_s).user == user
=> true
Run Code Online (Sandbox Code Playgroud)
类代码(简化):
class User
include MongoMapper::Document
# some keys definition
many :models
end
class Model
include MongoMapper::Document
# some keys definitions
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我正在尝试运行查询,我想忽略具有特定电子邮件地址的记录...
@foo = Bar.all(:email =>'xxx')<---除非我想否定此电子邮件地址的存在位置.
请让我知道如何做到这一点.
谢谢!
我成功地使用了MongoMapper对原子"$ push"和"$ set"的内置支持,但无法弄清楚"$ pull"
class Feed
include MongoMapper::Document
many :stories
end
class Story
include MongoMapper::EmbeddedDocument
key :title, String
end
feed = Feed.first
story = Story.new(:title => "Hello, world!")
Feed.push(feed.id, :stories => story.to_mongo) #works fine
Feed.set({:_id => feed.id, "stories.title" => "Hello, world!"}, "stories.$.title" => "Foo") #works fine
Feed.pull( ??? )
Run Code Online (Sandbox Code Playgroud)
如何使用拉动原子地删除故事?
在MongoMapper中,我可以使用以下查询来搜索基于正则表达式匹配的项目:
Foo.where(:name => /Foo.*/)
Run Code Online (Sandbox Code Playgroud)
如何搜索其name字段不匹配的项目/Foo.*/?