Sequence Numbers:序列号
传统的数据库中,通常用一个递增的序列来提供主键,在 MongoDB
中用 ObjectId 的来代替,我们可以通过如下的函数来获取主键





function
counter(name) { var ret = db.counters.findAndModify({query:{_id:name}, update:{$inc : {next:1}}, "new":true, upsert:true}); return ret.next; } db.users.insert({_id:counter("users"), name:"Sarah C."}) // _id : 1 db.users.insert({_id:counter("users"), name:"Bob D."}) // _id :2

 

相关文章:

  • 2022-12-23
  • 2021-09-29
  • 2022-03-11
  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
  • 2022-03-05
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
相关资源
相似解决方案