orlandos-nl / BSON

Native Swift library for BSON (http://bsonspec.org)
https://orlandos.nl/docs/mongokitten/articles/bson
MIT License
110 stars 35 forks source link

Values with same keys get inserted as a new value instead of replacing old #37

Closed CosmicYogi closed 2 years ago

CosmicYogi commented 6 years ago

I was working with MongoKitten today and found a weird and unexpected behavior i.e.., the existence of duplicate keys. Please refer the image below for better clearity.

screen shot 2018-01-12 at 4 26 33 pm

What I had tried to do and noticed is as follows -

let gotFromDb = try collection.find(["email": eMail_]) var user = try gotFromDb.find().first Till here 👆 is the same code(or common code), and after that I am getting different results based on different and those are the things I want to share here.

If I do the following - user.append("Mitesh", forKey: "something") user["something"] = ["ABCD", "eFgH"] try collection.update(["email": _eMail], to: user, upserting: false, multiple: false, writeConcern: nil, stoppingOnError: true) I face the issue of getting the duplicate keys.

Next case, If I do this - let myName: Document = ["myName": "\(arc4random_uniform(UInt32(999)))"] user.append(contentsOf: myName) -> This will not create an entry with duplicate key instead it will update to the new value. user.append(myName) -> This will create an entry with duplicate key try collection.update(["email": _eMail], to: user, upserting: false, multiple: false, writeConcern: nil, stoppingOnError: true)

Next case, If I do this - let tokenDocument: Document = ["lalla": ["\(arc4random_uniform(UInt32(999)))", "XKCD", "somethign", "Ron"]] user.append(contentsOf: tokenDocument) try collection.update(["email": _eMail], to: user, upserting: false, multiple: false, writeConcern: nil, stoppingOnError: true) It works properly I mean the new value overrites the previous one and no value with duplicate key is added.