nimongo
nimongo copied to clipboard
[critical] mongo errors silently ignored
the following code should fail on the 2nd time doAssert ai is reached because insert fails (we're inserting with same _id):
import std/[
oids,
asyncdispatch,
]
import nimongo/bson
import nimongo/mongo
proc test_sync2=
var m = newMongo()
doAssert m.connect()
let mc = m["tmp"]["tnimongo"]
for i in 0..<2:
let doc = %*{
"name": "bob2",
"_id": 123,
}
let ai = mc.insert(doc)
echo ai
doAssert ai
test_sync2()
here's the equivalent D code, which correctly throws:
/+dub.sdl:
dependency "vibe-d" version="~>0.8.0"
+/
import vibe.db.mongo.mongo;
import std.stdio;
void main(){
test();
}
import vibe.data.bson;
import vibe.data.json;
import vibe.db.mongo.mongo;
void test()
{
MongoClient client = connectMongoDB("127.0.0.1");
MongoCollection users = client.getCollection("tmp.tnimongo");
//users.insert(Bson(["name": "bob".Bson]));
users.insert(Bson(["name": "bob2".Bson, "_id": 10.Bson]));
/+
this will (correctly) throw since insert fails
vibe.db.mongo.connection.MongoDBException@../../../../../.dub/packages/vibe-d-0.8.4/vibe-d/mongodb/vibe/db/mongo/connection.d(472): E11000 duplicate key error collection: tmp.tnimongo index: _id_ dup key: { : 10 }
----------------
t10_mongo.d:30 pure @safe bool std.exception.enforce!(bool).enforce(bool, lazy object.Throwable) [0xf69b244]
../../../../../.dub/packages/vibe-d-0.8.4/vibe-d/mongodb/vibe/db/mongo/connection.d:472 @safe void vibe.db.mongo.connection.MongoConnection.checkForError(immutable(char)[]) [0xf6d4452]
../../../../../.dub/packages/vibe-d-0.8.4/vibe-d/mongodb/vibe/db/mongo/connection.d:241 @safe void vibe.db.mongo.connection.MongoConnection.insert(immutable(char)[], vibe.db.mongo.flags.InsertFlags, vibe.data.bson.Bson[]) [0xf6d3183]
t10_mongo.d:30 @safe void vibe.db.mongo.collection.MongoCollection.insert!(vibe.data.bson.Bson).insert(vibe.data.bson.Bson, vibe.db.mongo.flags.InsertFlags) [0xf6a2017]
t10_mongo.d:26 void t10_mongo.test() [0xf69651c]
t10_mongo.d:11 _Dmain [0xf696178]
+/
users.insert(Bson(["name": "bob2".Bson, "_id": 10.Bson]));
foreach(a; users.find(["name": "bob"])){
writeln(a);
}
}
likewise with python, which also throws
note
echo ai prints this:
the ok: true should be ok: false, and it should fail
(ok: true, n: 1, err: "", inserted_ids: @[123], bson: {
"n" : 1,
"ok" : 1.0
})
(ok: true, n: 0, err: "", inserted_ids: @[123], bson: {
"n" : 0,
"writeErrors" : [
{
"index" : 0,
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: tmp.tnimongo index: _id_ dup key: { : 123 }"
}
],
"ok" : 1.0
})
friendly ping