ssplyh 发表于 2018-10-25 11:05:13

MongoDB基础操作

  mongoDB:
  有集合和文档概念
  集合==表
  文档==数据库表里的行
  !!建立数据库(bosenru)
  > use bosenru;
  switched to db bosenru
  !!往bosenrui里插入一张表(t1),表里插入(x:1)这个字段(x为字段,1为它的值)
  > db.ti.insert({x:1});
  WriteResult({ "nInserted" : 1 })
  查看一下
  >show tadabases;
  admin   (empty)
  bosenrui0.078GB
  local   0.078GB
  > use bosenru
  switched to db bosenru
  > show tables;
  system.indexes
  ti
  > db.t1.find()
  { "_id" : ObjectId("5602402d575c3e0a6920d326"), "x" : 1 }
  是它唯一的标实
  ObjectId("5602402d575c3e0a6920d326")
  > db.t1.insert({x:2})
  WriteResult({ "nInserted" : 1 })
  > db.t1.find()
  { "_id" : ObjectId("5602402d575c3e0a6920d326"), "x" : 1 }
  { "_id" : ObjectId("560240ea575c3e0a6920d327"), "x" : 2 }
  !!只查询一个数据:
  > db.t1.findOne()
  { "_id" : ObjectId("56023de2575c3e0a6920d324"), "x" : 1 }
  (One的首字母要大写)
  !!删掉bosenrui这个库:
  > db.dropDatabase()
  { "dropped" : "bosenrui", "ok" : 1 }
  (Database的首字母要大写)
  !!使用集合(表),文档(行记录)
  插入多条数据:
  mongodb可以使用json语法插入多条数据
  _id:全局唯一值
  > use bosenrui
  switched to db bosenrui
  指定(_id)为2
  > db.t1.insert({x:1,_id:2});
  WriteResult({ "nInserted" : 1 })
  WriteResult({ "nInserted" : 1 })
  > db.t1.find()
  { "_id" : 2, "x" : 1 }
  > db.t1.findOne()
  { "_id" : 2, "x" : 1 }
  再插入db.t1.insert({x:1,_id:2}),会有报错,是主键冲突。(不要去指定_id的值,让系统给分配)
  > db.t1.insert({x:1,_id:2})
  WriteResult({
  "nInserted" : 0,
  "writeError" : {
  "code" : 11000,
  "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: bosenru.t1.$_id_dup key: { : 2.0 }"
  !!插入多条数据:
  > for(i=1;i db.t1.find()
  { "_id" : ObjectId("560247edd5f225b12c0b67aa"), "x" : 1 }
  { "_id" : ObjectId("56024852d5f225b12c0b67ab"), "x" : 1 }
  { "_id" : ObjectId("56024852d5f225b12c0b67ac"), "x" : 2 }
  { "_id" : ObjectId("56024852d5f225b12c0b67ad"), "x" : 3 }
  { "_id" : ObjectId("56024852d5f225b12c0b67ae"), "x" : 4 }
  Type "it" for more(可以用it进行翻页)
  > db.t1.find().count()
  100
  总共有100条记录
  > db.t1.find().skip(1).limit(5).sort({x:1})
  { "_id" : ObjectId("56024852d5f225b12c0b67ab"), "x" : 1 }
  { "_id" : ObjectId("56024852d5f225b12c0b67ac"), "x" : 2 }
  { "_id" : ObjectId("56024852d5f225b12c0b67ad"), "x" : 3 }
  { "_id" : ObjectId("56024852d5f225b12c0b67ae"), "x" : 4 }
  { "_id" : ObjectId("56024852d5f225b12c0b67af"), "x" : 5 }
  > db.t1.find().skip(1).limit(5).sort({x:-1})
  { "_id" : ObjectId("56024852d5f225b12c0b680c"), "x" : 98 }
  { "_id" : ObjectId("56024852d5f225b12c0b680b"), "x" : 97 }
  { "_id" : ObjectId("56024852d5f225b12c0b680a"), "x" : 96 }
  { "_id" : ObjectId("56024852d5f225b12c0b6809"), "x" : 95 }
  { "_id" : ObjectId("56024852d5f225b12c0b6808"), "x" : 94 }
  !!跳过1行,查看5行记录,根据(x)这个字段,(1)为顺序值(正序),(-1)(倒序)
  查看(_id:2)的数据
  > db.t1.find({'_id':2})
  { "_id" : 2, "x" : 1 }
  !查看(x:?)的数据
  > db.t1.find({'x':2})
  { "_id" : ObjectId("56024852d5f225b12c0b67ac"), "x" : 2 }
  > db.t1.find({'x':4})
  { "_id" : ObjectId("56024852d5f225b12c0b67ae"), "x" : 4 }
  !!把(x:1)的数据更新成(x:999)
  > db.t1.update({x:1},{x:999})
  WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  再查看一下(x:1)的数据
  > db.t1.find({'x':1})
  { "_id" : ObjectId("56024852d5f225b12c0b67ab"), "x" : 1 }
  { "_id" : 2, "x" : 1 }
  再查看一下(x:999)的数据
  > db.t1.find({'x':999})
  { "_id" : ObjectId("560247edd5f225b12c0b67aa"), "x" : 999 }
  *如果集合中需要跟新的值有重复的那么只更新第一个
  !!多字段更新时,只需更新部分字段
  > db.t1.insert({x:100,y:100,z:100})
  WriteResult({ "nInserted" : 1 })
  > db.t1.find()
  { "_id" : ObjectId("56024f44d5f225b12c0b680e"), "x" : 100, "y" : 100, "z" : 100 }
  !!更新z为100时,y为99
  db.t1.update({z:100},{y:99})
  WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  查看一下:
  > db.t1.find()
  { "_id" : ObjectId("56024f44d5f225b12c0b680e"), "y" : 99 }
  这时(z,x)的值找不到
  > db.t1.find({'z':100})
  > db.t1.find({'x':100})
  可以找到(y:99)的值
  > db.t1.find({'y':99})
  { "_id" : ObjectId("56024f44d5f225b12c0b680e"), "y" : 99 }
  *这种方法是错的
  !!正确的方法:
  重新插入这个条数据
  > db.t1.insert({x:100,y:100,z:100})
  WriteResult({ "nInserted" : 1 })
  > db.t1.update({z:100},{$set:{y:99}})
  WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  查看一下:
  > db.t1.find()
  { "_id" : ObjectId("56025482d5f225b12c0b6810"), "x" : 100, "y" : 99, "z" : 100 }
  db.t1.update({z:100},{$set:{y:99}}),set为部分更新操作符,更新存在的字段,不存在的字段会保持原样。
  !!更新集合中不存在数据:
  > db.t1.find({y:100})
  >
  没有这条数据
  把(y:100)更新成为(y:99)
  > db.t1.update({y:100},{y:999},true)
  WriteResult({
  "nMatched" : 0,
  "nUpserted" : 1,
  "nModified" : 0,
  "_id" : ObjectId("5602576b03e4970d981cb3bf")
  查看一下:
  > db.t1.find({y:999})
  { "_id" : ObjectId("5602576b03e4970d981cb3bf"), "y" : 999 }
  !!如何批量更新:
  将(c:1)改为(c:2)
  插入三条数据:
  > db.t1.insert({c:1})
  WriteResult({ "nInserted" : 1 })
  > db.t1.insert({c:1})
  WriteResult({ "nInserted" : 1 })
  > db.t1.insert({c:1})
  WriteResult({ "nInserted" : 1 })
  查看一下:
  > db.t1.find()
  { "_id" : ObjectId("56025d10d5f225b12c0b6814"), "c" : 1 }
  { "_id" : ObjectId("56025d28d5f225b12c0b6815"), "c" : 1 }
  { "_id" : ObjectId("56025d29d5f225b12c0b6816"), "c" : 1 }
  进行批量更新:
  > db.t1.update({c:1},{$set:{c:2}},false,true)
  WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
  查看一下:
  > db.t1.find()
  { "_id" : ObjectId("56025d10d5f225b12c0b6814"), "c" : 2 }
  { "_id" : ObjectId("56025d28d5f225b12c0b6815"), "c" : 2 }
  { "_id" : ObjectId("56025d29d5f225b12c0b6816"), "c" : 2 }
  !!如何删除一个值:
  > db.t1.remove({c:2})
  WriteResult({ "nRemoved" : 3 })
  查看一下:
  > db.t1.find()
  >
  没有数据了
  这个方法可以删除集合下的文档
  !!如何删除集合:
  > db.t1.drop()
  true
  查看一下:
  > show tables;
  system.indexes
  没有t1这个文档了

页: [1]
查看完整版本: MongoDB基础操作