Query Bson Documents directly using Linq

3,311 views
Skip to first unread message

SID

unread,
Sep 4, 2012, 6:32:22 PM9/4/12
to mongodb...@googlegroups.com

I have  Json data whose full structure I don't know. I want to store this data in MongoDb using c# driver.

I couldn't use c# dynamic keyword as this is not supported by Mongo C# Serializer.

 

I worked around this by creating Bson documents based on json passed. 

 

var bsonDocument = BsonSerializer.Deserialize<BsonDocument>(json);

testcollection.Insert(bsonDocument,SafeMode.True);

 

I want to use linq to query such documents without using a type so that I can get back a list of BsonDocuments matching a criteria.

I was thinking a of creating ExpressionTree visitor to convert linq ->javascript. I see that in mongo c# driver we can create query using javascript

var script = new BsonJavaScript(stringJs);

var mongoQuery = Query.Where(script)

 

Is there any other way I can do this.

Robert Stam

unread,
Sep 4, 2012, 10:27:04 PM9/4/12
to mongodb...@googlegroups.com
The current version of the C# driver (1.6) does not yet support C# dynamic types.

Support for C# dynamic types has not yet been scheduled, so I don't know when it will be added. You can follow the JIRA ticket:

Robert Stam

unread,
Sep 4, 2012, 10:30:09 PM9/4/12
to mongodb...@googlegroups.com
In addition, I would recommend that you avoid the Where clause with Javascript, because the Javascript engine in the server only allows one Javascript program to run at one time, and also is unable to use any indexes.

If you want to write your own translation your target should be an IMongoQuery (which you probably would create using the Query builder), not Javascript.

SID

unread,
Sep 19, 2012, 2:04:12 PM9/19/12
to mongodb...@googlegroups.com
Hi Robert,

Thanks for your suggestion.

I used the follwoing approach .

1. Get the MongoQueryable<T> for a collection

var provider = new MongoQueryProvider(collection);

return new MongoQueryable<T>(provider);

2. Create a linq query using the MongoQueryable<T>

3. use ((MongoQueryable<T>)query).GetMongoQuery() to convert a linq query to a valid monogo query.

4. Executing the mongo queries on collection returns valid BSON document.

Best Regards,

Sid

craiggwilson

unread,
Sep 19, 2012, 3:22:38 PM9/19/12
to mongodb...@googlegroups.com
I'm really confused as to how and why you are doing this.  Would you mind posting a fuller code sample that demonstrates your need and how you are accomplishing what you are doing?

SID

unread,
Sep 20, 2012, 3:53:17 PM9/20/12
to mongodb...@googlegroups.com
Hi Craig,
The data structure that client gives is dynamic with few common fields.
An example is shown below.
{
    "Id": "E4D6C0D4-7C09-4838-B96A-2574F66FA6F5",
    "Resource": [
        {
            "Id": "E4D6C0D4-7C09-4838-B96A-2574F66F1234",
            "Task": "Task1",
            "Parameter": {
                "Publish": "udp://127.0.0.1:1234",
                "Duration": 15
            }
        },
        {
            "Id": "E4D6C0D4-7C09-4838-B96A-2574F66F1234",
            "Task": "Task2",
            "Parameter": {
                "Publish": "some uri",
                "Retry": 3,
                "Validate": "True"
            }
        }
    ]
}
 
I am storing this data directly usng  Bson documents as we don't have a structure defined.
We wanted to have linq support for such a collection but as mongo driver doesn't support dynamic  I can't write something like
 

var query = from dynamic p in collection

where p.Id== id

select p;

 

To solve this I was using mongo query builder to write my queries which worked but is a little bit cumbersome.

Now my approach is to get the IQueryable using

var queryable = new MongoQueryProvider(collection);

return new MongoQueryable<T>(provider);

Create linq query

var query = from p in queryable

where p.Id== Id

select p;

To Execute this query I convert this query into mongo query

 

var mongoquery = ((MongoQueryable<T>)query).GetMongoQuery();

Then I can get the cursor using get cursor method

private static MongoCursor GetCursor(MongoCollection collection, IMongoQuery query)

        {

var cursor = MongoCursor.Create(typeof(BsonDocument), collection, query);

 

return cursor;

}

Now I can itierate the cursor

var result = cursor.Cast<BsonDocument>().ToList();

SID

unread,
Sep 21, 2012, 5:42:49 PM9/21/12
to mongodb...@googlegroups.com
One more thing I forgot to add is that we have some wrapper classes to expose common data.
These classes wrap JSON data and expose properties like ID.
Reply all
Reply to author
Forward
0 new messages