在Sails中创建应用程序时如何覆盖Mongo数据库中的对象ID [英] How to overwrite object Id's in Mongo db while creating an App in Sails

查看:95
本文介绍了在Sails中创建应用程序时如何覆盖Mongo数据库中的对象ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Sails和Mongo Db的新手.当前,我正在尝试使用Sails实现CRUD功能,我想在Mongo db中保存用户详细信息.在模型中,我具有以下属性

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
   }
} 

我想确保_id被我的值从100开始覆盖,并随着每个新条目自动递增.我正在使用水线模型,当我在DHC中调用Api时,会得到以下输出

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"

此处给出的ID是对象ID.有人可以告诉我如何使用从100开始的Integer覆盖对象ID,并随每个新值自动递增.

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.

推荐答案

注意:Mongo ID应该为 ObjectId 由时间戳,计算机ID,进程ID和随机递增值.只留下后者会使它易于发生碰撞.

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.

但是,有时您非常想美化永无休止的ObjectID值(即在编码).然后,您应该考虑使用适当的原子增量策略.

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.

覆盖 _id示例:

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

制作自动递增序列:

  • 使用计数器集合:基本上是一个单独的集合,用于跟踪序列的最后一个数字.就个人而言,我发现将findAndModify函数存储在system.js集合中更具凝聚力,尽管它
  • 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

我找到了问题,其中sails-mongo的所有者说:

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

MongoDb没有自动递增属性,因为它没有 支持它,而无需对 单独的收藏或文件.我们目前不在 适配器,但可以在将来添加,或者如果有人想添加 提交公关我们对Sails-disk和Sails-redis做类似的操作 获得对autoIncremeting字段的支持.

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.

他提到了我在此答案中添加的第一个技术: 使用计数器集合.在同一期中, lewins 显示了一种解决方法.

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

这篇关于在Sails中创建应用程序时如何覆盖Mongo数据库中的对象ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