SSPkrolik / nimongo

Pure Nim lang MongoDB driver
http://sspkrolik.github.io/nimongo
MIT License
101 stars 20 forks source link

[critical] inserted oid's have low entropy with lots of 0's (5bef5960c7dcd43e00000001 instead of 5bef58c408de32fe5a33a29b) #65

Closed timotheecour closed 5 years ago

timotheecour commented 5 years ago

in mongo shell (likewise with other clients):

db.tnimongo.insert({"name":"bob"})
db.tnimongo.find({name:"bob"})
{
  "_id": ObjectId("5bef58c408de32fe5a33a29b"),
  "name": "bob"
}

with nimongo:

import std/[
  oids,
  asyncdispatch,
]
import nimongo/bson
import nimongo/mongo

proc test_sync2=
  var m = newMongo()
  doAssert m.connect()
  let mc = m["tmp"]["tnimongo"]

  let doc = %*{
    "name": "bob2",
  }
  echo doc
  let ai = mc.insert(doc)
  echo ai
  doAssert ai

test_sync2()

prints:

{
    "name" : "bob2"
}
(ok: true, n: 1, err: "", inserted_ids: @[{"$oid": "5bef5a69a6d2183f00000001"}], bson: {
    "n" : 1,
    "ok" : 1.0
})

ie, there are 7 zeros, which reduces the entropy, and increases chances of collisions among other things

EDIT

root cause is https://github.com/nim-lang/Nim/issues/9741

timotheecour commented 5 years ago

EDIT: https://github.com/nim-lang/Nim/issues/9741 was finally fixed

and i just re-ran above code:

{
    "name" : "bob2"
}
(ok: true, n: 1, err: "", inserted_ids: @[{"$oid": "5c52b31b9595251b396ce0f8"}], bson: {
    "n" : 1,
    "ok" : 1.0
})

which now works