Giter Club home page Giter Club logo

mongoose-simple-random's Introduction

mongoose-simple-random

Author: Larry Price
Website: larry-price.com
Email: [email protected]

Description

Simple and easy-to-use NodeJS Mongoose Schema plugin to find random documents.

Usage

var random = require('mongoose-simple-random');

var s = new Schema({
  message: String
});
s.plugin(random);

Test = mongoose.model('Test', s);

// Find a single random document
Test.findOneRandom(function(err, result) {
  if (!err) {
    console.log(result); // 1 element
  }
});

// Find "limit" random documents (defaults to array of 1)
Test.findRandom({}, {}, {limit: 5}, function(err, results) {
  if (!err) {
    console.log(results); // 5 elements
  }
});

// Parameters match parameters for "find"
var filter = { genre: { $in: ['adventure', 'point-and-click'] } };
var fields = { name: 1, description: 0 };
var options = { skip: 10, limit: 10, populate: 'mySubDoc' };
Test.findRandom(filter, fields, options, function(err, results) {
  if (!err) {
    console.log(results); // 10 elements, name only, in genres "adventure" and "point-and-click"
  }
});

Tests

$ npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style.

Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 0.1.0 Initial release
  • 0.2.0 API change - findRandom always returns array, findOneRandom returns single item
  • 0.2.1 README update
  • 0.3.0 API change - flip-flopping on "count", use "limit" to tell findByRandom how many elements to return
  • 0.4.0 Update dependencies and use a new random algorithm
  • 0.4.1 Fix hang on 0 results and clean up callback syntax.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.