db.collection.insert(文档)

db.collection.insert({ })  : 插入单条

db.collection.insert([{ },{ }]) : 插入多条,   须用中括号括起来,否则默认插入第一条数据

其他插入方法:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
db.collection.insertOne():插入单个文档 ,返回包含id的文档              #yds : ??--------不存在该函数

db.collection.insertMany():插入多个文档,返回包含id的文档

db.collection.find() : 查询所有文档

db.collection.find().pretty() :  格式化返回查询结果

db.collection.findOne() : 返回一个文档

 

条件查询

db.collection.find({条件},{key:1}) : 控制显示所要查询的字段

第二参数不写时, 默认显示所有字段

写上时, 默认_id=1(显示), 其他未写的字段默认为0(不显示)

1.关系查询 (<,>,=,<=,>=,!=)

相等 {key:value}

小于 {key:{$lt:value}

小于等于 {key:{$lte:value}

大于 {key:{$gt:value}

小于等于 {key:{$gte:value}

不等于 {key:{$ne:value}

例: db.user.find{age:{$gt:20}} 查找年龄大于20的结果

 

2.逻辑查询(and,or)

and : db.collection.find({key1:value1,key2:value2})

or : db.collection.find({$or:[{key1:value1},{key2:value2}])

and 和 or 联合使用

 

3.读取数据(显示数据)

limit 和 skip : 类似mysql的 limit

mysql: limit 2,5 从下标2开始取5条数据 -- 第3,4,5,6,7条

mongodb: skip(2).limit(5) 跳过两条,取5条 -- 第 3,4,5,6,7 条

   limit():读取指定数量的记录
          db.student.find().limit(5)
  skip():跳过指定数量的数据
      db.student.find().skip(5)
  分页:
      db.student.find().skip(5).limit(5)

4.排序

db.collection.find().sort({key:1})

参数key:按字段key排序,1表示升序,-1表示降序

 

db.collection.update()

db.collection.update({匹配条件},{$set:{key:value}})  :  设置key的值 (没有则插入,有则修改)

db.collection.update({匹配条件},{$inc:{key:value}}) : key的值在原有基础上增加value

# $set 是在原有数据基础上修改 或 插入, 如果不写$set 或 $inc 则替换掉原有所有数据.
#匹配多个时,update默认只更新一个文档,如果要更新多个文档,则添加参数{multi:true}).
#未匹配到时,upsert:可选参数,如果不存在update的记录,是否插入该数据,true插入,默认false.

db.collection.save()

db.collection.save({ })  :  若文档包含了已存在 _id ,则**替换**该id 对应文档的数据   
      若没包含_id, 则插入一个新的文档,类似insert()

 

db.collection.remove() 接受两个参数

db.collection.remove({删除条件})   :  默认删除所有匹配到的文档

db.collection.remove({删除条件},{justOne:true}) : 只删除一个,   justOne默认为false 或 0

 

和python交互

import pymongo from pymongo import MongoClient from bson.objectid import ObjectId

1.建立连接 #创建MongoClient的对象 方式一 #特点:可以连接默认的主机和端口号 #client = MongoClient() 方式二 #明确指明主机和端口号 client = MongoClient('localhost',27017) client = MongoClient(host='localhost',port=27017) 方式三 #使用MongoDB URI的 client = MongoClient('mongodb://localhost:27017')

2.获取数据库

#方式一 db = client.test print(db) #方式二 #db = client['test']

3.获取集合

#方式一 collection = db.students #方式二 #collection = db['students']

4.文档

在pymongo中使用字典来表示文档 student1 = { 'id':'20180101', 'name':'jack', 'age':20, 'gender':'male' }

5.插入文档

  1. insert() : 返回一个mongodb自动生成的objectId

    result = collection.insert(student1) 插入单条

result = collection.insert([student2,student3]) 插入多条

  1. insert_one()

    result = collection.insert_one(student4)

  2. insert_many()

    result = collection.insert_many([student2,student3])

6.查询文档

  1. find_one()

    result = collection.find_one({'name':'jack'})

    #通过onjectId查询,需导入模块, 查询不到结果返货None

    result = collection.find_one({'_id':ObjectId('5b3ed21f2e1016e9ad2dc7b7')})

  2. find()

    results = collection.find({'age':{'$gt':20}})

    for r in results: print(r)

  3. count()

    count1 = collection.find().count()

  4. sort()

    r0 = collection.find().sort('name',pymongo.ASCENDING)

  5. limit(),skip() r0 = collection.find().sort('name',pymongo.ASCENDING).skip(2) r0 = collection.find().sort('name',pymongo.ASCENDING).skip(2).limit(5)

7.更新文档

1.update()

conditon = {'name':'jack'}

2.update_one()

3.update_many()

8.删除文档

1.remove() : 将符合条件的所有的数据全部删除 result = collection.remove({'name':'rose'})

2.delete_one() result = collection.delete_one({'name':'rose'})

3.delete_many() result = collection.delete_many({'name':'rose'})

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