1+ # start create index
2+ class Restaurant
3+ include Mongoid ::Document
4+
5+ field :name , type : String
6+ field :cuisine , type : String
7+ field :borough , type : String
8+
9+ index ( { cuisine : 1 } , { name : "cuisine_index" , unique : false } )
10+ end
11+
12+ Restaurant . create_indexes
13+ # end create index
14+
15+ # start create alias index
16+ class Restaurant
17+ include Mongoid ::Document
18+
19+ field :borough , as : :b
20+
21+ index ( { b : 1 } , { name : "borough_index" } )
22+ end
23+ # end create alias index
24+
25+ # start create embedded index
26+ class Restaurant
27+ include Mongoid ::Document
28+
29+ embeds_many :addresses
30+ index ( { "addresses.street" : 1 } )
31+ end
32+ # end create embedded index
33+
34+ # start create compound index
35+ class Restaurant
36+ include Mongoid ::Document
37+
38+ field :name , type : String
39+ embeds_many :addresses
40+
41+ index ( { "addresses.street" : 1 , name : -1 } , { name : "compound_index" } )
42+ end
43+ # end create compound index
44+
45+ # start create 2dsphere index
46+ class Restaurant
47+ include Mongoid ::Document
48+
49+ field :location , type : Point
50+
51+ index ( { location : "2dsphere" } , { name : "location_index" } )
52+ end
53+ # end create 2dsphere index
54+
55+ # start create sparse index
56+ class Restaurant
57+ include Mongoid ::Document
58+
59+ field :name , type : String
60+ field :cuisine , type : String
61+ field :borough , type : String
62+
63+ index ( { borough : 1 } , { sparse : true } )
64+ end
65+ # end create sparse index
66+
67+ # start list index
68+ Restaurant . indexes . each { |index | puts index }
69+ # end list index
70+
71+ # start create multiple indexes
72+ class Restaurant
73+ include Mongoid ::Document
74+
75+ field :name , type : String
76+ field :cuisine , type : String
77+ field :borough , type : String
78+
79+ index ( { name : 1 } )
80+ index ( { cuisine : -1 } )
81+ end
82+
83+ Restaurant . create_indexes
84+ # end create multiple indexes
85+
86+ # start drop indexes
87+ Restaurant . remove_indexes
88+ # end drop indexes
89+
90+ # start create atlas search index
91+ class Restaurant
92+ include Mongoid ::Document
93+
94+ field :name , type : String
95+ field :cuisine , type : String
96+ field :borough , type : String
97+
98+ search_index ( { name : 1 } )
99+ end
100+
101+ Restaurant . create_search_indexes
102+ # end create atlas search index
103+
104+ # start remove atlas search index
105+ Restaurant . remove_search_indexes
106+ # end remove atlas search index
107+
108+ #start list atlas search index
109+ Restaurant . search_indexes . each { |index | puts index }
110+ # end list atlas search index
0 commit comments