I am trying to query a MongoDB database from Vibe and am having trouble figuring out how to specify non-trivial queries.
I have a collection called images, which contains a set of documents describing images, and includes among its fields a
title field and, of course, the _id field.
I would like to execute a query that returns just the titles from each document. From the Mongo client this query looks
as follows:
db.images.find( { }, { title: 1 } )
and it returns records that look like:
{ "_id" : ObjectId("522b08dde8d4a38eb60cc4eb"), "title" : "Dog" }
{ "_id" : ObjectId("522f44f555d703955051bd6d"), "title" : "Cat" }
{ "_id" : ObjectId("522f450b55d703955051bd6e"), "title" : "House" }
In my vibe project I have the following code that I hope will replicate the above query:
Now would I make the same query from the Vibe MongoDB client (currently this code just prints the results to
the log):
foreach( img; images.find( Bson.EmptyObject, ["title"] ) ) {
logInfo("Image: %s", img.toJson() );
}
This prints only the _id field, not the title field as I had hoped.
Image: {"_id":"522b08dde8d4a38eb60cc4eb"}
Image: {"_id":"522f44f555d703955051bd6d"}
Image: {"_id":"522f450b55d703955051bd6e"}
I've tried numerous variants on this, including (I will skip all the combinations I tried before finding a reference to Bson.EmptyObject in an older post on this forum :o) :
Bson.EmptyObject, ["{title: 1}"]
Bson.EmptyObject, ["title:", "1"]
Bson.EmptyObject, ["title"]
Also, how would I specify something along the lines of, if I just want
images with 'Dog' in the title.
db.images.find( { title: /Dog/ }, { title: 1 } )
I tried poking through the source on GitHub to figure this out, but I guess my D-coding isn't quite up to snuff yet.
Cheers,
Craig