话不多说上代码:
import pymongo
from pymongo import UpdateOne
import datetime
def set_expire_time(collection, seconds):
"""
设置过期时间
:param collection:
:param seconds:
:return:
"""
collection.create_index([("time", pymongo.ASCENDING)], expireAfterSeconds=seconds)
class MongoDB:
def __init__(self, hostname='localhost', port=27017, user=None, password=None):
if user is not None and password is not None:
uri = "mongodb://{}:{}@{}:{}" .format(user, password, hostname,port)
self.client = pymongo.MongoClient(uri)
else:
self.client = pymongo.MongoClient(host=hostname, port=port)
def close(self):
self.client.close()
if __name__ == '__main__':
mongo_ = MongoDB(hostname='localhost').client
mydb = mongo_["runoobdb"]
mycol = mydb["site4"]
mylist = [
{"_id": 1, "name": "RUNOOB", "cn_name": "菜鸟教程"},
{"_id": 2, "name": "Google", "address": "Google 搜索"},
{"_id": 3, "name": "Facebook", "address": "脸书"},
{"_id": 4, "name": "Taobao", "address": "淘宝"},
{"_id": 5, "name": "Zhihu", "address": "知乎"}
]
mylist = [
{"_id": 1, "name": "RUNOOB", "cn_name": "2222"},
{"_id": 2, "name": "Google", "address": "Google 搜索"},
{"_id": 3, "name": "Facebook", "address": "脸书"},
{"_id": 4, "name": "Taobao", "address": "淘宝"},
{"_id": 5, "name": "Zhihu", "address": "知乎"}
]
ids =[1, 2]
result = mycol.find({'_id': {'$in': ids}}, {"_id": 0})
print(list(result))