Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Query Bson Documents directly using Linq
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
SID  
View profile  
 More options Sep 4 2012, 6:32 pm
From: SID <sidharth.sa...@gmail.com>
Date: Tue, 4 Sep 2012 15:32:22 -0700 (PDT)
Local: Tues, Sep 4 2012 6:32 pm
Subject: Query Bson Documents directly using Linq

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Stam  
View profile  
 More options Sep 4 2012, 10:27 pm
From: Robert Stam <rob...@10gen.com>
Date: Tue, 4 Sep 2012 22:27:04 -0400
Local: Tues, Sep 4 2012 10:27 pm
Subject: Re: [mongodb-csharp] Query Bson Documents directly using Linq

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Stam  
View profile  
 More options Sep 4 2012, 10:30 pm
From: Robert Stam <rob...@10gen.com>
Date: Tue, 4 Sep 2012 22:30:09 -0400
Local: Tues, Sep 4 2012 10:30 pm
Subject: Re: [mongodb-csharp] Query Bson Documents directly using Linq

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SID  
View profile  
 More options Sep 19 2012, 2:04 pm
From: SID <sidharth.sa...@gmail.com>
Date: Wed, 19 Sep 2012 11:04:12 -0700 (PDT)
Local: Wed, Sep 19 2012 2:04 pm
Subject: Re: [mongodb-csharp] Query Bson Documents directly using Linq

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
craiggwilson  
View profile  
 More options Sep 19 2012, 3:22 pm
From: craiggwilson <craiggwil...@gmail.com>
Date: Wed, 19 Sep 2012 12:22:38 -0700 (PDT)
Local: Wed, Sep 19 2012 3:22 pm
Subject: Re: [mongodb-csharp] Query Bson Documents directly using Linq

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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SID  
View profile  
 More options Sep 20 2012, 3:53 pm
From: SID <sidharth.sa...@gmail.com>
Date: Thu, 20 Sep 2012 12:53:17 -0700 (PDT)
Local: Thurs, Sep 20 2012 3:53 pm
Subject: Re: [mongodb-csharp] Query Bson Documents directly using Linq

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, IMongoQueryquery)

        {

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

return cursor;

}

Now I can itierate the cursor

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SID  
View profile  
 More options Sep 21 2012, 5:42 pm
From: SID <sidharth.sa...@gmail.com>
Date: Fri, 21 Sep 2012 14:42:49 -0700 (PDT)
Local: Fri, Sep 21 2012 5:42 pm
Subject: Re: [mongodb-csharp] Query Bson Documents directly using Linq

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »