You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _includes/js/queries.md
+183Lines changed: 183 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -205,6 +205,39 @@ The above example will match any `BarbecueSauce` objects where the value in the
205
205
206
206
Queries that have regular expression constraints are very expensive, especially for classes with over 100,000 records. Parse restricts how many such operations can be run on a particular app at any given time.
207
207
208
+
### Full Text Search
209
+
210
+
You can use `fullText` for efficient search capabilities. Text indexes are automatically created for you. Your strings are turned into tokens for fast searching.
211
+
212
+
* Note: Full Text Search can be resource intensive. Ensure the cost of using indexes is worth the benefit, see [storage requirements & performance costs of text indexes.](https://docs.mongodb.com/manual/core/index-text/#storage-requirements-and-performance-costs).
213
+
214
+
* Parse Server 2.5.0+
215
+
216
+
<pre><codeclass="javascript">
217
+
var query = new Parse.Query(BarbecueSauce);
218
+
query.fullText('name', 'bbq');
219
+
</code></pre>
220
+
221
+
The above example will match any `BarbecueSauce` objects where the value in the "name" String key contains "bbq". For example, both "Big Daddy's BBQ", "Big Daddy's bbq" and "Big BBQ Daddy" will match.
222
+
223
+
<pre><codeclass="javascript">
224
+
// You can sort by weight / rank. ascending('') and select()
225
+
var query = new Parse.Query(BarbecueSauce);
226
+
query.fullText('name', 'bbq');
227
+
query.ascending('$score');
228
+
query.select('$score');
229
+
query.find()
230
+
.then(function(results) {
231
+
// results contains a weight / rank in result.get('score')
232
+
})
233
+
.catch(function(error) {
234
+
// There was an error.
235
+
});
236
+
</code></pre>
237
+
238
+
239
+
240
+
For Case or Diacritic Sensitive search, please use the [REST API](http://docs.parseplatform.org/rest/guide/#queries-on-string-values).
208
241
209
242
## Relational Queries
210
243
@@ -391,3 +424,153 @@ mainQuery.find()
391
424
// There was an error.
392
425
});
393
426
</code></pre>
427
+
428
+
## Aggregate
429
+
430
+
Queries can be made using aggregates, allowing you to retrieve objects over a set of input values. The results will not be `Parse.Object`s since you will be aggregating your own fields
431
+
432
+
* Parse Server 2.7.1+
433
+
*`MasterKey` is Required.
434
+
435
+
Aggregates use stages to filter results by piping results from one stage to the next.
436
+
437
+
You can create a pipeline using an Array or an Object.
438
+
439
+
The following example is a pipeline similar to `distinct` grouping by name field.
440
+
441
+
<pre><codeclass="javascript">
442
+
var pipelineObject = {
443
+
group: { objectId: '$name' }
444
+
};
445
+
446
+
var pipelineArray = [
447
+
{ group: { objectId: '$name' } }
448
+
];
449
+
</code></pre>
450
+
451
+
For a list of available operators please refer to [Mongo Aggregate Documentation](https://docs.mongodb.com/v3.2/reference/operator/aggregation/).
452
+
453
+
* Note: Most operations in Mongo Aggregate Documentation will work with Parse Server, but `_id` doesn't exist. Please replace with `objectId`.
454
+
455
+
Group pipeline is similar to `distinct`.
456
+
457
+
You can group by a field.
458
+
459
+
<pre><codeclass="javascript">
460
+
// score is the field. $ before score lets the database know this is a field
461
+
var pipeline = [
462
+
group: { objectId: '$score' }
463
+
];
464
+
var query = new Parse.Query("User");
465
+
query.aggregate(pipeline);
466
+
query.find()
467
+
.then(function(results) {
468
+
// results contains unique score values
469
+
})
470
+
.catch(function(error) {
471
+
// There was an error.
472
+
});
473
+
</code></pre>
474
+
475
+
You can apply collective calculations like $sum, $avg, $max, $min.
476
+
477
+
<pre><codeclass="javascript">
478
+
// total will be a newly created field to hold the sum of score field
Schema is the structure representing classes in your app. You can use the schema
4
+
of an app to verify operations in a unit test, generate test data, generate test
5
+
classes and then clean up after tests. The schema API can also be used to create
6
+
custom views of your data. We use the schema API to display column names and
7
+
types in the databrowser.
8
+
9
+
This API allows you to access the schemas of your app.
10
+
11
+
* Parse Server 2.7.1+
12
+
*`MasterKey` is required.
13
+
14
+
Schema will return an object similar to the following:
15
+
16
+
<pre><codeclass="javascript">
17
+
{
18
+
className: 'MyClass',
19
+
fields: {
20
+
// Default fields
21
+
ACL: {type: 'ACL'},
22
+
createdAt: {type: 'Date'},
23
+
updatedAt: {type: 'Date'},
24
+
objectId: {type: 'String'},
25
+
// Custom fields
26
+
aNumber: {type: 'Number'},
27
+
aString: {type: 'String'},
28
+
aBool: {type: 'Boolean'},
29
+
aDate: {type: 'Date'},
30
+
aObject: {type: 'Object'},
31
+
aArray: {type: 'Array'},
32
+
aGeoPoint: {type: 'GeoPoint'},
33
+
aPolygon: {type: 'Polygon'},
34
+
aFile: {type: 'File'}
35
+
},
36
+
classLevelPermissions: {
37
+
find: {
38
+
'*': true
39
+
},
40
+
create: {
41
+
'*': true
42
+
},
43
+
get: {
44
+
'*': true
45
+
},
46
+
update: {
47
+
'*': true
48
+
},
49
+
addField: {
50
+
'*': true
51
+
},
52
+
delete: {
53
+
'*': true
54
+
}
55
+
},
56
+
indexes: {
57
+
indexName: { aString: 1 },
58
+
}
59
+
}
60
+
</code></pre>
61
+
62
+
Direct manipulation of the classes that are on your server is possible through ParseSchema. Although fields and classes can be automatically generated (the latter assuming client class creation is enabled) ParseSchema gives you explicit control over these classes and their fields.
63
+
64
+
*With great power comes great responsibility. Altering the schema directly should be done with care, you can't go back to retrieve data if you remove a field and it's associated values.*
65
+
66
+
<pre><codeclass="javascript">
67
+
// create an instance to manage your class
68
+
const mySchema = new Parse.Schema('MyClass');
69
+
70
+
// gets the current schema data
71
+
mySchema.get();
72
+
73
+
// returns schema for all classes
74
+
Parse.Schema.all()
75
+
76
+
// add any # of fields, without having to create any objects
77
+
mySchema
78
+
.addString('stringField')
79
+
.addNumber('numberField')
80
+
.addBoolean('booleanField')
81
+
.addDate('dateField')
82
+
.addFile('fileField')
83
+
.addGeoPoint('geoPointField')
84
+
.addPolygon('polygonField')
85
+
.addArray('arrayField')
86
+
.addObject('objectField')
87
+
.addPointer('pointerField', '_User')
88
+
.addRelation('relationField', '_User');
89
+
90
+
// new types can be added as they are available
91
+
mySchema.addField('newField', 'ANewDataType')
92
+
93
+
// save/update this schema to persist your field changes
94
+
mySchema.save().then((result) => {
95
+
// returns save new schema
96
+
});
97
+
// or
98
+
mySchema.update().then((result) => {
99
+
// updates existing schema
100
+
});
101
+
</code></pre>
102
+
103
+
Assuming you want to remove a field you can simply call `deleteField` and `save/update` to clear it out.
104
+
105
+
<pre><codeclass="javascript">
106
+
mySchema.deleteField('stringField');
107
+
mySchema.save();
108
+
// or for an existing schema...
109
+
mySchema.update();
110
+
</code></pre>
111
+
112
+
## Indexes
113
+
114
+
Indexes support efficient execution of queries from the database. Keep in mind that the `masterKey` is required for these operations, so be sure it's set in your initialization code before you use this feature.
115
+
116
+
<pre><codeclass="javascript">
117
+
// To add an index, the field must exist before you create an index
118
+
mySchema.addString('stringField');
119
+
const index = {
120
+
stringField: 1
121
+
};
122
+
mySchema.addIndex('stringFieldIndex', index);
123
+
mySchema.save().then((result) => {
124
+
// returns schema including index stringFieldIndex and field stringField
125
+
});
126
+
127
+
// Delete an index
128
+
testSchema.deleteIndex('indexName');
129
+
mySchema.save().then((result) => {
130
+
// returns schema without indexName index
131
+
});
132
+
133
+
// If indexes exist, you can retrieve them
134
+
mySchema.get().then((result) => {
135
+
// result.indexes
136
+
});
137
+
</code></pre>
138
+
139
+
## Purge
140
+
141
+
All objects can be purged from a schema (class) via purge. But *be careful*! This can be considered an irreversible action. Only do this if you really need to delete all objects from a class, such as when you need to delete the class (as in the code example above).
0 commit comments