创建、删除数据库

  • use 数据库名称
  • show dbs 查询所有数据库
  • db.dropDatabase 删除数据库
> use mytest		##数据库不存在时创建,存在则切换到此数据库
  switched to db mytest
  > db				##查询当前数据库
  mytest
  > show dbs			##此时查询是没有刚创建的数据库的,因为数据库里没有内容
  admin   0.000GB
  config  0.000GB
  local   0.000GB
  testdb  0.000GB
  > db.test1.insert({"a":1})			##插入内容后再查询,数据库存在
  WriteResult({ "nInserted" : 1 })
  > show dbs
  admin   0.000GB
  config  0.000GB
  local   0.000GB
  mytest  0.000GB
  testdb  0.000GB
  > db.dropDatabase()					##删除当前数据库
  { "dropped" : "mytest", "ok" : 1 }
  > show dbs
  admin   0.000GB
  config  0.000GB
  local   0.000GB
  testdb  0.000GB

集合

创建集合
语法:db.createCollection(name, options)
options:

  1. capped :布尔(可选)如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。当该值为 true 时,必须指定 size 参数。
  2. autoIndexId: 布尔(可选)如为 true,自动在 _id 字段创建索引。默认为 false。
  3. size :数值(可选)为固定集合指定一个最大值(以字节计)。如果 capped 为 true,也需要指定
  4. max:数值(可选)指定固定集合中包含文档的最大数量。

实例:

> db.createCollection("test1",{capped:true,autoIndexID:true,size:10000,max:5})
			{
				"ok" : 0,
				"errmsg" : "BSON field 'create.autoIndexID' is an unknown field.",
				"code" : 40415,
				"codeName" : "Location40415"
			}
			> show collections 			##查询集合
			test1
			> db.test1.find()				##插入5条数据
			{ "_id" : ObjectId("5e9834fccb186e3027ff7000"), "a" : 1 }
			{ "_id" : ObjectId("5e9834fecb186e3027ff7001"), "a" : 2 }
			{ "_id" : ObjectId("5e983500cb186e3027ff7002"), "a" : 3 }
			{ "_id" : ObjectId("5e983502cb186e3027ff7003"), "a" : 4 }
			{ "_id" : ObjectId("5e983504cb186e3027ff7004"), "a" : 5 }

##因max值为5,再次插入时会自动删除第一条

> db.test1.find()
{ "_id" : ObjectId("5e9834fecb186e3027ff7001"), "a" : 2 }
{ "_id" : ObjectId("5e983500cb186e3027ff7002"), "a" : 3 }
{ "_id" : ObjectId("5e983502cb186e3027ff7003"), "a" : 4 }
{ "_id" : ObjectId("5e983504cb186e3027ff7004"), "a" : 5 }
{ "_id" : ObjectId("5e983562cb186e3027ff7005"), "a" : 6 }

注:在MongoDB中当你插入一些文档时会自动创建集合

> db.test2.insert({a:1})
> show collections
> test1
>test2

删除集合
语法:db.collection.drop() 成功返回true,失败则返回false

> db.test2.drop()
true

文档

  • 插入文档:db.COLLECTION_NAME.insert({key:valuse})
  • 查询文档内容:db.COLLECTION_NAME.find()

实例:

> db.test1.insert({"name":"lisi","age":18})
WriteResult({ "nInserted" : 1 })
> db.test1.find()
{ "_id" : ObjectId("5e98392bcb186e3027ff7007"), "name" : "lisi", "age" : 18 }

或将值定义成一个变量插入

> abc=({"name":"xx","age":20})
{ "name" : "xx", "age" : 20 }
> db.test1.insert(abc)
WriteResult({ "nInserted" : 1 })
> db.test1.find()
{ "_id" : ObjectId("5e98392bcb186e3027ff7007"), "name" : "lisi", "age" : 18 }
{ "_id" : ObjectId("5e983a1dcb186e3027ff7008"), "name" : "xx", "age" : 20 }

注:3.2版本后还有几种语法

db.collection.insertOne():向指定集合中插入一条文档数据
db.collection.insertMany():向指定集合中插入多条文档数据
  • 更新文档
    update方法:用于更新已存在的文档
    参数说明:
    1.query : update的查询条件,类似sql update查询内where后面的。
    2.update : update的对象和一些更新的操作符(如mongodb集合去掉id mongodb删除集合命令_数据库inc…)等,也可以理解为sql update查询内set后面的
    3.upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入新的,true为插入,默认是false,不插入。
    4.multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

实例:

