The product of a web developer with a little too much caffeine

jLinq in MongoDB (Oh snap!!)

with 9 comments

My new project CSMongo, a Mongo driver for .NET, is now online! Check it out!

Yeah, you read that right – jLinq in MongoDB!

Lately I’ve been working on a Mongo database driver and I got to thinking — Since I can use Javascript with MongoDB then I wonder if I could use jLinq to write queries? As it turns out the answer is yes!

… and if you don’t know what jLinq is… – jLinq is a javascript query language, similar to the LINQ project created by Microsoft, for JSON data which was designed to run within web pages using Javascript

Getting Started

I’m not really sure how you can get a Javascript library to load directly into MongoDB but for now I was simply copying and pasting the existing jLinq packed library into the command line and allowing it to evaluate the script — and that is it! You can use jLinq right away!

You should be aware though that you can’t just evaluate results directly. You actually need to call the toArray() function on each of them to see the results. So for example, your query would look something like…

jLinq.from(db.users.find().toArray()).startsWith("name", "h").select();

You don’t need to actually do anything with the results since they are automatically displayed on the screen for you.

So far everything works. I could do standard queries, use the OR operator, reorder records, join different databases, perform comparisons against sub-properties — really everything I’ve tried has worked just the way I’d expect it to!

Granted, database and collection names are going to be different, here are some interesting queries you can try with your database.

//Selects records where the name starts with a, b or c (case-insensitive)
    .startsWith("name", "a")

//selects users older than 50 and are administrators then orders them by their names
    .greater("age", 50)

//performs a join against another database to get locations
//and then queries the location to find users that live in 'texas'
//and then selects only the name of the person and location
    .join(db.locations.find().toArray(), "location", "locationId", "id")
    .equals("", "texas")
    .select(function(rec) {
        return {

More On jLinq

Unfortunately, it isn’t possible to go over all of the cool stuff you can do with jLinq – but here are some screencasts and blog posts that you can read that might help you get started.

jLinq Project Page
Screencast 1 – Getting Started
Screencast 2 – Extending jLinq (Custom commands)
Screencast 3 – Modifying Live Data In A Query (Joins, Assignment)
Screencast 4 – jLinq 2.2.1 (Updates)

Anyways, for now I’m not sure how to integrate jLinq into Mongo queries but keep an eye for future blog posts as I find out more.

Written by hugoware

February 14, 2010 at 11:45 pm

9 Responses

Subscribe to comments with RSS.

  1. Wow, jLinq is neat!


    February 15, 2010 at 12:34 am

  2. Dude, you da man! One thought – what would a server side solution look like?

    ActiveEngine Sensei

    February 15, 2010 at 7:25 am

    • That is a good question – I’m still learning a lot about how MongoDB works – I’ve seen that you can execute Javascript as part of a query (I think I’ve seen that) as well as send ‘commands’ to the server – But if that means you can import a library then I’m not sure (or at least without doing an eval each request)


      February 15, 2010 at 9:44 am

  3. Don’t know if you have seen this, but there IronJS for the DLR!!!

    Maybe this helps with quest for the driver.

    ActiveEngine Sensei

    February 18, 2010 at 11:07 pm

    • Yeah, I *looked* at it… and when I couldn’t find File -> New Project -> IronJS I gave up 🙂

      I think a lot of these new languages are awesome but I don’t normally have the interest in getting them up and running. I’m one of those “just make it work for me” people… I suppose I’m just a lazy code– oops… Lazy developer…

      Is there a way to actually use IronJS in a real project? Or is VS integration still a long ways off?


      February 19, 2010 at 10:18 am

      • Of course, the Interactive mode works fine — I meant that I wanted to figure out how to write an MVC application using IronJS for example.


        February 19, 2010 at 10:20 am

  4. WTF?

    Imagine a collection with millions of documents.
    In the moment you do “.toArray()” you bring all those documents into memory and then jLinq does a “query” on this data (same as Linq to Objects).

    So, do you have enough RAM? =)

    This would only be usable if the jLinq could be used in a Map/Reduce schema (without making its iterations on the RESULT of a “select”)


    December 16, 2010 at 5:29 pm

    • Right – this was just to show that jLinq could be used as a query language for Mongo. Ideally the .from call would access the database directly and perform the query that way.


      December 16, 2010 at 8:47 pm

Leave a Reply to ActiveEngine Sensei Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: