RejectedSoftware Forums

Sign up

MongoDB driver + passing Json/Bson over REST

After trying a lot of stuff from docs I can't find short and convenient way to return result of collection.find() as a single Bson or Json object from REST interface. What is a "default" way?

Re: MongoDB driver + passing Json/Bson over REST

Am 10/17/2012 7:19 AM, schrieb mist:

After trying a lot of stuff from docs I can't find short and convenient
way to return result of collection.find() as a single Bson or Json
object from REST interface. What is a "default" way?

Json restMethod()
{
	return collection.findOne(BsonEmpty.Object).get!Json;
	// or
	return cast(Json)collection.findOne(BsonEmpty.Object);
}

should work. I'll improve the Bson docs on that one and maybe add
toJson/fromJson methods to Bson, so that it can be used directly in the
REST interface (although with type info loss in that case).

Re: MongoDB driver + passing Json/Bson over REST

May be this is the most stupid question ever, but: how can I build up a Json[] from collection.find()? ( not findOne )
MongoCursor provides only opApply and I can't find any way to grow Json[] length dynamically in foreach loop.

On Wed, 17 Oct 2012 07:50:50 +0200, Sönke Ludwig wrote:

Am 10/17/2012 7:19 AM, schrieb mist:

After trying a lot of stuff from docs I can't find short and convenient
way to return result of collection.find() as a single Bson or Json
object from REST interface. What is a "default" way?

Json restMethod()
{
	return collection.findOne(BsonEmpty.Object).get!Json;
	// or
	return cast(Json)collection.findOne(BsonEmpty.Object);
}

should work. I'll improve the Bson docs on that one and maybe add
toJson/fromJson methods to Bson, so that it can be used directly in the
REST interface (although with type info loss in that case).

Re: MongoDB driver + passing Json/Bson over REST

To be clear: Not exactly Json[], but Json which is array internally.

On Sun, 21 Oct 2012 04:57:40 GMT, mist wrote:

May be this is the most stupid question ever, but: how can I build up a Json[] from collection.find()? ( not findOne )
MongoCursor provides only opApply and I can't find any way to grow Json[] length dynamically in foreach loop.

On Wed, 17 Oct 2012 07:50:50 +0200, Sönke Ludwig wrote:

Am 10/17/2012 7:19 AM, schrieb mist:

After trying a lot of stuff from docs I can't find short and convenient
way to return result of collection.find() as a single Bson or Json
object from REST interface. What is a "default" way?

Json restMethod()
{
	return collection.findOne(BsonEmpty.Object).get!Json;
	// or
	return cast(Json)collection.findOne(BsonEmpty.Object);
}

should work. I'll improve the Bson docs on that one and maybe add
toJson/fromJson methods to Bson, so that it can be used directly in the
REST interface (although with type info loss in that case).

Re: MongoDB driver + passing Json/Bson over REST

Hm.. I thought ~= was defined for Json, but for some reason I left it out in the initial commit of json.d. A workaround is of course to use an actual array:

Json[] arr;
foreach( doc; coll.find() )
    arr ~= doc.toJson();
auto json = Json(arr);

I'll look into why ~= is not defined.

Re: MongoDB driver + passing Json/Bson over REST

Ugh, all this makes me think part of bson/json will need kind of revamp one day to provide way to do such typical tasks more efficiently.

On Sun, 21 Oct 2012 08:04:10 GMT, Sönke Ludwig wrote:

Hm.. I thought ~= was defined for Json, but for some reason I left it out in the initial commit of json.d. A workaround is of course to use an actual array:

Json[] arr;
foreach( doc; coll.find() )
    arr ~= doc.toJson();
auto json = Json(arr);

I'll look into why ~= is not defined.

Re: MongoDB driver + passing Json/Bson over REST

Am 10/21/2012 10:21 AM, schrieb mist:

Ugh, all this makes me think part of bson/json will need kind of revamp
one day to provide way to do such typical tasks more efficiently.

On Sun, 21 Oct 2012 08:04:10 GMT, Sönke Ludwig wrote:

Hm.. I thought ~= was defined for Json, but for some reason I left
it out in the initial commit of json.d. A workaround is of course to
use an actual array:

Json[] arr;
foreach( doc; coll.find() )
    arr ~= doc.toJson();
auto json = Json(arr);

I'll look into why ~= is not defined.

Well, I wouldn't call it revamp ;) but certainly there are some corners
that can be improved. In the case of just returning a list of DB
documents I would imagine at least this should be possible:

auto json = Bson(coll.find().array()).toJson();

but that would more be MongoCursor, which needs to get a range
interface (or does array() already work on iterable things?)

The thing with JSON/BSON is that I would like to avoid a cyclic
dependency between the two; so Json may never directly know about Bson,
which restricts a bit of what is possible.