Socket
Book a DemoInstallSign in
Socket

mongoid-sample

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoid-sample

0.1.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

Mongoid::Sample

Brings the API of Ruby's Array#sample to Mongoid collections

Installation

Add this line to your application's Gemfile:

gem 'mongoid-sample'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid-sample

And require:

require 'mongoid/sample'

Usage

Sample a single document

require 'mongoid/sample'

class MyModel
  include Mongoid::Document
  field :name
end

puts MyModel.sample.inspect # => nil
5.times { MyModel.create(name: 'Foo') }
puts MyModel.sample.inspect # => #<MyModel _id: 57954c1002182308d7000000, name: "Foo">

Sample multiple documents

require 'mongoid/sample'

class MyModel
  include Mongoid::Document
  field :name
end

puts MyModel.sample(3).inspect # => []
5.times { MyModel.create(name: 'Foo') }
puts MyModel.sample(3).inspect # => [#<MyModel _id: 57954c6f02182308d7000008, name: "Foo">, #<MyModel _id: 57954c6f02182308d700000a, name: "Foo">, #<MyModel _id: 57954c6f02182308d7000009, name: "Foo">]

Sample from scope

require 'mongoid/sample'

class MyModel
  include Mongoid::Document
  field :name
end

puts MyModel.where(name: 'Foo').sample(3).inspect # => []
5.times { MyModel.create(name: 'Foo') }
5.times { MyModel.create(name: 'Bar') }
puts MyModel.sample(3).inspect # => [#<MyModel _id: 57954c6f02182308d7000007, name: "Foo">, #<MyModel _id: 57954c6f02182308d7000008, name: "Foo">, #<MyModel _id: 57954ca702182308d700000e, name: "Bar">]
puts MyModel.where(name: 'Foo').sample(3).inspect # => [#<MyModel _id: 57954c6f02182308d7000008, name: "Foo">, #<MyModel _id: 57954c6f02182308d7000009, name: "Foo">, #<MyModel _id: 57954c6f02182308d7000006, name: "Foo">]

FAQs

Package last updated on 04 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.