# Add object to existing BSON file?

**URL:** <https://discourse.julialang.org/t/add-object-to-existing-bson-file/41305>\
**Category:** Data\
**Tags:** bson, filesystem\
**Created:** [June 12, 2020, 11:41pm UTC](https://discourse.julialang.org/t/add-object-to-existing-bson-file/41305 "2020-06-12T23:41:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [June 12, 2020, 11:41pm UTC](https://discourse.julialang.org/t/add-object-to-existing-bson-file/41305/1 "2020-06-12T23:41:54Z")

</div>

Suppose I saved a BSON file with `BSON.@save "path/to/file.bson" obj1 obj2` . Then later on I create a new `obj3` and I want to also add to this file, as if I had executed `BSON.@save "path/to/file.bson" obj1 obj2 obj3` in the first place (but I can’t do that). At this point I no longer have `obj1,obj2` in memory. What’s the best way of adding `obj3` to the bson file? Do I have to `BSON.@load "path/to/file.bson" obj1 obj2` first to recover `obj1,obj2` and then `BSON.@save "path/to/file.bson" obj1 obj2 obj3` ? Or is there a more direct way?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [June 13, 2020, 1:44am UTC](https://discourse.julialang.org/t/add-object-to-existing-bson-file/41305/2 "2020-06-13T01:44:25Z")

</div>

BSON only reads the first serialized object, and this syntax saves a dict with keys `"a"` and `"b"`

```julia
a=5
b=6
c=7
 BSON.@save "path/to/file.bson" a b

```

You can append `c` with

```julia
open("path/to/file.bson", "a") do io
  BSON.@save io c
end

```

But then you need to issue two reads to get everything back

```julia
julia> open("test.bson", "r") do io
            println(BSON.load(io))
            println(BSON.load(io))
       end
Dict{Symbol,Any}(:a => 5,:b => 6)
Dict{Symbol,Any}(:c => 7)

```

Not very convenient, but maybe useful?
