Comments (3)
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.
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"]
from mongo.cr.
Found the error, used id
not _id
. Sorry.
from mongo.cr.
Related Issues (20)
- Spec fails with libmongoc 1.4.2 (Debian stretch)
- Unable to build on Debian stretch HOT 6
- Can't convert query result to array HOT 1
- Insert a document with array of hash HOT 1
- Undefined method [] for Nil:Class during compilation HOT 6
- How does one execute a command using the 'client.command()' method? HOT 3
- Would be nice to have a folder for some examples? HOT 2
- Mongo.cr and the event loop HOT 3
- undefined method 'to_bson' for JSON::Any (did you mean 'to_json'?) HOT 2
- upsert method
- get id of inserted document HOT 3
- how to work with BSON arrays? HOT 1
- update to work with Crystal 0.24.1 HOT 7
- How to find all documents from collection HOT 2
- [request] upgrade mongo-c driver version HOT 3
- Add support for Change Streams HOT 1
- Unhandled exception: File not open for writing (IO::Error) from usr/share/crystal/src/crystal/system/unix/file_descriptor.cr:23:11 in 'unbuffered_write' HOT 2
- Error when trying to retrieve value by key from BSON object
- Date Range Query
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongo.cr.