windows安装、使用MongoDB

安裝

  1. MongoDB下载,下载zip包,解压
  2. 添加环境变量
  3. 创建数据库文件夹 C:MongoDb_dataDB
  4. 配置数据库并启动 mongod --dbpath C:MongoDb_dataDB

配置文件:

在MongoDB根目录创建文件mongod.cfg,写入:

systemLog:
    destination: file
    path: C:MongoDb_datalogmongod.log
storage:
    dbPath: C:MongoDb_dataDB

安装 MongoDB服务:

mongod.exe --config "C: oolsmongodb-win32-x86_64-2012plus-4.2.8mongod.cfg" --install

CMD管理员模式:

启动MongoDB服务

net start MongoDB

关闭MongoDB服务

net stop MongoDB

移除 MongoDB 服务

C:mongodbinmongod.exe --remove

命令行输入mongo进入


指定数据库

db = client.test  # 使用test数据库
db = client['test'] # 使用test数据库

MongoDB 的每个数据库又包含许多集合(collection),它们类似于关系型数据库中的表。

指定集合

collection = db.students
#或是
collection = db['students']

插入数据

student = {
    'id': '20170101',
    'name': 'Jordan',
    'age': 20,
    'gender': 'male'
}
result = collection.insert(student)
print(result)

在 MongoDB 中,每条数据其实都有一个 _id 属性来唯一标识。如果没有显式指明该属性,MongoDB 会自动产生一个 ObjectId 类型的 _id 属性。insert() 方法会在执行后返回_id 值。

在 PyMongo 中,官方已经不推荐使用 insert 方法了。但是如果你要继续使用也没有什么问题。目前,官方推荐使用 insert_one 和 insert_many 方法来分别插入单条记录和多条记录,示例如下:

result = collection.insert_one(student)

插入多条数据:

student1 = {
    'id': '20170101',
    'name': 'Jordan',
    'age': 20,
    'gender': 'male'
}

student2 = {
    'id': '20170202',
    'name': 'Mike',
    'age': 21,
    'gender': 'male'
}

result = collection.insert_many([student1, student2])
print(result)
print(result.inserted_ids)

查询

单数据查询

result = collection.find_one({'name': 'Mike'})
print(type(result))
print(result)

对于多条数据的查询,我们可以使用 find 方法。例如,这里查找年龄为 20 的数据,示例如下:

results = collection.find({'age': 20})
print(results)
for result in results:
    print(result)

如果要查询年龄大于 20 的数据,则写法如下:

results = collection.find({'age': {'$gt': 20}})
$lt     <
$gt     >
$lte    <=
$gte    >=
$ne     不等于
$in     在范围内
$nin    不在范围内

另外,还可以进行正则匹配查询。例如,查询名字以 M 开头的学生数据,示例如下:

results = collection.find({'name': {'$regex': '^M.*'}})

高级查询

$regex  正则
$exists 判断属性是否存在
$type   类型判断
$mod    数字模操作
$text   文本查询
$where  高级条件查询    {'$where':obj.fans_count==obj.follows_count}

详细用法

计数:collection.find().count()

排序:collection.find().sort('name',pymongo.ASCENDING)

排序后偏移:collection.find().sort('name', pymongo.ASCENDING).skip(2)

取2个:collection.find().sort('name', pymongo.ASCENDING).skip(2).limit(2)

值得注意的是,在数据量非常庞大的时候,比如在查询千万、亿级别的数据库时,最好不要使用大的偏移量,因为这样很可能导致内存溢出。此时可以使用类似如下操作来查询:

from bson.objectid import ObjectId
collection.find({'_id': {'$gt': ObjectId('593278c815c2602678bb2b8d')}})

更新:

condition = {'name': 'Kevin'}
student = collection.find_one(condition)
student['age'] = 25
result = collection.update(condition, student)
print(result)
result = collection.update(condition, {'$set': student})

这样可以只更新 student 字典内存在的字段。如果原先还有其他字段,则不会更新,也不会删除。而如果不用 $set 的话,则会把之前的数据全部用 student 字典替换;如果原本存在其他字段,则会被删除。

condition = {'age': {'$gt': 20}}
result = collection.update_one(condition, {'$inc': {'age': 1}})
print(result)
print(result.matched_count, result.modified_count)

这里指定查询条件为年龄大于 20,然后更新条件为 {'$inc': {'age': 1}},表示年龄加 1,执行之后会将第一条符合条件的数据年龄加 1。(update_many改多条)

删除

result = collection.remove({'name': 'Kevin'})
print(result)

另外,这里依然存在两个新的推荐方法 —— delete_one 和 delete_many,示例如下:

result = collection.delete_one({'name': 'Kevin'})
print(result)
print(result.deleted_count)
result = collection.delete_many({'age': {'$lt': 25}})
print(result.deleted_count)

其他操作

另外,PyMongo 还提供了一些组合方法,如 find_one_and_delete、find_one_and_replace 和 find_one_and_update,它们分别用于查找后删除、替换和更新操作,其使用方法与上述方法基本一致。

另外,我们还可以对索引进行操作,相关方法有 create_index、create_indexes 和 drop_index 等。

关于 PyMongo 的详细用法,可以参见官方文档:http://api.mongodb.com/python/current/api/pymongo/collection.html

另外,还有对数据库和集合本身等的一些操作,这里不再一一讲解,可以参见官方文档:http://api.mongodb.com/python/current/api/pymongo/

原文地址:https://www.cnblogs.com/Neroi/p/13215449.html