Skip to content
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

needs documentation in README: insert(bson) modifies bson; re-inserting will have no effect #67

Open
timotheecour opened this issue Nov 17, 2018 · 0 comments

Comments

@timotheecour
Copy link
Contributor

timotheecour commented Nov 17, 2018

this generates a single insert:

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": "bob4",
  }

  for i in 0..<2:
    # doc["number"] = toBson i
    # echo doc
    let ai = mc.insert(doc)
    echo ai
    doAssert ai

test_sync2()

the equivalent code in D generates 2 inserts:

/+dub.sdl:
dependency "vibe-d" version="~>0.8.0"
+/

/+
[[critical] mongo errors silently ignored · Issue #66 · SSPkrolik/nimongo](https://github.com/SSPkrolik/nimongo/issues/66)
+/

import vibe.data.bson;
import vibe.db.mongo.mongo;
import std.stdio;

void main(){
  auto name = "case2";
  test(name);
}

void test(string name)
{
  MongoClient client = connectMongoDB("127.0.0.1");
  MongoCollection users = client.getCollection("tmp.tnimongo");
  if(name == "case2"){
    auto a = Bson(["name": "bob3".Bson]);
    users.insert(a);
    users.insert(a);
  }
}

likewise with other clients, eg the mongo shell client

foo={name:"bob5"}
db.tnimongo.insert(foo)
db.tnimongo.insert(foo)
db.tnimongo.count()
2

edit

in python, it behaves same as Nim (well almost, minus the fact that python will throw, see #66)

import pymongo
from pymongo import MongoClient
client = MongoClient()
cm = client.tmp.tnimongo
obj = {"name": "bob6"}
for i in range(2):
  print(obj)
  ret = cm.insert_one(obj)
  print(obj)
  print(ret.inserted_id)

maybe behavior of Nim is ok, and derives from fact that Bson is a reference type (unlike Bson in Nim); this could have advantages in terms of efficiency (avoiding copies), but IMO should be clearly documented in README that insert modifies input Bson (unlike other implementations, eg D, mongo shell)

workaround

pending nim-lang/Nim#9740 we can do:

let ai = mc.insert(doc.deepCopy)
@timotheecour timotheecour changed the title [critical] inserting the same Bson object doesn't re-generate _id, resulting in a single insert needs documentation in README: insert(bson) modifies bson; re-inserting will have no effect Nov 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant