Am 13.03.2017 um 16:34 schrieb Carl Sturtivant:

On Mon, 13 Mar 2017 00:04:31 GMT, Alexey Kulentsov wrote:

On Sun, 12 Mar 2017 23:09:53 GMT, Carl Sturtivant wrote:

auto cursor = db[collectionname].find();
foreach( rec; cursor) {
    s = rec[fieldname].to!string;
    // rec[fieldname] is a Bson string
}

Here s is assigned quoted text looking like a string literal if printed, i.e. as if Bson is being converted to Json text, suitable for display in the console or a log.

However, what if we want to get the string, not its representation as a string literal?

We can do this if we use a struct to represent each of the results of find, but this is not possible if the structure of the collection is not known at compile time. So how do we proceed in this dynamic case?

I don't use bson, but according to http://vibed.org/api/vibe.data.bson/ it's rec[fieldname].get!string - the same as with json.

You duplicated the code in my message just above. Not an answer: produces the string literal encoding of the text I want to retrieve.

Looks like deserializeBson does what I want.

There is a subtle difference: to <-> get. The problem is that
to!string has to work in a consistent way for any type of BSON value
stored. Using a JSON representation was just the most obvious approach.

get!T on the other hand is meant to actually extract the raw value.
But deserializeBson will also work fine, especially if multiple fields
need to be extracted at once.