Skip to content

Instantly share code, notes, and snippets.

@yuki24
Created July 24, 2012 10:54
Show Gist options
  • Save yuki24/3169376 to your computer and use it in GitHub Desktop.
Save yuki24/3169376 to your computer and use it in GitHub Desktop.
moped examples
session = Moped::Session.new(["127.0.0.1:27017"], database: "mongodb")
session[:collection].find.one
# => {
# "_id"=>"500b7c0d1d41c81647000016",
# "name"=>"a photo",
# "updated_at"=>2012-07-22 04:05:33 UTC,
# "created_at"=>2012-07-22 04:05:33 UTC
# }
session[:collection].find.select(name: false).one
# => {
# "_id"=>"500b7c0d1d41c81647000016",
# "updated_at"=>2012-07-22 04:05:33 UTC,
# "created_at"=>2012-07-22 04:05:33 UTC
# }
session[:collection].find.limit(2).select(name: 0).to_a
# =>
# [
# {
# "_id"=>"500b7c0d1d41c81647000016",
# "updated_at"=>2012-07-22 04:05:33 UTC,
# "created_at"=>2012-07-22 04:05:33 UTC
# },
# {
# "_id"=>"500b7c9c1d41c8164700002a",
# "updated_at"=>2012-07-22 04:07:56 UTC,
# "created_at"=>2012-07-22 04:07:56 UTC
# }
# ]
session[:collection].find.limit(2).select(name: 1).to_a
# =>
# [
# {
# "_id"=>"500b7c0d1d41c81647000016",
# "name"=>"a photo"
# },
# {
# "_id"=>"500b7c1b1d41c8164700001a",
# "name"=>"aaaaaaa"
# }
# ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment