3

I am new to Sails and Mongo Db. Currently I am trying to implement a CRUD Function using Sails where I want to save user details in Mongo db.In the model I have the following attributes

"id":{
  type:'Integer',
  min:100,
  autoincrement:true
},
attributes: {
    name:{
     type:'String',
     required:true,
     unique:true
 },
    email_id:{
       type:'EMAIL',
       required:false,
       unique:false
},
   age:{
     type:'Integer',
     required:false,
     unique:false
   }
} 

I want to ensure that the _id is overridden with my values starting from 100 and is auto incremented with each new entry. I am using the waterline model and when I call the Api in DHC, I get the following output

"name": "abc"
"age": 30
"email_id": "abc@gmail.com"
"id": "5587bb76ce83508409db1e57"

Here the Id given is the object Id.Can somebody tell me how to override the object id with an Integer starting from 100 and is auto incremented with every new value.

shubhamagiwal92
  • 1,362
  • 4
  • 25
  • 47

1 Answers1

4

Attention: Mongo id should be unique as possible in order to scale well. The default ObjectId is consist of a timestamp, machine ID, process ID and a random incrementing value. Leaving it with only the latter would make it collision prone.

However, sometimes you badly want to prettify the never-ending ObjectID value (i.e. to be shown in the URL after encoding). Then, you should consider using an appropriate atomic increment strategy.

Overriding the _id example:

db.testSOF.insert({_id:"myUniqueValue", a:1, b:1})

Making an Auto-Incrementing Sequence:

  • Use Counters Collection: Basically a separated collection which keeps track the last number of the sequence. Personally, I have found it more cohesive to store the findAndModify function in the system.js collection, although it lacks version control's capabilities.
  • Optimistic Loop

Edit:

I've found an issue in which the owner of sails-mongo said:

MongoDb doesn't have an auto incrementing attribute because it doesn't support it without doing some kind of manual sequence increment on a separate collection or document. We don't currently do this in the adapter but it could be added in the future or if someone wants to submit a PR. We do something similar for sails-disk and sails-redis to get support for autoIncremeting fields.

He mentions the first technique I added in this answer: Use Counters Collection. In the same issue, lewins shows a workaround.

TechWisdom
  • 3,960
  • 4
  • 33
  • 40
  • hi TechWisdom, I tried using _id too and it gave me the same error. Since I using Sails for making an app, we need to specify the attributes in the model file. When given with specification for the _id, I assigned its type to an integer and auto incremented it . Even on specifying the requirements in the model, i am getting the object id. I don't understand why _id is not getting the particular settings i set in the model? – shubhamagiwal92 Jun 22 '15 at 08:44
  • Hi @shubhamagiwal, does [setting autoPK to false](https://github.com/balderdashy/waterline#options) in Model Settings help you? – TechWisdom Jun 22 '15 at 08:49
  • I did as shown above and I got the same objectid. Is there anything else you can suggest – shubhamagiwal92 Jun 22 '15 at 11:49
  • @shubhamagiwal Seems like unimplemented feature. Look at my new edit. – TechWisdom Jun 22 '15 at 12:21