ScottPJones / Mongo.jl

Mongo bindings for the Julia programming language
Other
43 stars 21 forks source link

update() deletes other fields not explicitly stated in update query #16

Closed IanButterworth closed 8 years ago

IanButterworth commented 8 years ago

My understanding is that the update function should just affect the fields stated in the update query, but it seems to delete every other field in the record, leaving just the updated field.

oid = insert(collection,("a" => 1,"b"=>2,"c"=>3)
update(collection, ("_id" => did), ("a" => 323))

Result: ("a" => 323)

not ("a" => 323,"b"=>2,"c"=>3)

ghost commented 8 years ago

mongo.db requires that you use special "update operators" in order to update specific fields, leaving the others as-is. This seems to be supported in the Julia implementation as suggested in the unit tests:

        update(
            collection,
            ("_id" => oid),
            set("hello" => "after")
            )

Note the set(...) operation. Without any update operators, mongo.db will replace the entire "document" (using mongo.db terminology), which means effectively removing any fields that aren't provided. For more info on this, see https://docs.mongodb.org/manual/reference/method/db.collection.update/#example-update-specific-fields

ghost commented 8 years ago

BTW this is another place where a small update to the docs would be greatly appreciated -- again, if it's not obvious then it's worth spelling out!

IanButterworth commented 8 years ago

Great. That worked as promised. Thanks. I agree that it's not obvious. 'Set' should be listed as an alternative to 'inc' in the update example