码迷,mamicode.com
首页 > 其他好文 > 详细

Mongoid - Queries

时间:2019-04-30 21:53:52      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:operator   pre   tle   mongo   multiple   cal   cto   and   initial   

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

标签:operator   pre   tle   mongo   multiple   cal   cto   and   initial   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!