-
-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Embedded document: add auto increment id does not work. #15
Comments
Hello. // for the sensorModule schema
SensorModelSchema.plugin(AutoIncrement, {id: 'sensor_model_id_counter'});
// and for the sensor schema
SensorSchema.plugin(AutoIncrement, {id: 'sensor_id_counter'}); |
Any news on this? Did it worked? |
I noticed that you're using a SequenceArchive singleton to verify existing fields. Which means that whenever a new Schema sets the same field as counter, the singleton checks the fields based on all the mongoose defined Schemas. I also noticed that the method
That might trigger the error that @Castrovalva is getting. |
I think this won't fix the problem. If we change the way you say it will avoid the problem from @Castrovalva but then it will increment the same counter for the two different schemas. Maybe I'm wrong or I can't see what you're sayng (easily both) and so if you want to provide a PR with the modification you're welcome.
I prefer the first solution as it is robust even for cross-application counters. |
You're right. I reviewed the collection where the counters are being saved and noticed that By enforcing a mandatory rule for the id name, wouldn't that generate a breaking change? I guess most of the users are using Maybe I could do a PR to add the schema's name to the counters collection, this follows your second idea. Now the problem would be how to put that property into an existing counter collection. That might also break the current implementations. |
Yes I know enforcing the id is a breaking change and so the new version would be incompatible with the previous. As you say, considering the schema name can lead to another breaking change. Sadly I think the problem is due to a poor design of mine. I think I'll go for the mandatory id solution because it is easier and it has the same guarantees than the other. And, as a bonus point, it will not break any implementation which already set the id. If you think we can manage adding the schema name to the counter without breaking any implementation, please send your PR, it would be awesome! |
Enforcing the name ids would not cause any database breaking change (only API), however it would keep the rubbish counters. Uhm... what if when boostraping the plugin it could fetch the highest ids from each plugin enabled schema and assume that this would be the correct one to reassign to the Counters collection? I mean, counters are incremental only right? So the plugin shouldn't care about the lower ids, neither the database nor the application. So maybe it could do something like that:
The reference could be something like this:
I'm gonna fork this project and start implementing this idea after your thoughts. Ahh, btw this might works for both approaches. |
i am still facing ths issue . any solution ? |
If you specify the Id for the counter you should have no problem as said in other comments. |
It would be great if someone posts solution for this |
For someone with the same problem, and who did not understand the answer as I did in the beginning, this was my solution: My code with the error:
and it was fixed by changing the autoincrement part to this: for example: |
This resolved my issue. |
Thanks, that solved my problem! |
The example shows that the auto increment does not work on embedded documents.
This does not work:
The parent document implementation with mongoose-sequence
The embedded document implementation
The parent document's auto increment id does work, however when I add an auto increment id on my embedded document I get this error:
Error: Counter already defined for field "_id" at Sequence.getInstance (C:\Users\\Desktop\Programming\MonDoAPI\node_modules\mongoose-sequence\lib\sequence.js:69:15) at Schema.plugin (C:\Users\...\Desktop\Programming\MonDoAPI\node_modules\mongoose\lib\schema.js:1139:3) at Object.<anonymous> (C:\Users\...\Desktop\Programming\MonDoAPI\models\sensorModule.model.js:21:19) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object.<anonymous> (C:\Users\...\Desktop\Programming\MonDoAPI\controllers\sensorModule.controller.js:1:84) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) [nodemon] app crashed - waiting for file changes before starting...
The text was updated successfully, but these errors were encountered: