Skip to content

Instantly share code, notes, and snippets.

@ngsw
Created January 14, 2013 16:15
Show Gist options
  • Select an option

  • Save ngsw/4531169 to your computer and use it in GitHub Desktop.

Select an option

Save ngsw/4531169 to your computer and use it in GitHub Desktop.
# #
# http://dotinstall.com/lessons/basic_mongodb #
# #
use dtbs
db.dropDatabase()
db.createCollection("posts")
db.createCollection("users")
db.users.renameCollection("entries")
show collections
db.collection.find()
### memo
db.${collection}.insert({ ${key00} : ${value00} , ${key01} : ${value01} , … , ${keyNN} : ${valueNN} })
### memo
for ( var i = 0 ; i < 10 ; i++ ) {
db.users.insert(
{"name" :"user-"+i,
"team" : i % 3,
"score" : Math.floor( Math.random() * 100)
}
);
}
db.users.find()
db.users.find({"team" : 0})
db.users.find({"team" : 0 }, {"name" :true})
db.users.find({"team" : {$ne:0} })
### memo
db.${collection}.find({
"${key}" : { ${condition} }
})
### memo
db.users.find({
"team" : { $ne : 0 }
})
db.users.find({ "score" : { $gt : 60 , $lte : 75 } })
db.users.find({ "score" : { $gt : 60 , $lte : 75 } , "name" : {$regex:/user-[0-3]/i} })
db.users.find({ "score" : { $gt : 60 , $lte : 75 } , "name" : {$regex:/user-[0-5]/i} })
### memo
${condition} is "{ ${key} : ${exp} }" .
${condition} is "{ ${key00} : ${exp00} , ${key01} : ${exp01} }" too.
${exp) is
$ne
$gt
$gte
$lt
$lte
$regex:/${pattern}/i
..etc
### memo
db.users.find({ "name" : {$regex:/user-[0-3]/i} }).sort({"score" : 1 })
db.users.findOne()
db.users.find({"team" : 2 }).limit(3)
db.users.find().skip(2).limit(3)
db.users.find().count()
db.users.distinct("team");
@ngsw
Copy link
Copy Markdown
Author

ngsw commented Jan 14, 2013

too ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment