orlandos-nl / MongoKitten

Native MongoDB driver for Swift, written in Swift

Home Page:https://orlandos.nl/docs/mongokitten/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

InsertError unclear (same insert works in Robo3T with JavaScript)

tettoffensive opened this issue · comments

I'm doing an insert on with a simple document that just has 2 strings

{"key1": "value1", "key2": "value2"}
print(document)
let objectId = try collection.insert(document)

I get The operation couldn’t be completed. (MongoKitten.InsertErrors error 1.)

I copied the results of print(document) and tried it in Robo3T

db.getCollection('mycollection').insert({"key1": "value1", "key2": "value2"})

And it worked. And I'm able to use findOne in MongoKitten to find that element now. And all my other queries work.

I'm just not sure how to debug this if it's something with my code or MongoKitten?

(tried with both 4.0.0 and 4.1.1)

I'm not certain what the issue is reading just this. I'd suggest catching the error.

do {
  try collection.insert(document)
} catch let error as InsertError {
  print(error)
} catch {
  print(error)
}

@Joannis Thanks, I've tried that, but it's not really helpful.

This is what I get:

InsertErrors(errors: [], successfulIds: [ObjectId("5a09d73af5074ca207c5d784")])

successfulIds makes me think that the insert worked, but it doesn't actually do the insert.

do {
  try collection.insert(document)
} catch let error as InsertErrors {
  print(error)
} catch {
  print(error)
}

the insert did work then. I'm not sure why it's throwing an error, then. But it does look like it successfully inserted the entity. Did you verify manually if the entity exists?

Unfortunately, the entity does not exist in the collection.

I'm not sure then. I cannot look into this more deeply now. I'll pick this up in an hour or two or tomorrow morning. Is this result consistently reproducible?

Does this error occur locally or on a replica/sharded cluster? Do you know which MongoDB version it is? (MongoKitten has some MongoDB details inside server.buildInfo)

Yes, it happens every time. The error is local.

BuildInfo(gitVersion: "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e", 
          versionArray: [3,4,9,0], 
          version: MongoKitten.Version(major: 3, minor: 4, patch: 9), 
          storageEngines: Optional(["devnull","ephemeralForTest","mmapv1","wiredTiger"]), 
          bits: 64, 
         debug: false, 
         maxBsonObjectSize: 16777216, 
         openSSL: Optional({"running":"OpenSSL 1.0.2m  2 Nov 2017","compiled":"OpenSSL 1.0.2l  25 May 2017"}), 
         modules: [])

The MongoKitten version is obviously incorrectly recorded 😬 Looks perfect. I'll try to reproduce this

Thanks for the thorough info

Are you using the same user, collection and database in Studio3T and MongoKitten? I'm not able to reproduce the error, all is working here with no signs of problems.

It seems like you're lacking write permissions on this user, somehow. I know you said you're running locally, but did you mean that MongoKitten is local or is the MongoDB database local?

If the database is a replica/sharded cluster, maybe MongoKitten is trying to query a read-only node (in the replica)?

It does seem like that. I'm connected to: mongodb://localhost/mydatabase. I'm not using any username or password for localhost. And I'm able to write to other collections in the database.

Is there such a thing as per collection write permissions?

After much trial and error I have discovered the issue!

My Document was malformed - even though when you print(document) the appearance was the same. That's why when I copied and pasted it, the query worked. But I guess it was being seen as an array or something that made it not look like a singular object.

I had

var document: Document = []

And I changed it to = Document() to make it work

func toDocument() throws -> Document {
    var document: Document = Document()
    document.append(name, forKey: "name")
    if let id = id {
      let objectId = try ObjectId(id)
      document.append(objectId, forKey: "_id")
    }
    return document
}

@Joannis Seems resolved, close?

Yup 👍