MongoDB安装及增删改查操作
?
2. 连接MongoDB
再打开一个命令行,执行以下mongo命令:
?
?
?
以下是三个常用的命令
?
5. 更新记录
更新一条记录语法:
db.tablename.update({criteria},{$set: {new value}}).
下面的例子中更新用户henry的密码:
?
?
?
6. 查询记录
查找记录的语法:
db.tablename.find({criteria}).
6.1 列出users表中的所有记录
?
?
?
6.2 查询username 为henry的记录
?
?
?
7. 删除记录
删除记录的语法:
db.tablename.remove({criteria}).
下面的例子
?
?
?
Note
To delete all records from a table, uses db.tablename.remove().
To drop the table, uses db.tablename.drop().
8. 索引
Index(索引)可以提高查询的效率
8.1 List all indexes of table “users”, by default the column “_id” is always the primary key and created automatically.
?
?
?
8.2 To create an index, uses db.tablename.ensureIndex(column). In below example, an index is created on column “username”.
?
?
?
8.3 To drop an index, uses db.tablename.dropIndex(column). In below example, the index on column “username” is deleted or dropped.
?
?
?
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”.
?
?
?
10. 帮助
At last, uses help() to guide you how to do things in MongoDB.
10.1 help – All available commands.
?
?
Done. Hope this summary of MongoDB commands could help others.
References
Official MongoDB tutorials
MongoDB Indexes
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/