Question: I'm trying to read a collection dump generated by mongodump. The file is a few gigabytes so I want to read it incrementally.

I can read the first object with something like this:

```
buf := make([]byte, 100000)
f, _ := os.Open(path)
f.Read(buf)

var m bson.M
bson.Unmarshal(buf, &m)

```

However I don't know how much of the buf was consumed, so I don't know how to read the next one.

Is this possible with mgo?

Answer: 
The method `File.Read` returns the number of bytes read.

[File.Read](http://golang.org/pkg/os/#File.Read)

> 
> Read reads up to len(b) bytes from the File. It returns the number of bytes read and an error, if any. EOF is signaled by a zero count with err set to io.EOF. 
> 
> 
> 

So you can get the number of bytes read by simply storing the return parameters of you read:

```
n, err := f.Read(buf)

```
[Niks Keets' answer](https://stackoverflow.com/a/24240292/447492) did not work for me. Somehow `len(r.Data)` was always the whole buffer length. So I came out with this other code:

```
for len(buff) > 0 {
    messageSize := binary.LittleEndian.Uint32(buff)
    err = bson.Unmarshal(buff, &myObject)
    if err != nil {
        panic(err)
    }

    // Do your stuff

    buff = buff[messageSize:]
}

```

Of course you have to handle truncated strucs at the end of the buffer. In my case I could load the whole file into memory.