Hugoware

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

Posts Tagged ‘LINQ

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)
jLinq.from(db.users.find().toArray())
    .startsWith("name", "a")
    .or("b")
    .or("c")
    .select();

//selects users older than 50 and are administrators then orders them by their names
jLinq.from(db.users.find().toArray())
    .greater("age", 50)
    .is("admin")
    .orderBy("name")
    .select();

//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
jLinq.from(db.users.find().toArray())
    .join(db.locations.find().toArray(), "location", "locationId", "id")
    .equals("location.name", "texas")
    .select(function(rec) {
        return {
            person: rec.name,
            location: rec.location.name
        };
    });

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.

Advertisements

Written by hugoware

February 14, 2010 at 11:45 pm

jLinq Update (2.2.1)

with 10 comments

I’m off from work this week so the first thing I did was get some work done on jLinq. I’ve had a few bugs I’ve needed to sort out along with a new feature I wanted to implement. I’m also going to be working on documentation this week. Previously, I had created a Wiki and was hoping that people might add to the documentation… but instead spam bots pretty much ruined it so I’ll be starting over from scratch… oh well..

New Features

jLinq itself doesn’t really have a new feature but instead a new way to get the jLinq library. Instead of simply downloading a standard jLinq library you can use the new online jLinq Framework Generator to select only the functionality that you want to include. You can still download the basic pack but this option gives you a little more control what goes into your version of jLinq.

Changes

The most notable change for jLinq is that all of the operator commands have been included as standard functions for the framework. Before, these functions were actually methods extended onto the framework. Since these functions are required for jLinq to work I’ve moved them so that they are in every jLinq framework by default.

Bug Fixes

orderBy with joined records would return incorrect results
jLinq uses eval to figure out the values of field names since you can provide methods, array indexes or whatever. The code used to sort the values wasn’t getting the values correctly so the sorted result was always wrong.

using 0 in certain comparisons would return incorrect results
Ah, this one was fun. So check out the code below…

var values = [ 0 ];
var index = 0;
var a = !values[index];
var b = values[index] == null;

So what is a and what is b? If you said true and false then give yourself a cookie.

jLinq uses the number of arguments passed to help determine if anything is being memorized (like the field name). jLinq makes a quick pass to select all values until it finds a null value… or at least that is what I meant for it to do. Unfortunately, I wasn’t thinking when I wrote that method and didn’t check explicitly for null.

Feedback

If you find any problems with jLinq or have any suggestions, please leave a comment or contact me directly.

Written by hugoware

November 23, 2009 at 9:03 pm

Double Whammy – jLinq Release And Screencast!

with 2 comments

Now that you’re all excited time for some disappointment – it is just a minor update to fix a few bugs and to add one new feature.

But instead of just blogging about it I’ve also done a new screencast to talk about the updates and new feature in this release.

  • Dynamic Queries (using OR) – Before, if you used “OR” at the start of a query then you’d get an exception. jLinq would try and tag “||” at the start of your query which would cause the whole thing to collapse. Now, jLinq correctly handles this and prevents the error. In turn, writing dynamic queries just got a whole lot easier!.
  • Range Comparisons With Dates – Version 2.2.0 introduced smarter comparisons by evaluating the types of values being passed in instead of expecting only one type. However, in the change, the range comparisons like greater, less, between, etc, would all incorrectly evaluate Dates as strings… bummer.
  • Bitwise Flag Comparisons – jLinq 2.2.1 introduces a new query method called has which performs bitwise flag comparisons on records. It even works with regular ol’ numbers by converting them before performing the comparisons.

Anyways, go check out he new screen cast now and tell me what you think!


screencast-4-play

Bonus: CamStudio Icon

camStudioExample

Last night while I was setting up CamStudio for this screencast I was looking at their UGLY icon they use for their program — I just couldn’t stand it being in my beautiful RocketDock – so I designed a new one for them. If you’re looking for an improved logo then you can download the PNG for the CamStudio icon.

Written by hugoware

September 9, 2009 at 2:18 am

jLinq Screencast #3 – Modifying Records During A Query

with 3 comments

Sometimes when you get your records they aren’t exactly what you we’re needing, or sometimes some of the fields need to be formatted before they can be queried against. Fortunately, jLinq comes with that functionality built into it.

