On Thursday, 20 September 2012 12:53:17 UTC-7, SID wrote:
> 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();
> On Wednesday, 19 September 2012 12:22:38 UTC-7, craiggwilson wrote:
>> 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?
>> On Wednesday, September 19, 2012 2:04:12 PM UTC-4, SID wrote:
>>> 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
>>> On Tuesday, 4 September 2012 19:30:11 UTC-7, Robert Stam wrote:
>>> 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.
>>>> On Tue, Sep 4, 2012 at 10:27 PM, Robert Stam <rob...@10gen.com> wrote:
>>>>> 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:
>>>>> https://jira.mongodb.org/browse/CSHARP-539
>>>>> On Tue, Sep 4, 2012 at 6:32 PM, SID <sidhart...@gmail.com> wrote:
>>>>>> 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.****