Sha256: 28550dc3e512ce323cf0b7bc64b47a1a2f4c54e29b16f502b20097fbf4e89928

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

function multiquery(queries) {
  var results = {}, collectionObject;

  // extract all the collection names,
  // so we can return an error if one is provided that doesn't exist
  // since db[invalidName] returns a collection object
  // we could perhaps make this more optimized by building a hash,
  // but it shouldn't be a problem in most cases
  var collectionNames = db["system.namespaces"].find().map(function(ns) { return ns.name }),
      dbName = db.toString(),
      query, base, modifiers, collection;

  for (var queryName in queries) {
    try {
      query = queries[queryName];
      modifiers = query.modifiers || [];
      collection = query.collection;

      if (collectionNames.indexOf(dbName + "." + collection) !== -1) {
        base = db[collection].find(query.selector, query.fields || null)
        // apply any number of modifiers, such as sort, limit, etc.
        for (var modifier in modifiers) {
          base = base[modifier](modifiers[modifier])
        }
        results[queryName] = base.toArray();
      }
      else {
        // if the collection doesn't exist, return an error
        // (rather than null -- the DB allows queries on non-existent collections)
        // doing this here saves us a database call in Ruby
        results[queryName] = {error: "Unable to locate collection " + (collection ? collection.toString() : collection)}
      }
    }
    catch(e) {
      results[queryName] = {error: e};
    }
  }

  return results;
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoscript-0.0.10 lib/mongoscript/javascripts/multiquery.js
mongoscript-0.0.9 lib/mongoscript/javascripts/multiquery.js
mongoscript-0.0.8 lib/mongoscript/javascripts/multiquery.js