This screencast goes over several of the methods you have available to you that allow records to be formatted before they are selected.

  • each(delegate(record)): Performs a loop through each of the values in the array. This code can be used to modify records, create new properties or anything you need it to do.
  • attach(alias, delegate(record)): Performs the method passed in on each of the records in the array and attaches the result to the record using the alias provided.
  • join(records, alias, foreignKey, primaryKey): Joins two arrays together using the alias and the keys to match the values. Joins tend to be slow when using large arrays, so use caution.


screencast3

If you have any recommendations or requests for the next screencast, please let me know and I’ll see what I can do.

Side note: Never improvise on a screen cast – nothing like having to redo a screen cast twice because you say something that isn’t true and then realize it while recording. 🙂

Written by hugoware

August 24, 2009 at 2:21 am

Saving Your Sanity – Handling XML Namespaces Using LINQ

with one comment

Have you ever worked with XML documents that have Namespaces with them? It’s that extra bit of markup in XML documents that is supposed to prevent any of your elements from conflicting with each other. For me, they tend to get in they way more than they help — Let’s take some example XML.

<?xml version="1.0"?>
<data:Inventory xmlns:data="http://www.bookstore.com/" >
    <data:Books xmlns:inv="http://www.bookstore.com/book-inventory">
        <inv:Book count="231" >C# For Dummies</inv:Book>
        <inv:Book count="35" >Using jLinq</inv:Book>
        <inv:Book count="54" >Ruby On Raylze</inv:Book>
    </data:Books>
</data:Inventory>

Typically when using XML I’ve used XPaths to find the information I need. I’m certainly not an expert but I know enough to get the job done. Normally, with plain vanilla XML, you could write a quick XPath to find your books and parse over them. There is a handful of ways you could do it but for this example let’s be a little lazy…

XDocument inventory = XDocument.Load("d:\\temp\\sample.xml");
IEnumerable<XElement> books = inventory.XPathSelectElements("//Book");

//How many books do you think we have?
Console.WriteLine(books.Count());

If you run this code you’re going to end up with a big, fat, ugly zero. You can even try a variety of other XPaths, but none of which will find you the data you want.

  • //inv:Bookthrows an error
  • Inventory/Books/Bookreturns 0
  • Inventory/Books/*returns 0
  • //Books/*returns 0

Nothing — Ouch…

LINQ To The Rescue

If you’ve been using LINQ then you know that you can’t help but use it when you realize just how powerful it is. As much as I used it though, it never occurred to me that LINQ could be used in place of an XPath. Using the same XML as before and applying a tiny bit of LINQ magic, we come up with the following solution.

var results =
    inventory.Descendants()
        .Where(o => o.Name.LocalName.Equals("Book"))
        .Select(o => new {
            Name = o.Value,
            Count = int.Parse(o.Attribute("count").Value)
        });

I didn’t do any performance tests but I doubt that something like this would incur any sort of noticeable hit. Even if it is a bit slower, I think it is worth it. With a single LINQ query I’m not only able to avoid dealing with Namespaces all together, but I’m able to parse and return my data into an anonymous type. All my information is ready to go just like that!

This certainly isn’t a monumental discovery — In fact I’m sure that I’m arriving to this late in the game, but for today my life got a lot better now that it became much less painful to work with SOAP responses!

Written by hugoware

August 5, 2009 at 1:07 am

Posted in General Discussions

Tagged with , , , , ,

jLinq – Going Online!

leave a comment »

This blog is focused on being about general development along with a lot of discussion about my projects I’m currently working on. I’ve cleared out all of my other blogs and created this one so it can be a broader discussion about development than the others could be.

jLinq is the main project I’ll be posting about for now. jLinq is similar to LINQ for .NET, but it is used in Javascript. It’s especially useful for working with medium sized arrays of information that won’t be changing.

Most of the time people use AJAX to make calls to their server and download the information they need. jLinq allows you use many of the same functions you use in LINQ, but against existing data on the page. 

One of the most important parts of jLinq was that I wanted it to be easy to extend. In fact, at one point the whole jLinq library was wiped and started over and built around an extensibility framework! I wanted the extensibility to work so well that even the core functions of jLinq was built around it!

I’ll blog more about using jLinq, but for now the best thing you can do is try it out on my website! Please leave me feedback about what you think!

Documentation is still in progress, but there are a lot of handy tutorials that will get you started!

Written by hugoware

April 26, 2009 at 5:46 am