checkiz / elixir-mongo

MongoDB driver for Elixir
MIT License
101 stars 31 forks source link

update replaces, not updates #3

Closed ryanhall-ttec closed 10 years ago

ryanhall-ttec commented 10 years ago

{:ok, mongo} = Mongo.connect db = mongo.db("test") coll = db.collection("user") coll.find.toArray Returns : [%{_id: ObjectId(5336737fd17892e2c42db0da), some_nested: [%{things: "like this"}, %{and: "like that"], login: "MyUsername"}

Then I run coll.update(%{login: "MyUsername"}, %{login: "MyOtherUsername"})

coll.find(%{login: "MyOtherUsername"}).toArray returns: [%{_id: ObjectId(5336737fd17892e2c42db0da), login: "MyOtherUsername"}]

jerp commented 10 years ago

I believe, this is the expected feature of the update command of MongoDB, see db.collection.update()

To update specific fields of the retrieved document, use this:

coll.update(%{login: "MyUsername"}, %{"$set": %{ login: "MyOtherUsername" }}

doing so, you'll get this:

iex(1)> coll = Mongo.connect!.db("test").collection("user")
{Mongo.Collection, "user",...
iex(2)> coll.drop
...
iex(3)> coll.insert_one %{some_nested: [%{things: "like this"}, %{and: "like that"}], login: "MyUsername"}
{:ok, ...
iex(4)> coll.update %{login: "MyUsername"}, %{"$set": %{login: "MyOtherUsername"}} 
:ok
iex(5)> coll.find(%{login: "MyOtherUsername"}).toArray
[%{_id: ObjectId(53a820fe210c035c35375b1d), login: "MyOtherUsername",
   some_nested: [%{things: "like this"}, %{and: "like that"}]}]