Skip to content

Instantly share code, notes, and snippets.

@jeffdonthemic
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffdonthemic/6ec3fb2c0921223d91db to your computer and use it in GitHub Desktop.
Save jeffdonthemic/6ec3fb2c0921223d91db to your computer and use it in GitHub Desktop.
Mongodb Notes
## Tutorials
[Node.js Development with the MongoDB Service](http://start.cloudfoundry.com/services/mongodb/nodejs-mongodb.html)
[MongoDB in 5 minutes](http://mongodbtutorial.com/mongodb-in-5-minutes.html)
[Best Doc on Commands](http://www.mongodb.org/display/DOCS/Tutorial)
## Mongo CLI
to start local mongodb from the terminal:
> mongod
to start remote mongodb from the terminal:
> mongo paulo.mongohq.com:10067/app18484458 -u thurgood -p thurgood
On my mac, to connect to mongodb from terminal do:
> /usr/local/Cellar/mongodb/2.0.4-x86_64/bin/mongo
Export records from the db:
mongoexport -h ds033111-a0.mongolab.com:33111 -d heroku_app30161628 -u <user> -p <password> -c users --fields handle,complete,hints,totalTime --out allusers.csv --csv
see: http://docs.mongodb.org/v2.2/reference/mongoexport/
Samples commands:
> mongo
> show dbs
> use local
> use test
switched to db test
> show collectionsfind
> i = { name : "jeff test 1" }
{ "name" : "jeff test 1" }
> db.test.save(i)
> db.test.find()
{ "_id" : ObjectId("4f6dc73f58a40304c66f3d03"), "name" : "jeff test 1" }
> db.test.find({name:"jeff test 1"})
{ "_id" : ObjectId("4f6dc73f58a40304c66f3d03"), "name" : "jeff test 1" }
> db.test.update({name:"jeff test 1"}, {$set: {topic:"my value"}});
> db.test.find();
{ "_id" : ObjectId("4f6dc73f58a40304c66f3d03"), "name" : "jeff test 1", "topic" : "my value" }
db.loggerAccounts.find({}, {name: true, _id: false}).limit(10)
db.registrationcodes.remove({"_id": ObjectId("53adad475dfcf300004a18e8")})
# find and update a document by id
> db.facilities.findAndModify({query: {'_id':ObjectId('4f6f146ac82dd05624000002')}, update: { $set: {'ddress':'400 Main St.'}}})
db.users.findAndModify({query: {'_id':ObjectId('535a31a86bfbfb6833bf61db')}, update: { $set: {'contactId':'003G000001h1bjC'}}})
## Sample JS Code
// find one record with the hightest value
User.find({}).sort({goldenTicket: 'descending'}).limit(1).exec(function(err, items) {
if (!items[0].goldenTicket) {
deferred.resolve(1000);
} else {
deferred.resolve(items[0].goldenTicket + 1);
}
});
// choose a random problem and return it
Problem.count(function(err, ct) {
var r = Math.floor(Math.random() * ct);
Problem.find({ event: 'tco14' }).limit(1).skip(r).exec(function(err, problem) {
console.log(problem);
});
});
// find users where totalTime > 0 and sort by totalTime and return handle, totalTime and _id fields
User.find({totalTime: { $gt: 0 }}, { handle: 1, totalTime: 1} ).sort({ 'totalTime': 1 }).exec(function(err, allUsers) {
res.json({users: allUsers});
});
// update a user by handle. update values in an array
User.update({ handle: req.user.handle, 'steps.number': req.user.steps.length}, {'$set': {
'steps.$.complete': true,
'steps.$.userAnswer': req.body.answer
}}, function(err) {
if (err) console.log(err);
});
// update a user by handle
User.update({ handle: req.user.handle}, {'$set': {
'complete': true,
'endDatetime': new Date(),
'totalTime': (end.diff(start)/1000)/60
}}, function(err) {
if (err) console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment