c# Driver: how to add dynamic array

148 views
Skip to first unread message

Tom DeMille

unread,
Mar 14, 2011, 9:16:17 AM3/14/11
to mongodb-user, Robert Stam, rstam...@gmail.com
morning, I'm trying to add a dynamic array of booleans as a single
object so that I can use multikey approach to index (http://
www.mongodb.org/display/DOCS/Using+Multikeys+to+Simulate+a+Large+Number+of+Indexes)

I can't figure out the proper way to do this with the c# driver, my
object arrays end up looking like this:

attribs : [
... { \"Value\": false },
... { \"Value\" : true },
... { \"Value\" : false },
... { \"Value\" : true } ]
... };

but should look like this

attribs : [
... { HasPool : false },
... { HasGarage : true },
... { HasBasement : false },
... { IsHOA : true } ]
... };

code I'm using below, trying to approaches..

BsonDocument newDoc = new BsonDocument();
var boolAttribs = new BsonDocument();
var bsonArray = new BsonArray();


boolProps.ForEach(delegate(System.Reflection.PropertyInfo p)
{

boolAttribs.Add(new BsonElement(p.Name,
true));
bsonArray.Add(new BsonElement(p.Name,
true).ToBsonDocument());

});



newDoc.Add(MapToMongoFieldName("BooleanAttributes"), boolAttribs);

newDoc.Add(MapToMongoFieldName("BooleanAttributes2"), bsonArray);

Robert Stam

unread,
Mar 14, 2011, 10:07:23 AM3/14/11
to mongodb-user
I struggled a bit trying to reproduce your sample because there just
seems to be a lot of code missing.

Here's some sample code that produces the output you want, so maybe
you can start from this and work backwards to your use case:

http://www.pastie.org/1670261

Let me know if you have any questions about this sample code or how to
apply it to your use case.

On Mar 14, 9:16 am, Tom DeMille <tdemi...@gmail.com> wrote:
> morning, I'm trying to add a dynamic array of booleans as a single
> object so that I can use multikey approach to index (http://www.mongodb.org/display/DOCS/Using+Multikeys+to+Simulate+a+Large+Numb...)

Tom DeMille

unread,
Mar 14, 2011, 10:44:52 AM3/14/11
to mongodb-user, rstam...@gmail.com
You sample helped, I had been adding a bsonelement to the bsonarray,
adding a bsondocument instead produced the desired object array..


Now I struggling with how to produce a query to search this embedded
array using the querybuilder classes in the c# driver..

How would one build a query like this?


db.foo.find( { attribs : {avail:false, hasfireplace:true,
hassomethingelse:false} } );

Tom DeMille

unread,
Mar 14, 2011, 10:50:45 AM3/14/11
to mongodb-user
or rather, the correct syntax for MongoDB would be this:


db.foo.find({attribs: { $all: [{HasThing:true}, {Hasotherthing:
false}] }})


how to build in querybuilder?

Robert Stam

unread,
Mar 14, 2011, 12:08:25 PM3/14/11
to mongodb-user
I would write that query in C# like this:

var query = Query.All("attribs", new BsonArray {
new BsonDocument("HasThing", true),
new BsonDocument("HasOtherThing", false)
});
json = query.ToJson();
Console.WriteLine(json);

Converting the query object to a JSON string is a good testing
technique that you can use to make sure you are creating the right
query.

Here's the sample program I used to test with:

http://www.pastie.org/1670674

Tom DeMille

unread,
Mar 14, 2011, 12:46:11 PM3/14/11
to mongodb-user
Thanks Robert, appreciate your help as I get used to the conventions
of the driver..

Robert Stam

unread,
Mar 14, 2011, 1:29:45 PM3/14/11
to mongodb-user
No problem. Happy I could help.
Reply all
Reply to author
Forward
0 new messages