Skip to content

Instantly share code, notes, and snippets.

@mingderwang
Created April 29, 2012 05:33
Show Gist options
  • Save mingderwang/2533726 to your computer and use it in GitHub Desktop.
Save mingderwang/2533726 to your computer and use it in GitHub Desktop.
my first trial for mongodb
ming@ming-laptop:~/mongodb-linux-x86_64-2.0.2/bin$ ./mongo
MongoDB shell version: 2.0.2
connecting to: test
Mon Jan 9 13:17:10 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
ming@ming-laptop:~/mongodb-linux-x86_64-2.0.2/bin$ ./mongo
MongoDB shell version: 2.0.2
connecting to: test
> db
test
> db blog
Mon Jan 9 13:17:56 SyntaxError: missing ; before statement (shell):1
> post = { "title", "my title",
... datae , new Date() }
Mon Jan 9 13:19:54 SyntaxError: missing : after property id (shell):1
> post = { "title", "my title",
... date, new Date() }
Mon Jan 9 13:20:08 SyntaxError: missing : after property id (shell):1
> post = { "title", "my title",
... "date"
ming@ming-laptop:~/mongodb-linux-x86_64-2.0.2/bin$ ./mongo
MongoDB shell version: 2.0.2
connecting to: test
> post = { "title" : "my blog",
... "date" : new Date() }
{ "title" : "my blog", "date" : ISODate("2012-01-09T05:20:52.046Z") }
> insert(post)
Mon Jan 9 13:21:06 ReferenceError: insert is not defined (shell):1
> test.insert(post)
Mon Jan 9 13:21:13 ReferenceError: test is not defined (shell):1
> db!!
...
ming@ming-laptop:~/mongodb-linux-x86_64-2.0.2/bin$ !!
./mongo
MongoDB shell version: 2.0.2
connecting to: test
> post
Mon Jan 9 13:21:23 ReferenceError: post is not defined (shell):1
> post = { "title": "my blog",
... "date" : new Date() }
{ "title" : "my blog", "date" : ISODate("2012-01-09T05:21:47.164Z") }
> db.test.insert(post)
> db.test.findAll()
Mon Jan 9 13:22:14 TypeError: db.test.findAll is not a function (shell):1
> db.test.find()
{ "_id" : ObjectId("4f0a7972d16c38386be67077"), "title" : "my blog", "date" : ISODate("2012-01-09T05:21:47.164Z") }
> db.test.findOne()
{
"_id" : ObjectId("4f0a7972d16c38386be67077"),
"title" : "my blog",
"date" : ISODate("2012-01-09T05:21:47.164Z")
}
> db.test.update
function (query, obj, upsert, multi) {
    assert(query, "need a query");
    assert(obj, "need an object");
    var firstKey = null;
    for (var k in obj) {
        firstKey = k;
        break;
    }
    if (firstKey != null && firstKey[0] == "$") {
        this._validateObject(obj);
    } else {
        this._validateForStorage(obj);
    }
    this._mongo.update(this._fullName, query, obj, upsert ? true : false, multi ? true : false);
}
> db.help()
DB methods:
db.addUser(username, password[, readOnly=false])
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, { size : ..., capped : ..., max : ... } )
db.currentOp() displays the current operation in the db
db.dropDatabase()
db.eval(func, args) run code server-side
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus()
db.removeUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }
db.serverStatus()
db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
db.shutdownServer()
db.stats()
db.version() current version of the server
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnock() unlocks server following a db.fsyncLock()
> db.help
function () {
    print("DB methods:");
    print("\tdb.addUser(username, password[, readOnly=false])");
    print("\tdb.auth(username, password)");
    print("\tdb.cloneDatabase(fromhost)");
    print("\tdb.commandHelp(name) returns the help for the command");
    print("\tdb.copyDatabase(fromdb, todb, fromhost)");
    print("\tdb.createCollection(name, { size : ..., capped : ..., max : ... } )");
    print("\tdb.currentOp() displays the current operation in the db");
    print("\tdb.dropDatabase()");
    print("\tdb.eval(func, args) run code server-side");
    print("\tdb.getCollection(cname) same as db['cname'] or db.cname");
    print("\tdb.getCollectionNames()");
    print("\tdb.getLastError() - just returns the err msg string");
    print("\tdb.getLastErrorObj() - return full status object");
    print("\tdb.getMongo() get the server connection object");
    print("\tdb.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair");
    print("\tdb.getName()");
    print("\tdb.getPrevError()");
    print("\tdb.getProfilingLevel() - deprecated");
    print("\tdb.getProfilingStatus() - returns if profiling is on and slow threshold ");
    print("\tdb.getReplicationInfo()");
    print("\tdb.getSiblingDB(name) get the db at the same server as this one");
    print("\tdb.isMaster() check replica primary status");
    print("\tdb.killOp(opid) kills the current operation in the db");
    print("\tdb.listCommands() lists all the db commands");
    print("\tdb.logout()");
    print("\tdb.printCollectionStats()");
    print("\tdb.printReplicationInfo()");
    print("\tdb.printSlaveReplicationInfo()");
    print("\tdb.printShardingStatus()");
    print("\tdb.removeUser(username)");
    print("\tdb.repairDatabase()");
    print("\tdb.resetError()");
    print("\tdb.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }");
    print("\tdb.serverStatus()");
    print("\tdb.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all");
    print("\tdb.shutdownServer()");
    print("\tdb.stats()");
    print("\tdb.version() current version of the server");
    print("\tdb.getMongo().setSlaveOk() allow queries on a replication slave server");
    print("\tdb.fsyncLock() flush data to disk and lock server for backups");
    print("\tdb.fsyncUnock() unlocks server following a db.fsyncLock()");
    return __magicNoPrint;
}
> db.version
function () {
    return this.serverBuildInfo().version;
}
> db.getCollection("version"); test.version
Mon Jan 9 13:23:32 ReferenceError: test is not defined (shell):1
> doc = db.test.findOne()
{
"_id" : ObjectId("4f0a7972d16c38386be67077"),
"title" : "my blog",
"date" : ISODate("2012-01-09T05:21:47.164Z")
}
> doc.title
my blog
>
>
> db.test.remove({title : my})
Mon Jan 9 13:28:43 ReferenceError: my is not defined (shell):1
> db.test.remove({title : "my blog"})
> db.test.find()
> db.test.findOne()
null
> show collections
system.indexes
test
> show users
> show profile
db.system.profile is empty
Use db.setProfilingLevel(2) will enable profiling
Use db.system.profile.find() to show raw profile entries
> use system.indexes
switched to db system.indexes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment