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

How to find document by Object ID? #19

Closed
ghost opened this Issue Mar 17, 2017 · 3 comments

Comments

Projects
None yet
1 participant
@ghost
Copy link

ghost commented Mar 17, 2017

What I want: Find a Document by the Document ID.

My Code and tries:

Collection.update({ "_id" => { "$oid" => ANYVALIDID } }, { "$set" => { "title" => "Hello World" } })
# Not working

id = BSON::ObjectId.new(doc["id"].to_s)
Coolection.update({ "_id" => id }, { "title" => "Hello World" })
# Not working. Error:
# Index out of bounds (IndexError)
# 0x5d9b1f: *BSON#[]<String>:(BSON | BSON::Code | BSON::MaxKey | BSON::MinKey | BSON::ObjectId | # BSON::Symbol | BSON::Timestamp | Bool | Float64 | Int32 | Int64 | Regex | String | Time | Nil) at /app/lib/mongo/src/bson.cr 127:18

Any Ideas
@datanoise

@datanoise

This comment has been minimized.

Copy link
Owner

datanoise commented Mar 17, 2017

The general idea is this:

require "./src/mongo"

client = Mongo::Client.new "mongodb://localhost"
db = client["test"]
c = db["coll"]

id = BSON::ObjectId.new
c.insert({"_id" => id, "name" => "James Bond"})
doc = c.find({"_id" => id}).first
puts doc

You can also obtain the ObjectId from String:

require "./src/mongo"

client = Mongo::Client.new "mongodb://localhost"
db = client["test"]
c = db["coll"]

id = BSON::ObjectId.new "58cc42dee56f3a0ab841f672"
doc = c.find({"_id" => id}).first
puts doc
@ghost

This comment has been minimized.

Copy link
Author

ghost commented Mar 17, 2017

That works, but my problem is, I cannot create an Object ID Object from a BSON object I searche before.

Code:

collection.find({ "age" => { "$gt" => 30 } }) do |doc|
  id = BSON::ObjectId.new doc["id"]

  collection.update({ "_id" => id }, { "age" => 29 })
end

Error:

in src/model/frage_model.cr:23: no overload matches 'BSON::ObjectId.new' with type (BSON | BSON::Code | BSON::MaxKey | BSON::MinKey | BSON::ObjectId | BSON::Symbol | BSON::Timestamp | Bool | Float64 | Int32 | Int64 | Regex | String | Time | Nil)
Overloads are:
 - BSON::ObjectId.new(handle : ::Pointer(LibBSON::Oid))
 - BSON::ObjectId.new(str : String)
 - BSON::ObjectId.new()
Couldn't find overloads for these types:
 - BSON::ObjectId.new(BSON)
 - BSON::ObjectId.new(BSON::Code)
 - BSON::ObjectId.new(BSON::MaxKey)
 - BSON::ObjectId.new(BSON::MinKey)
 - BSON::ObjectId.new(BSON::ObjectId)
 - BSON::ObjectId.new(BSON::Symbol)
 - BSON::ObjectId.new(BSON::Timestamp)
 - BSON::ObjectId.new(Bool)
 - BSON::ObjectId.new(Float64)
 - BSON::ObjectId.new(Int32)
 - BSON::ObjectId.new(Int64)
 - BSON::ObjectId.new(Regex)
 - BSON::ObjectId.new(Time)
 - BSON::ObjectId.new(Nil)

  id = BSON::ObjectId.new doc["id"]

@datanoise

@ghost ghost closed this Mar 17, 2017

@ghost

This comment has been minimized.

Copy link
Author

ghost commented Mar 17, 2017

Found the error, used id not _id. Sorry.

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment