comtihon / mongodb-erlang Goto Github PK
View Code? Open in Web Editor NEWMongoDB driver for Erlang
License: Apache License 2.0
MongoDB driver for Erlang
License: Apache License 2.0
thanks
Hi, Mr Christkv, When I use mongodb-erlang's modify function,I had a problem.Could give me some tips, thank you very much!
erlang code ---
test_modify() ->
Actions = fun() -> mongo:modify(c1, {x,{'$lt',100}}, {y,10}) end,
do(Actions).
do(Actions) ->
case test_connection() of
{ok, Conn} ->
{State, Reason} = mongo:do(safe, master, Conn, testdb, Actions),
case {State, Reason} of
{failure, Reason} ->
mongo:disconnect(Conn),
{failure, Reason};
{ok, Result} ->
mongo:disconnect(Conn),
{ok, Result}
end;
{error, Reason} ->
{error, Reason}
end.
end code -----
my collections is :
mongo shell ----
db.c1.find();
{ "_id" : ObjectId("503b252196d6a81fb4000005"), "x" : 5, "y" : 6 }
{ "_id" : ObjectId("503b26ef96d6a81fb4000006"), "x" : 10, "y" : 11 }
{ "_id" : ObjectId("503b26ef96d6a81fb4000007"), "x" : 12, "y" : 13 }
{ "_id" : ObjectId("503b26ef96d6a81fb4000008"), "x" : 14, "y" : 15 }
end mongo shell ---
When running : test_modify/0 , there catch a error:
error code ---
{failure,{write_failure,10158,
<<"multi update only works with $ operators">>}}
end error code ---
Loading documents that contains Infinity
fails due to BSON parsers can't decode the float:
// insert in mongo shell
db.test.insert({"hello": Infinity})
# try to query back
:poolboy.transaction :mongodb, fn(conn) -> :mongo.find_one(conn, :test, %{}) end
** (exit) exited in: :gen_server.call(#PID<0.3001.0>, {:query, false, false, false, false, :test, 0, -1, %{}, []}, :infinity)
** (EXIT) an exception was raised:
** (MatchError) no match of right hand side value: <<0, 0, 0, 0, 0, 0, 240, 127>>
(bson) src/bson_binary.erl:87: :bson_binary.get_field/4
(bson) src/bson_binary.erl:144: :bson_binary.get_field/2
(bson) src/bson_binary.erl:54: :bson_binary.get_fields/2
(bson) src/bson_binary.erl:40: :bson_binary.get_map/1
(mongodb) src/core/mongo_protocol.erl:111: :mongo_protocol.get_docs/3
(mongodb) src/core/mongo_protocol.erl:92: :mongo_protocol.get_reply/1
(mongodb) src/connection/mc_worker_logic.erl:68: :mc_worker_logic.decode_responses/2
(mongodb) src/connection/mc_worker.erl:102: :mc_worker.handle_info/2
There is a pull request for it in the bson-erlang
repo put i'm posting it here for reference.
Hi,
I can't figure out which api method to use to write a "distinct" query with both keys and selectors.
Any help please ?
i want to use $and query with this erlang driver, any one can show me an example here please? thank you!
Hi,
currently I'm using the following erlang tuple to generate an "_id" with BinData(3, binary):
generate_objectid_subtype3() ->
{bin, uuid, uuid:uuid4()}.
How can I achieve that MongoDB will get a BinData of Type 4?
When I test SCRAM-SHA-1 auth mechanism in single mongod instance, it seems everything goes well. But when I setup a replica set cluster and connect one member in the cluster, it report "not authorized on xxx collection to execute command xxx" when I issue a COUNT command.
Does mongodb-erlang support the maps syntax ?
maps is easy for insert and match the query result
Example:
mongo:insert(Connection, Collection, #{name => <<"Tom">>, age => 22 }),
mongo:update(Connection, Collection, #{'$set' => #{age => 33 }}),
Data = mongo:find_one(Connection, Collection, #{age => 30}),
#{name := Name} = Data
Found in master brunch, not found in refactor.
Is there any reason to remove it?
Hello, I'm trying to connect to remote mongo server 2.6.4 like this
mongo:connect([{database, list_to_binary(DB)}, {host, Host}, {port, Port}, {login, list_to_binary(U)}, {password, list_to_binary(P)}])
and getting this error
[error] CRASH REPORT Process <0.376.0> with 1 neighbours crashed with reason: bad argument in call to erlang:iolist_to_binary([{ok.......<<"my_login_here">>......in crypto:hash/2 line 223
Connection like this
mongo:connect([{database, list_to_binary(DB)}, {host, Host}, {port, Port}
works just fine in the same time.
Any ideas?
say i have a document like this:
{user:
{nick:"joy.cood",
phone: "+8613533332221",
email:"[email protected]"
}}
i will get the email field only, what the query i write should like?
Hi
I was looking for authentication stuff and figured out that it existed until this commit by @asabil
mongodb@47ddb64#diff-809c6e623041846b3814a1175d444e08
Do you plan to add it again ? =)
branch main,
mongo:add_user
system.users missing single quotes.
While trying to connect to mongodb with login and password, i am getting <<"Can't pass authentification">>.
Login & Password are correct and working with other application.
mc_worker:start_link([{database, <<"database">>},{login, <<"admin">>},{password, <<"password">>},{host,"x.x.x.x"},{port,27017}]).
[error] CRASH REPORT Process <0.478.0> with 1 neighbours crashed with reason: <<"Can't pass authentification">> in mc_auth_logic:scram_sha_1_auth/4 line 34
I want use selector {field_name, '$regex', "/data/i"}
find data
mongod-2.6version is ok
mongod-3.0version is badValue '$regex' to a string
Hi,
I want to do such command:
db.students.find({age: {$gte: 25}});
what should i do ? thanks!
Hi, could you please provide example how to use connect/6 with empty username
and password
Why I need this?
I have not auth, but want to tune DB server IP and port.
Thanks in advance.
mongo:find_one(my_collection, {<<"_id">>, <<82,29,123,102,44,196,167,32,184,0,0,1>>}). %% Does not work, nor does replacing with <<"ObjectId("my_id")">> or the base64 representation.
How do I query for the id?
mongodb office command lilke below:
db.runCommand( {
geoNear: ,
near: { type: "Point" , coordinates: [ ] } ,
spherical: true,
...
} )
http://docs.mongodb.org/manual/reference/command/geoNear/
how to do the same with mongodb-erlang driver?
case dict:size(UReqStor) of
0 -> ok;
_ -> inet:setopts(State#state.socket, [{active, once}]) %TODO why not always active true?
end,
When I use poolboy, mc_worker can't recognize tcp closed bucause socket not set active once when mc_worker is idle
Could you please add licensing terms? I know this driver has long history starting from 10gen but anyway.
The readme doesn't go with the refactor version.
Hi
I have been using this plugin to connect to mongohq
While I can establish an initial connection to the databse it throws an error everytime i try to do a query such as find:
=ERROR REPORT==== 15-Jul-2013::12:34:45 ===
Conn: GET /
Reason: (MatchError) no match of right hand side value: {:failure, :unauthorized}
Stacktrace:
lib/dynamodemoapp/models/task.ex:2: Tasks.find_all/2
The custom code above is just a wrapper to :mongo.find
It seems that there is an authorization error failure? and i can't work out why it is causing it as it uses the same connection parameters as if you are connecting to localhost?
any help or pointers will be useful
thanks!
https://github.com/mongodb/mongodb-erlang/blob/v0.3.1/ebin/mongodb.app app version is 0.2.1 but should be 0.3.1.
Does anyone know about ensure_syntax?
I tried
mongo:ensure_index(DBConn, conversations, {key, {page_id, 1}, unique, true, dropDups, true})
but not work at all
It throws undefined function error
$ erl -pa ebin deps/*/ebin
Erlang R16B (erts-5.10.1) [source-05f1189] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.1 (abort with ^G)
1> application:start(bson).
ok
2> application:start(mongodb).
ok
3> mongo:connect({localhost, 27017}).
** exception error: undefined function mongo:connect/1
Why?
How to connect ro replica set, And reconnect to the new master if old one die ?
Hi,
currently I insert datetimes as ISO8601 timestamp string but I'd like to change that to the MongoDB Datatype ISODate.
How can I achieve this?
Thanks
In this project, it seems not support gridfs. How to solve this problem?
If I'm correct currently mongodb-erlang
uses old opcode-based implementation. With new commands you don't need to do ConfirmWrite
to confirm success. Also they return more information than just confirmation.
insert
reference http://docs.mongodb.org/manual/reference/command/insert/
update
reference http://docs.mongodb.org/manual/reference/command/update/
delete
reference http://docs.mongodb.org/manual/reference/command/delete/
This driver seems very nice. I use it in my elixir project.
This is my code to connect to my remote mongo server. But it returns error.
iex> :mongo.connect("db_name", "account", "password", "safe", "master", [{"host", "example.com"}, {"port", 27017}])
** (EXIT from #PID<0.88.0>) {:errmsg, "auth failed", :code, 18}
(I've tried to connect by my local mongo client and it works.)
Is this right format of the :mongo.connect/6 command when use in elixir?
Please help me!
I have been searching everywhere, I can't find for the life of me how to sort a find query and ordering it... has it been forget ?
Other less supported drivers does it, I do see why this one couldn't be able to...
Any clue would be greatly appreciated.
Hi,
I'm trying to write an Erlang application that connects to MongoDB and I looked into using this client driver.
I notice that soundrop/bson-erlang (the rebar dependency) maps document field names to Erlang atoms using binary_to_atom. This is visible in the bson parser code (bson_binary:get_fields) and also mentioned in the blog post, http://blog.mongodb.org/post/7270427645/design-of-the-erlang-driver-for-mongodb .
I think it's very dangerous to use binary_to_atom like that, since atoms are not garbage collected and so document collections containing too many different field names can overrun memory in the Erlang node. I'd consider this a critical bug (in my application I can't control what's in documents and might even get malicious ones) so I can't use the driver like that. I'd urge a refactoring to not use atoms in this unsafe way, that the Erlang documentation advises against.
I thought about just modifying the driver and sending you a patch, but since it would be an API change, I decided I better ask your advice about it first. It's of course also possible that I'm missing something, in which case enlightenment is always appreciated. Please let me know your thoughts.
(Note: I tried emailing this but it bounced).
Thanks
--Paul
Subj.
First of all: installing into lib directory returned by code:lib_dir()
does not work as per instructions. In reality you have to create two directories:
one for mongodb, one for bson, e.g.
/usr/lib64/erlang/lib/mongo-master
and
/usr/lib64/erlang/lib/bson-master
Then it'll work.
Second - I'm now reading through code, as module mongo doesn't have connect function. I guess I have to use mongo_connect module, but there is no mention of it in docs, so one has to dig into the code. Bless open-source, by the way :)
I`am use finde_one method which returns bson object. After that In observer i see not closed connect to db (object mc_worker module). How close this connection? Method disconnect/1 http://api.mongodb.org/erlang/mongodb/mongo.html#disconnect-1 not implemented
I followed the README and got stuck at trying to connect to my local instance with no auth.
$ erl -pa mongodb/ebin mongodb/deps/bson/ebin
Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9.1 (abort with ^G)
1> application:start (bson).
ok
2> application:start (mongodb).
ok
3> Database = <<"test">>.
<<"test">>
4> {ok, Connection} = mongo:connect (Database).
** exception error: undefined function mongo:connect/1
the compile went fine, without errors.
Any suggestions?
p.s. i read the issue from 2 years ago that the api may have changed, but I saw the README being updated today, so I assume it correct now...
Версия монги 3.0.
Версия erlang 18.
Cypto запущено.
Подключение:
ok, Connection} = mongo:connect ([
{database, <<"dbname">>},
{login, <<"user">>},
{password, <<"pass">>}
])
в итоге "Can't pass authentification".
В эксепшене:
"A: error, B: {badmatch,{false,#{<<99,111,100,101>>=>18,<<101,114,114,109,115,103>>=><<65,117,116,104,101,110,116,105,99,97,116,105,111,110,32,102,97,105,108,101,100,46>>}}}".
Либо с другой учёткой:
"A: error, B: undef"
=CRASH REPORT==== 18-Aug-2015::12:47:27 ===
crasher:
initial call: mc_worker:init/1
pid: <0.96.0>
registered_name: []
exception error: <<"Can't pass authentification">>
in function mc_auth_logic:scram_sha_1_auth/4 (src/core/mc_auth_logic.erl, line 42)
in call from mc_worker:init/1 (src/connection/mc_worker.erl, line 41)
ancestors: [<0.94.0>]
messages: []
links: [<0.94.0>,#Port<0.1592>]
dictionary: [{random_seed,{879,21160,14733}}]
trap_exit: false
status: running
heap_size: 987
stack_size: 27
reductions: 2920
neighbours:
neighbour: [{pid,<0.94.0>},
{registered_name,[]},
{initial_call,{erlang,apply,2}},
{current_function,{proc_lib,sync_wait,2}},
{ancestors,[]},
{messages,[]},
{links,[<0.26.0>,<0.96.0>]},
{dictionary,[]},
{trap_exit,false},
{status,waiting},
{heap_size,233},
{stack_size,29},
{reductions,498}]
** exception exit: <<"Can't pass authentification">>
С теми же данными из php подключаюсь без проблем. Куда копать?
Following error while compiling Bson in Mongodb
src/bson_binary.erl:55: illegal use of variable 'Name' in map
How could I use the refactory erlang driver with mongo replica set?
I am writing backend storage for Erl db abstraction lib kvs.
There is start
fun in kvs, that establish connection with mongoDB. How I can store connection, to re-use for future queries?
I understand that result of connection is PID of connection process, but what is next?
How mongodb-erlang
suggest that external apps will use it? any recommendation?
I have tried the bson:utf8() method... but doesn't help.
Basically how do I turn the binary representation into a string-like representation?
<<83,22,19,109,142,79,136,52,52,0,0,1>>
--> "5314e0d68e4f8823fb000001"
When compiling the driver using the cmd 'erlc -o ebin -I include -I .. src/*.erl
', I get the error info:
src/mongo.erl:333: syntax error before: '.'
src/mongo.erl:32: function add_user/3 undefined
src/mongo.erl:330: spec for undefined function mongo:add_user/3
And then I fixed it with:
before
add_user (Permission, Username, Password) ->
User = case find_one (system.users, {user, Username}) of {} -> {user, Username}; {Doc} -> Doc end,
Rec = {readOnly, case Permission of read_only -> true; read_write -> false end, pwd, pw_hash (Username, Password)},
save (system.users, bson:merge (Rec, User)).
after
add_user (Permission, Username, Password) ->
User = case find_one ('system.users', {user, Username}) of {} -> {user, Username}; {Doc} -> Doc end,
Rec = {readOnly, case Permission of read_only -> true; read_write -> false end, pwd, pw_hash (Username, Password)},
save ('system.users', bson:merge (Rec, User)).
Can someone give me an example of connect to a remote mongodb server with username and password? I tried several ways but didn't work.
Best Regards,
I want to use mc_worker.erl terminate function to notice other process when mongo server close.
but when mongod kill or dead mc_worker:terminate not called.
what happed?
Our server is using mongoerl in master branch. We found a bug which causes our server down about every month in mongoerl. The trace is as follows:
["web request failed",
{path,"l"},
{type,error},
{what,{badmatch,{-2146148680,
{reply,false,false,true,0,0,
[{setName,<<"rs0">>,ismaster,true,secondary,false,
hosts,
[<<"ip-10-121-13-170:27017">>,
<<"ip-10-121-12-150:27017">>,
<<"ip-10-121-12-108:27017">>],
primary,<<"ip-10-121-13-170:27017">>,me,
<<"ip-10-121-13-170:27017">>,
maxBsonObjectSize,16777216,
maxMessageSizeBytes,48000000,localTime,
{1371,74346,818000},
ok,1.0}]},
<<>>}}},
After looking into the source of mongodb-erlang, I think this is mainly because that after request_id was requested more than 2147483647 times(every time it will use ets:update_counter to increase by 1), it will be larger than 32 bit int, then this error will occur. We are using this in production environment so we need a urgent fix. I checked refactor branch, it seems not many documents on that new branch with new interfaces and so it's impossible for us to risk upgrading it to new. Could anyone please tell me what to do to avoid this problem, probably a quick fix for this in mongoerl?
Thanks very much
Hi, is there any plan to support SCRAM-SHA-1 auth mechanism to work with MongoDB 3.0 ?
Best regards,
Morgan.
In file rebar.config change atom fail_on_warning to warnings_as_errors.
Atom fail_on_warning is deprecated
I'm trying to get all the distinct values of some collection, I can get the result by executing db.$cmd.findOne({distinct: collection_name, key: some_key})
from within the shell. However, when I do:
mongo:do(safe, master, DbConn, some_db,
fun() ->
mongo:command(
{
distinct, some_collection,
key, some_key
}
)
end
)
I always get a blank list. I'm working on Ubuntu 12.04 + MongoDb 2.2.1 + Erlang R15B02.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.