设为首页 加入收藏

TOP

mongdb文件型数据库开发实例(二)
2014-11-24 07:53:19 来源: 作者: 【 】 浏览:4
Tags:mongdb 文件 数据库 开发实例
123” }
6.3 Find records where username’s length is less than or equal to 2
6.3查找记录,其中username的长度小于或等于2
6.4 Find records where username field is existed.
6.4查找其中username字段存在的记录。
db.users.find({username:{$exists : true}})
7。删除记录
要删除一条记录,使用db.tablename.remove({标准}) 。在下面的例子中,用户名 “google”的记录将被删除。
> db.users.remove({username:"google"})
> db.users.find()
{ "_id" : ObjectId("504f45cd17f6c778042c3c07"), "password" : "hello123", "username" : "mkyong" }
使用db.tablename.remove()注意:从表中删除所有记录,
删除表,使用db.tablename.drop() 。
8. Indexing
8。索引
指数可以帮助您提高数据查询的速度。
8.1 List all indexes of table “users”, by default the column “_id” is always the primary key and created automatically.
8.1名单表“用户”,默认情况下,所有的索引列“_id”始终是主键和自动创建。
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "mkyong.users",
"name" : "_id_"
}
]
>
8.2要创建索引时,使用db.tablename.ensureIndex(列)。在下面的例子中,“用户名”列上创建索引。
> 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删除索引,使用db.tablename.dropIndex(列)。在下面的例子中,“用户名”列上的索引被删除或丢弃
> db.users.dropIndex({username:1})
{ "nIndexesWas" : 2, "ok" : 1 }
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "mkyong.users",
"name" : "_id_"
}
]
>
8.4要创建一个唯一索引,使用db.tablename.ensureIndex({列},{独特之处:真}) 。在下面的例子中,“用户名”列上创建唯一索引。
> 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。帮助
最后,使用说明()来指导你如何在MongoDB做的事情。
10.1 help – All available commands.
10.1 帮助 -所有可用的命令。
> 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
//...
> 帮助
db.help() 帮助DB方法 db.mycoll.help() 帮助收集方法 rs.help()
副本一套方法
帮助帮助admin管理员的帮助
帮助连接到一个数据库帮助
帮助键快捷键
/ /...
10.2 db.help() -显示help 在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.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)
//...
>db.users.help()
DBCollect
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇full join语句练习 下一篇MongoDB数据修改总结

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)