mongo数据库的基本操作
1、{ “authors.name” : { $type : 2} }
2、{ ‘authors.org’: { $type : 4} }
查询作者字段下面的,org字段里面是否有list对象,本来里面应该全是对象类型的。
https://docs.mongodb.com/manual/reference/operator/query/type/
总览
https://docs.mongodb.com/manual/reference/glossary/#std-term-BSON
3、{ ‘venue.sid’: { $exists: false } }
查询不存在这个字段的记录
https://docs.mongodb.com/manual/reference/operator/query/exists/
4、{’_id’:ObjectId(‘5feee51891e0113b2659fd0e’)}
5、** 数据库连接的类**
#-*- encoding: utf-8 -*-
from calendar import day_name
import pymongo
from config import mongodb_ip,mongodb_port
"""
使用方法如下所示:
table_database=mongodb_connect.MongoDBUtil('web')
# 传入你要使用的数据库名称
table_uesrEventLog=table_database.create_database('user_event_log')
# 传入你要使用的数据表名称
"""
class MongoDBUtil:
"""
MongoDB工具类
"""
def __init__(self,db_name=None):
"""构造函数"""
self.mongo_client = pymongo.MongoClient(host=mongodb_ip, port=mongodb_port)
self.mongo_auth_db = self.mongo_client.aminer
self.mongo_auth_db.authenticate(name="aminer_platform_reader", password="Reader@123", mechanism="SCRAM-SHA-1")
self.mongo_db_web = self.mongo_client[db_name]
def __del__(self):
"""析构函数"""
# print("__del__")
self.mongo_client.close()
def create_database(self, table_name):
self.table_uesrEventLog = self.mongo_db_web[table_name]
"""创建数据库"""
return self.table_uesrEventLog
这篇博客介绍了MongoDB数据库的基本查询操作,包括检查字段类型(如`authors.org`是否为list)和检查字段是否存在(如`venue.sid`)。同时,展示了如何使用Python的pymongo库建立数据库连接,并提供了`MongoDBUtil`类的实现,用于创建数据库实例。示例中还给出了查询特定_id记录的方法。

被折叠的 条评论
为什么被折叠?



