原文出自:http://www.mkyong.com/mongodb/mongodb-hello-world-example/
返回目录:http://ysj5125094.iteye.com/blog/2192754
MongoDB Hello World Example
A quick guide to show you how to do basic operations like create, update, find, delete record and indexing in MongoDB. This example is using MongoDB 2.0.7, running on Mac OS X 10.8, both MongoDB client and server console are run on localhost, same machine.
译:一个快速指南,告诉你如何在MongoDB中做基本的操作如创建,更新,删除记录和索引。这个例子是使用MongoDB 2.0. 7,在MAC OS X 10.8的运行,无论是MongoDB的客户端和服务器控制台上运行的本地主机,同一台机器。
1. Install MongoDB
Install MongoDB on Windows(译), Ubuntu(待译) or Mac OS X(待译). The installation is easy, basically just download the MongoDB zip file, extra and run the command – $MongoDB-folder/bin/mongod
.
译:在Windows,Ubuntu或MAC OS X 上安装MongoDB非常容易,基本上只是下载MongoDB ZIP文件,额外的运行命令 – $MongoDB-folder / bin / mongod。
Uses mongod
to start MongoDB.
译:使用 mongod 命令启用MongoDB。
$./mongod Tue Sep 11 21:55:36 [initandlisten] MongoDB starting : pid=72280 port=27017 dbpath=/data/db/ 64-bit host=Yongs-MacBook-Air.local Tue Sep 11 21:55:36 [initandlisten] db version v2.0.7, pdfile version 4.5 Tue Sep 11 21:55:36 [initandlisten] options: {} Tue Sep 11 21:55:36 [initandlisten] journal dir=/data/db/journal Tue Sep 11 21:55:36 [initandlisten] recover : no journal files present, no recovery needed Tue Sep 11 21:55:36 [websvr] admin web console waiting for connections on port 28017 Tue Sep 11 21:55:36 [initandlisten] waiting for connections on port 27017
2. Connect MongoDB
To connect MongoDB, uses $MongoDB-folder/bin/mongo
译:使用 $MongoDB-folder / bin / mongo 命令来连接MongoDB。
$ ./mongo MongoDB shell version: 2.0.7 connecting to: test
3. Create a database or table (collection)
译:创建一个数据库或表(集合),在MongoDB中表被称做集合。
In MongoDB, both database and table are created automatically when the first time data is inserted. Uses use database-name
, to switch to your database (even this is not created yet).
译:在MongoDB中,第一次插入数据时数据库和表自动创建。使用 use database - name 命令,切换到您的数据库(即使这个仍未被创建)。
In below example, after you inserted a single record, database “mkyong”, and table “users” are created on the fly.
译:在下面的例子中,当你插入一个记录后,数据库 "mkyong" 和表 "users" 是动态创建的。
$ ./mongo MongoDB shell version: 2.0.7 connecting to: test > use mkyong switched to db mkyong > db.users.insert({username:"mkyong",password:"123456"}) > db.users.find() { "_id" : ObjectId("504f45cd17f6c778042c3c07"), "username" : "mkyong", "password" : "123456" }
Three database commands you should know.
译:以下三个关于数据库的命令,你应该知道。
-
show dbs
– List all databases.译:显示所有数据库列表 -
use db_name
– Switches to db_name.译:打开db_name数据库。 -
show collections
– List all tables in the current selected database.译:当前选定的数据库中所有表的集合(列表)。
Note
In MongoDB, collection means table in SQL.
译:在MongoDB中,collection 相当于SQL中的 table。
4. Insert A Record
To insert a record, uses db.tablename.insert({data})
or db.tablename.save({data})
, both works, no idea why MongoDB created both.
译:使用 db.tablename.insert({data}) 或 db.tablename.save({data})都可以在MongoDB中插入一条新的记录。
> db.users.save({username:"google",password:"google123"}) > db.users.find() { "_id" : ObjectId("504f45cd17f6c778042c3c07"), "username" : "mkyong", "password" : "123456" } { "_id" : ObjectId("504f48ea17f6c778042c3c0a"), "username" : "google", "password" : "google123" }
5. Update A Record
To update a record, uses db.tablename.update({criteria},{$set: {new value}})
. In below example, the password of username : “mkyong” is updated.
译:使用 db.tablename.update({criteria},{$set:{new value}})命令更新一条记录。下面的例子中,将用户名为“mkyong”的密码更新。
> db.users.update({username:"mkyong"},{$set:{password:"hello123"}}) > db.users.find() { "_id" : ObjectId("504f48ea17f6c778042c3c0a"), "username" : "google", "password" : "google123" } { "_id" : ObjectId("504f45cd17f6c778042c3c07"), "password" : "hello123", "username" : "mkyong" }
6. Find Records
To find or query records, uses db.tablename.find({criteria})
.
译:使用 db.tablename.find({criteria}) 命令寻找或查询记录。
6.1 List all records from table “users”.译:从表"users"中获取用户列表。
> db.users.find() { "_id" : ObjectId("504f48ea17f6c778042c3c0a"), "username" : "google", "password" : "google123" } { "_id" : ObjectId("504f45cd17f6c778042c3c07"), "password" : "hello123", "username" : "mkyong" }
6.2 Find records where username is “google”.译:查询username是"google"的记录。
> db.users.find({username:"google"}) { "_id" : ObjectId("504f48ea17f6c778042c3c0a"), "username" : "google", "password" : "google123" }
6.3 Find records where username’s length is less than or equal to 2.译: 查询username长度小于或等于2的记录。
db.users.find({$where:"this.username.length<=2"})
6.4 Find records where username field is existed. 译:查询不存在username字段的记录。
db.users.find({username:{$exists : true}})
7. Delete Record
To delete a record, uses db.tablename.remove({criteria})
. In below example, the record of username “google” is deleted.
译:使用 db.tablename.remove({criteria}) 命令删除记录。在下面例子中告诉你如何删除username是"google"的记录。
> db.users.remove({username:"google"}) > db.users.find() { "_id" : ObjectId("504f45cd17f6c778042c3c07"), "password" : "hello123", "username" : "mkyong" }
Note
To delete all records from a table, uses db.tablename.remove()
.
To drop the table, uses db.tablename.drop()
.
译1:使用 db.tablename.remove() 命令删除表中所有记录。
译2:使用 db.tablename.drop() 命令删除表。
8. Indexing
Index may help you increase the speed of querying data.
译:索引可以帮助你提高数据查询速度。
8.1 List all indexes of table “users”, by default the column “_id” is always the primary key and created automatically.
译:"users"表的所有索引列表,默认情况下 "_id" 字段是自动创建主键的。
> db.users.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "mkyong.users", "name" : "_id_" } ] >
8.2 To create an index, uses db.tablename.ensureIndex(column)
. In below example, an index is created on column “username”.
译:使用 db.tablename.ensureIndex(column) 命令创建一个索引。在下面例子中,在"username"字段上创建一个索引。
> db.users.ensureIndex({username:1}) > db.users.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "mkyong.users", "name" : "_id_" }, { "v" : 1, "key" : { "username" : 1 }, "ns" : "mkyong.users", "name" : "username_1" } ]
8.3 To drop an index, uses db.tablename.dropIndex(column)
. In below example, the index on column “username” is deleted or dropped.
译:使用 db.tablename.dropIndex(column) 命令删除一个索引。 在下面例子中,删除 "username" 字段上的索引。
> db.users.dropIndex({username:1}) { "nIndexesWas" : 2, "ok" : 1 } > db.users.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "mkyong.users", "name" : "_id_" } ] >
8.4 To create an unique index, uses db.tablename.ensureIndex({column},{unique:true})
. In below example, an unique index is created on column “username”.
译:使用 db.tablename.ensureIndex({column},{unique:true}) 命令创建唯一索引。 在下面例子中,为"username"字段创建一个唯一索引。
> db.users.ensureIndex({username:1},{unique:true}); > db.users.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "mkyong.users", "name" : "_id_" }, { "v" : 1, "key" : { "username" : 1 }, "unique" : true, "ns" : "mkyong.users", "name" : "username_1" } ]
10. Help
At last, uses help()
to guide you how to do things in MongoDB.
译:最后,使用 help() 命令来获取帮助信息。
10.1 help
– All available commands.译:所有可用的命令信息。
> help db.help() help on db methods db.mycoll.help() help on collection methods rs.help() help on replica set methods help admin administrative help help connect connecting to a db help help keys key shortcuts //...
10.2 db.help()
– Shows help on db.译:显示db级别的帮助信息。
> 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) //...
10.3 db.collection.help()
– Shows help on collection (table).译:显示集合级别的帮助信息。
> db.users.help() DBCollection help db.users.find().help() - show DBCursor help db.users.count() db.users.dataSize() db.users.distinct( key ) - eg. db.users.distinct( 'x' ) db.users.drop() drop the collection db.users.dropIndex(name) //...
10.4 db.collection.function.help()
– Shows help on function.译:显示函数级别的帮助信息。
> db.users.find().help() find() modifiers .sort( {...} ) .limit( n ) .skip( n ) .count() - total # of objects matching query, ignores skip,limit .size() - total # of objects cursor would return, honors skip,limit .explain([verbose]) //...
Done. Hope this summary of MongoDB commands could help others.
References