datanoise / mongo.cr

Crystal binding for MongoDB C Driver
MIT License
97 stars 35 forks source link

Insert a document with array of hash #24

Closed amiceli closed 1 year ago

amiceli commented 7 years ago

When I try to insert a doc with an array of tuple :

og = [{"property"=>"og:title","content"=>"Open Graph protocol"},{"property"=>"og:type","content"=>"website"}]

bookmarkCollection.insert({
  "link" => "https://angular.io",
  "og" => og,
  "tags" => [firstTag, secondTag]
})

I've following error :

no overload matches 'BSON#[]=' with types String, (BSON | Mongo::Cursor | Nil)
 - BSON#[]=(key, value : Int64)
 - BSON#[]=(key, value : Binary)
 - BSON#[]=(key, value : Bool)
 - BSON#[]=(key, value : Float64 | Float32)
 - BSON#[]=(key, value : MinKey)
 - BSON#[]=(key, value : MaxKey)
 - BSON#[]=(key, value : Nil)
 - BSON#[]=(key, value : ObjectId)
 - BSON#[]=(key, value : String)
 - BSON#[]=(key, value : Symbol)
 - BSON#[]=(key, value : Time)
 - BSON#[]=(key, value : Timestamp)
 - BSON#[]=(key, value : Code)
 - BSON#[]=(key, value : BSON)
 - BSON#[]=(key, value : Regex)
Couldn't find overloads for these types:
 - BSON#[]=(key : String, value : Mongo::Cursor)

        bson[i.to_s] = item
elrok123 commented 7 years ago

@amiceli You need to use .to_bson on your hash array there, hopefully should fix the issue, this implementation makes use of binary JSON, hence the BSON errors.