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
@@ -42,7 +47,7 @@ A commodity collection named `counters` is created for you. You can override the
42
47
43
48
If you want to increment the `_id` field which is special to mongoose, you have to explicitly specify it as a Number and tell mongoose to not interfer:
44
49
45
-
```javascript
50
+
```js
46
51
UserSchema =mongoose.Schema({
47
52
_id:Number,
48
53
name:String
@@ -54,55 +59,53 @@ In this case you don't have to specify `inc_field` because the default value is
54
59
55
60
## Not automatic sequences
56
61
57
-
Let say our user model has a `like` field which counts how many likes the user has in our amazing application. This field is of course a sequence but has to be incremented everytime an event occours. Because we have concurrent access to database we want to be sure that the increment of this counter happens safely.
62
+
Let say our user model has a `rank` field which cgives the rank of the user in a tournament. So it saves the arrival order of a user to the end of our amazing game. This field is of course a sequence but has to be incremented everytime an event occours. Because we have concurrent access to database we want to be sure that the increment of this counter happens safely.
58
63
Let's start modify our schema
59
64
60
-
```javascript
65
+
```js
61
66
UserSchema =mongoose.Schema({
62
67
name:String,
63
-
like:Number
68
+
rank:Number
64
69
});
65
70
```
66
71
67
-
this time we specified explicitly the field `like`. There is no difference between defining and omitting the field specification. The only constraints is that the field has to be of type `Number`, otherwise the plugin will raise an error.
68
-
So, let's say to the plugin we want the `like` field to be a safe counter
72
+
this time we specified explicitly the field `rank`. There is no difference between defining and omitting the field specification. The only constraints is that the field has to be of type `Number`, otherwise the plugin will raise an error.
73
+
So, let's say to the plugin we want the `rank` field to be a safe counter
We specified `disable_hooks`. This avoid the field to be incremented when a new document is saved. So, how to increment this field? Your models has a new method: `setNext`. You must specify which sequence you want to increment and a callback. Here an example:
79
+
We specified `disable_hooks`. This avoid the field to be incremented when a new document is saved. So, how to increment this field? Your models have a new method: **setNext**. You must specify which sequence you want to increment and a callback. Here an example:
if(err) console.log('Cannot increment the likes because',err);
83
+
user.setNext('rank', function(err, user){
84
+
if(err) console.log('Cannot increment the rank because',err);
80
85
});
81
86
});
82
87
```
83
88
84
89
You noticed that the method `setNext` takes, as argument, the counter field name. Is possible to give a name to the counter and use it as reference. For the previous example we can define the counter like this:
So, if you not specify the `id`, the field name is used. Even if you're not forced to specify an id, its use is strongly suggested. This because if you have two different counters, which refers to fields with the same name, they will collide and incrementing one, will increment the other too.
99
-
So use unique id to be sure to avoid collision. In any case any collision led to an error by the plugin.
103
+
So, if you not specify the `id`, the field name is used. Even if you're not forced to specify an id, its use is strongly suggested. This because if you have two different counters, which refers to fields with the same name, they will collide and incrementing one, will increment the other too. Counters are not bound to the schema they refer too, so two counters for two different schemas can collide.
104
+
So use unique id to be sure to avoid collision. In case of collision the plugin will raise an error.
100
105
101
106
As we will see, the use of an id for the counter is mandatory when you're are defining a `scoped counter`.
102
107
103
-
**NOTE**: When you call `setNext` the document is automatically saved. This behavior is different from version <=2.0.0
104
-
105
-
108
+
**NOTE**: When you call `setNext` the document is automatically saved. This behavior has changed since version 3.0.0. If you use a prior version you have to call save by yourself.
106
109
107
110
## Advanced
108
111
@@ -111,24 +114,24 @@ As we will see, the use of an id for the counter is mandatory when you're are de
111
114
Let say our users are organized for `country` and `city`. And we want to save the `inhabitant_number` according to the two informations.
112
115
The schema is like this:
113
116
114
-
```javascript
117
+
```js
115
118
UserSchema =mongoose.Schema({
116
119
name:String,
117
120
country:String,
118
121
city:String,
119
-
inhabitants:Number
122
+
inhabitant_number:Number
120
123
});
121
124
```
122
125
123
-
Every time a new Parisian is added the counting of Parisians have to be incremented. The inhabitants of New York must not interfer and have their separated counting. We should define a __scoped__ counter which says, increment the counter, depending on the value of other fields.
126
+
Every time a new Parisian is added the counting of Parisians have to be incremented. The inhabitants of New York must not interfer and have their separated counting. We should define a __scoped__ counter which increment the counter depending on the value of other fields.
Notice that we have to use an id for our sequence, otherwise the plugin will raise an error.
130
133
Now save a new user
131
-
```
134
+
```js
132
135
var user =newUser({
133
136
name:'Patrice',
134
137
country:'France',
@@ -137,25 +140,25 @@ var user = new User({
137
140
user.save();
138
141
```
139
142
140
-
This user will have the `inhabitants` counter to 1.
141
-
If now we add a new inhabitant from New York, this will have the counter to 1 also, because the counter is referenced to the value of the fields country and city.
143
+
This user will have the `inhabitant_number` counter to 1.
144
+
If now we add a new inhabitant from New York, this will have the counter to 1 also, because the counter is referred to the value of the fields `country` and `city`.
142
145
143
146
If we want to increment manually this counter we have to specify the id of the sequence in the `setNext` method
Of course this example is a bit forced and this is for sure not the perfect use case. The field country and city have to be present and must not change during the life of the document because no automatic hook are set on the change of those values. But there are situation when you want a similar behaviour.
154
+
Of course this example is a bit forced and this is for sure not the perfect use case. The field country and city have to be present and must not change during the life of the document because no automatic hook are set on the change of those values. But there are situations when you want a similar behavior.
152
155
153
156
### Options
154
157
155
158
This plugin accept a series of options.
156
159
157
-
-__inc_field__: The name of the field to increment. Mandatory, default is `_id`
158
-
-__id__: Id of the sequence. Is mandatory only for scoped sequences but its use is strongly encouraged.
159
-
-__reference_fields__: The field to reference for a scoped counter. Optional
160
-
-__disable_hooks__: If true, the counter will not be incremented on saving a new document
161
-
-__collection_name__: By default the collection name for the counters is `counters`. You can override it using this option
160
+
-**inc_field**: The name of the field to increment. Mandatory, default is `_id`
161
+
-**id**: Id of the sequence. Is mandatory only for scoped sequences but its use is strongly encouraged.
162
+
-**reference_fields**: The field to reference for a scoped counter. Optional
163
+
-**disable_hooks**: If true, the counter will not be incremented on saving a new document. Default to `false`
164
+
-**collection_name**: By default the collection name to mantain the status of the counters is `counters`. You can override it using this option
0 commit comments