Giter VIP home page Giter VIP logo

Comments (3)

datanoise avatar datanoise commented on August 19, 2024

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

from mongo.cr.

 avatar commented on August 19, 2024

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

from mongo.cr.

 avatar commented on August 19, 2024

Found the error, used id not _id. Sorry.

from mongo.cr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.