首页 > 其他 > 详细

Mongoid - Queries

时间:2019-04-30 21:39:33      阅读:124      评论:0      收藏:0      [点我收藏+]

Queries

Find:

Actor.find("nm0993498").birth_name
#multiple id
Actor.find("nm000006", "nm0000008")

#
Movie.find_or_create_by()

#Does not persist, call save to persist
find_or_initialize_by()

 

where: 

Movie.where(:title => "Rocky")

Move.where(:year.gt => 2000).distinct(:title) 

# where ;$exists & :$regex 

writer = Writer.where(:hometown => {:exists => 0}).first

damon = Actor.where(:name => {:$regex => "Matt Da}).first

# where Geolocation query 
silver_spring = Place.where(:city=>"Silver Spring", :state=>"MD").first 

Actor.near(:"place_of_birth.geolocation=>silver_spring.geolocation").limit(5).each 

 

pluck and scope:

#pluck - give all the non nil value for the provided field

Movie.all.pluck(:title) 

#named scope

scope :current, -> {where(:year.gt => Date.current.year-2)}
Movie.current.where 

#default scope 
field :active, type: boolean, default: true 

#OR & in 

Movie.where(:year.gt => 2014).in(title:["The Martian"].pluck(:plot)

or conditional operator 
Movie.or({id: xxx},{title: "The Martian"}).pluck(:plot)

 

Mongoid - Queries

原文:https://www.cnblogs.com/vixennn/p/10798127.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!