首页 > 数据库技术 > 详细

mongodb基本操作

时间:2015-08-30 08:48:49      阅读:234      评论:0      收藏:0      [点我收藏+]

在MongoDB 中,一个数据库可能会包含多个集合,就像MySql中一个数据库可能包含多个表;一个集合中可能又会包含多个文档,就像MySql中一个表中包含多条数据。


基本操作命令:
show dbs  查看所有数据库列表
use <dbname> 连接指定的数据库
db  查看当前使用的数据库
> use testdb                      # 创建testdb数据库
switched to db testdb
> show dbs
admin       0.078GB
ceilometer  1.953GB
local       6.075GB
列出的数据库中没有testdb或者显示testdb(empty),是因为testdb里面没有任何东西。

> use testdb                     # 删除testdb数据库
switched to db testdb
> db.dropDatabase()
{ "dropped" : "testdb", "ok" : 1 }

> use testdb                             # 创建集合
switched to db testdb
> db.createCollection(‘users‘)
{ "ok" : 1 }
> show collections
system.indexes
users

> show collections            # 删除集合
system.indexes
users
> db.users.drop()
true
> show collections
system.indexes

> db.createCollection(‘users‘)   # 往集合中插入数据,如果集合没有会自动创建
{ "ok" : 1 }
> show collections
system.indexes
users
> db.users.insert([
... { name : ‘yao‘,
... email : ‘yao@qq.com‘
... },
... { name: ‘shen‘,
... email : ‘shen@qq.com‘
... }
... ])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

> db.users.save([                         #  save也能实现上述insert的功能
... { name : ‘test‘,
... email : ‘test@qq.com‘
... }
... ])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 1,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

数据查询命令:
> db.users.find()               # find不加任何参数返回所有数据记录
{ "_id" : ObjectId("55e1cf2326f975bfd9b902e9"), "name" : "yao", "email" : "yao@qq.com" }
{ "_id" : ObjectId("55e1cf2326f975bfd9b902ea"), "name" : "shen", "email" : "shen@qq.com" }
{ "_id" : ObjectId("55e1d32b26f975bfd9b902ec"), "name" : "test", "email" : "test@qq.com" }

> db.users.find().pretty()     # pretty查询输出结果更美观显示
{
	"_id" : ObjectId("55e1cf2326f975bfd9b902e9"),
	"name" : "yao",
	"email" : "yao@qq.com"
}
{
	"_id" : ObjectId("55e1cf2326f975bfd9b902ea"),
	"name" : "shen",
	"email" : "shen@qq.com"
}
{
	"_id" : ObjectId("55e1d32b26f975bfd9b902ec"),
	"name" : "test",
	"email" : "test@qq.com"
}

> db.users.find({‘name‘:‘yao‘,‘email‘:‘yao@qq.com‘}).pretty()  # find中传入多个键值对,mongodb针对多个键值对会当做and处理
{
	"_id" : ObjectId("55e1cf2326f975bfd9b902e9"),
	"name" : "yao",
	"email" : "yao@qq.com"
}

> db.users.find({$or: [{‘name‘:‘yao‘}, {‘email‘:‘test@qq.com‘}]}).pretty()  # or查询语句需要$or关键字
{
	"_id" : ObjectId("55e1cf2326f975bfd9b902e9"),
	"name" : "yao",
	"email" : "yao@qq.com"
}
{
	"_id" : ObjectId("55e1d32b26f975bfd9b902ec"),
	"name" : "test",
	"email" : "test@qq.com"
}

> db.users.find({‘name‘: ‘yao‘, $or: [{‘name‘:‘yao‘}, {‘email‘:‘test@qq.com‘}]}).pretty() # and、or一起用
{
	"_id" : ObjectId("55e1cf2326f975bfd9b902e9"),
	"name" : "yao",
	"email" : "yao@qq.com"
}







本文出自 “the-way-to-cloud” 博客,请务必保留此出处http://iceyao.blog.51cto.com/9426658/1689662

mongodb基本操作

原文:http://iceyao.blog.51cto.com/9426658/1689662

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!