> db.test1.find()
{ "_id" : ObjectId("5e98392bcb186e3027ff7007"), "name" : "lisi", "age" : 18 }
{ "_id" : ObjectId("5e983a1dcb186e3027ff7008"), "name" : "xx", "age" : 20 }
> db.test1.update({"name":"lisi"},{$set:{"name":'aa'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test1.find()
{ "_id" : ObjectId("5e98392bcb186e3027ff7007"), "name" : "aa", "age" : 18 }
{ "_id" : ObjectId("5e983a1dcb186e3027ff7008"), "name" : "xx", "age" : 20 }

##如果有多条相同的文档可以设置multi为true
> db.test2.find()
{ "_id" : ObjectId("5e983d24cb186e3027ff700f"), "a" : 1 }
{ "_id" : ObjectId("5e983d26cb186e3027ff7010"), "a" : 1 }
{ "_id" : ObjectId("5e983d26cb186e3027ff7011"), "a" : 1 }
> db.test2.update({a:1},{$set:{a:2}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.test2.find()
{ "_id" : ObjectId("5e983d24cb186e3027ff700f"), "a" : 2 }
{ "_id" : ObjectId("5e983d26cb186e3027ff7010"), "a" : 2 }
{ "_id" : ObjectId("5e983d26cb186e3027ff7011"), "a" : 2 }

save() 方法:通过传入的文档来替换已有文档
实例:

> db.test2.find()
{ "_id" : ObjectId("5e983ea0cb186e3027ff7014"), "a" : 1 }
> db.test2.save({"_id" : ObjectId("5e983ea0cb186e3027ff7014"),a:2})	##更改id为ObjectId("5e983ea0cb186e3027ff7014")数据
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test2.find()
{ "_id" : ObjectId("5e983ea0cb186e3027ff7014"), "a" : 2 }

删除文档 remove()、deleteOne()、 deleteMany()
1.remove()

  • query :(可选)删除的文档的条件。
  • justOne : (可选)如果设为 true 或 1,则只删除一个文档。

实例:

> db.test2.find()
{ "_id" : ObjectId("5e983ea0cb186e3027ff7014"), "a" : 2 }
{ "_id" : ObjectId("5e98632dfb04aed167df32e1"), "a" : 1 }
{ "_id" : ObjectId("5e98632ffb04aed167df32e2"), "a" : 3 }
{ "_id" : ObjectId("5e986330fb04aed167df32e3"), "a" : 4 }
> db.test2.remove({"a":2})
WriteResult({ "nRemoved" : 1 })				##删除"a":2的文档
> db.test2.find()
{ "_id" : ObjectId("5e98632dfb04aed167df32e1"), "a" : 1 }
{ "_id" : ObjectId("5e98632ffb04aed167df32e2"), "a" : 3 }
{ "_id" : ObjectId("5e986330fb04aed167df32e3"), "a" : 4 }

删除所有 remove({})

> db.test2.find()
{ "_id" : ObjectId("5e98632dfb04aed167df32e1"), "a" : 1 }
{ "_id" : ObjectId("5e98632ffb04aed167df32e2"), "a" : 3 }
{ "_id" : ObjectId("5e986330fb04aed167df32e3"), "a" : 4 }
> db.test2.remove({})
WriteResult({ "nRemoved" : 3 })
> db.test2.find()
>
  1. deleteOne() 删除符合条件的一个文档
> db.test2.find()
 { "_id" : ObjectId("5e98642cfb04aed167df32e4"), "a" : 1 }
 { "_id" : ObjectId("5e98642dfb04aed167df32e5"), "a" : 1 }
 { "_id" : ObjectId("5e98642dfb04aed167df32e6"), "a" : 1 }
 { "_id" : ObjectId("5e98642ffb04aed167df32e7"), "a" : 2 }
 { "_id" : ObjectId("5e986431fb04aed167df32e8"), "a" : 3 }
 > db.test2.deleteOne({a:2})
 { "acknowledged" : true, "deletedCount" : 1 }
 > db.test2.find()
 { "_id" : ObjectId("5e98642cfb04aed167df32e4"), "a" : 1 }
 { "_id" : ObjectId("5e98642dfb04aed167df32e5"), "a" : 1 }
 { "_id" : ObjectId("5e98642dfb04aed167df32e6"), "a" : 1 }
 { "_id" : ObjectId("5e986431fb04aed167df32e8"), "a" : 3 }

3.deleteMany() 删除符合条件的全部文档

> db.test2.find()
{ "_id" : ObjectId("5e98642cfb04aed167df32e4"), "a" : 1 }
{ "_id" : ObjectId("5e98642dfb04aed167df32e5"), "a" : 1 }
{ "_id" : ObjectId("5e98642dfb04aed167df32e6"), "a" : 1 }
{ "_id" : ObjectId("5e986431fb04aed167df32e8"), "a" : 3 }
> db.test2.deleteMany({"a":1})
{ "acknowledged" : true, "deletedCount" : 3 }
> db.test2.find()
{ "_id" : ObjectId("5e986431fb04aed167df32e8"), "a" : 3 }