From f454776242df86dee7669909f5d15dd036d9bec1 Mon Sep 17 00:00:00 2001 From: Gao Date: Tue, 13 Jun 2023 19:23:56 +0200 Subject: [PATCH] regular backup --- testMongoDB.ipynb | 123 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 111 insertions(+), 12 deletions(-) diff --git a/testMongoDB.ipynb b/testMongoDB.ipynb index ac7ea66..fb88c95 100644 --- a/testMongoDB.ipynb +++ b/testMongoDB.ipynb @@ -16,44 +16,101 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 172, "metadata": {}, "outputs": [], "source": [ - "mongoClient = pymongo.MongoClient('mongodb://contorl:DyLab2021@127.0.0.1:27017/?authMechanism=DEFAULT')\n", + "mongoClient = pymongo.MongoClient('mongodb://userAdmin:DyLab2021userAdmin@127.0.0.1:27017/?authMechanism=DEFAULT')\n", "mongoDB = mongoClient.testDB\n", "mongoCollection = mongoDB.testCollection" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 174, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'ok': 1.0}" + ] + }, + "execution_count": 174, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# mongoClient.admin.command(\n", - "# 'createUser', 'userAdmin', \n", - "# pwd='DyLab2021userAdmin',\n", - "# roles=[{'role': 'userAdminAnyDatabase', 'db': 'admin'}]\n", + "# 'createUser', 'control', \n", + "# pwd='DyLab2021',\n", + "# roles=[{'role': 'readWriteAnyDatabase', 'db': 'admin'}]\n", "# )" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 173, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'ok': 1.0}" + ] + }, + "execution_count": 173, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# mongoClient.admin.command(\n", - "# 'dropUser', 'userAdmin'\n", + "# 'dropUser', 'contorl'\n", "# )" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 175, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'users': [{'_id': 'admin.control',\n", + " 'userId': Binary(b'\\xef\\x01\\xec\\xf4\\xfc\\xe2Bp\\x84v\\xcaZ\\xa3D\\xbb\\x18', 4),\n", + " 'user': 'control',\n", + " 'db': 'admin',\n", + " 'roles': [{'role': 'readWriteAnyDatabase', 'db': 'admin'}],\n", + " 'mechanisms': ['SCRAM-SHA-1', 'SCRAM-SHA-256']},\n", + " {'_id': 'admin.dbAdmin',\n", + " 'userId': Binary(b'\\xfc\\x84\\x07\\x13\\xfc\\x81F\\xc9\\x87\\xbc<\\xe6D\\xf7\\xcc\\xe0', 4),\n", + " 'user': 'dbAdmin',\n", + " 'db': 'admin',\n", + " 'roles': [{'role': 'dbAdminAnyDatabase', 'db': 'admin'}],\n", + " 'mechanisms': ['SCRAM-SHA-1', 'SCRAM-SHA-256']},\n", + " {'_id': 'admin.root',\n", + " 'userId': Binary(b'\\x07\\xe1\\xc7\\x18\\xca\\xacN5\\x93\\xf7{s`+\\xc2\\xf7', 4),\n", + " 'user': 'root',\n", + " 'db': 'admin',\n", + " 'roles': [{'role': 'root', 'db': 'admin'}],\n", + " 'mechanisms': ['SCRAM-SHA-1', 'SCRAM-SHA-256']},\n", + " {'_id': 'admin.userAdmin',\n", + " 'userId': Binary(b'\\x83U\\xb4YraK\\xc7\\xadB\\x11\\xcb\\x08\\x0eL\\x9a', 4),\n", + " 'user': 'userAdmin',\n", + " 'db': 'admin',\n", + " 'roles': [{'role': 'userAdminAnyDatabase', 'db': 'admin'}],\n", + " 'mechanisms': ['SCRAM-SHA-1', 'SCRAM-SHA-256']}],\n", + " 'ok': 1.0}" + ] + }, + "execution_count": 175, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# mongoClient.admin.command('usersInfo')" ] @@ -5421,6 +5478,8 @@ "metadata": {}, "outputs": [], "source": [ + "from collections import OrderedDict\n", + "\n", "import pymongo\n", "import xarray_mongodb\n", "import bson\n", @@ -5696,6 +5755,45 @@ " \n", " return self.mongoDB['global'].find_one(filter)\n", " \n", + " def read_global_all(self, shotNum, date=None):\n", + " \n", + " from xarray.core.utils import equivalent\n", + " \n", + " if not date is None:\n", + " self.set_date(date)\n", + " \n", + " filter = { \n", + " 'year': self.year,\n", + " 'month': self.month,\n", + " 'day': self.day,\n", + " 'shotNum': shotNum,\n", + " }\n", + " \n", + " result = {}\n", + " dropped_attrs = OrderedDict()\n", + " \n", + " for doc in self.mongoDB['global'].find(filter):\n", + " \n", + " result.update(\n", + " {\n", + " key: value\n", + " for key, value in attrs.items()\n", + " if key not in result and key not in dropped_attrs.keys()\n", + " }\n", + " )\n", + " result = {\n", + " key: value\n", + " for key, value in result.items()\n", + " if key not in attrs or equivalent(attrs[key], value)\n", + " }\n", + " dropped_attrs.update(\n", + " {\n", + " key: []\n", + " for key in attrs if key not in result \n", + " }\n", + " )\n", + " \n", + " \n", " def _load_data_single(self, mongoID, engine):\n", " if engine == 'xarray':\n", " return self.xdb.get(mongoID)\n", @@ -5730,6 +5828,7 @@ " \n", " def load_data(self, shotNum=None, globalDict=None, date=None, field=None):\n", " pass\n", + " \n", "DB = MongoDB(mongoClient, mongoDB, date='2023/05/23')" ] },