From dc1fca1f94ea23c210b97237f589c51f76909d97 Mon Sep 17 00:00:00 2001 From: Zhong Han Date: Fri, 31 Mar 2017 15:45:14 +0800 Subject: [PATCH 1/3] Add support of "bson.dbref.DBRef" data type for formatting value. The "bson.dbref.DBRef" is used by Solr DBRef. --- mongo_connector/doc_managers/formatters.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mongo_connector/doc_managers/formatters.py b/mongo_connector/doc_managers/formatters.py index d5d6fca1..8e9348cf 100644 --- a/mongo_connector/doc_managers/formatters.py +++ b/mongo_connector/doc_managers/formatters.py @@ -96,6 +96,8 @@ def transform_value(self, value): return value elif isinstance(value, datetime.datetime): return value + elif isinstance(value, bson.dbref.DBRef): + return value.id elif value is None: return value # Default From 92c7c771724e4c741667a8eb71cc22e08802aba3 Mon Sep 17 00:00:00 2001 From: Zhong Han Date: Mon, 3 Apr 2017 15:45:38 +0800 Subject: [PATCH 2/3] Add support for DBRef. Now it normalized DBRef.collection, DBRef.database and DBref.id. --- mongo_connector/doc_managers/formatters.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/mongo_connector/doc_managers/formatters.py b/mongo_connector/doc_managers/formatters.py index 8e9348cf..6a3a54f0 100644 --- a/mongo_connector/doc_managers/formatters.py +++ b/mongo_connector/doc_managers/formatters.py @@ -96,8 +96,6 @@ def transform_value(self, value): return value elif isinstance(value, datetime.datetime): return value - elif isinstance(value, bson.dbref.DBRef): - return value.id elif value is None: return value # Default @@ -165,6 +163,25 @@ def flatten(doc, path): for inner_k, inner_v in flatten(v, path): yield inner_k, inner_v path.pop() + elif isinstance(v, bson.dbref.DBRef): + transformed = self.transform_element("database", v.database) + for new_k, new_v in transformed: + if top_level: + yield "%s.%s" % (k, new_k), new_v + else: + yield "%s.%s.%s" % (path_string, k, new_k), new_v + transformed = self.transform_element("collection", v.collection) + for new_k, new_v in transformed: + if top_level: + yield "%s.%s" % (k, new_k), new_v + else: + yield "%s.%s.%s" % (path_string, k, new_k), new_v + transformed = self.transform_element("id", v.id) + for new_k, new_v in transformed: + if top_level: + yield "%s.%s" % (k, new_k), new_v + else: + yield "%s.%s.%s" % (path_string, k, new_k), new_v else: transformed = self.transform_element(k, v) for new_k, new_v in transformed: From 2d8fb6231dbd7c5ec9dd3a2b1b7b820a782ffa6c Mon Sep 17 00:00:00 2001 From: Zhong Han Date: Mon, 3 Apr 2017 19:12:35 +0800 Subject: [PATCH 3/3] Add dollar sign as prefix of DBRef values. Stay same syntax with MongoDB. --- mongo_connector/doc_managers/formatters.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mongo_connector/doc_managers/formatters.py b/mongo_connector/doc_managers/formatters.py index 6a3a54f0..254b2e0b 100644 --- a/mongo_connector/doc_managers/formatters.py +++ b/mongo_connector/doc_managers/formatters.py @@ -165,23 +165,24 @@ def flatten(doc, path): path.pop() elif isinstance(v, bson.dbref.DBRef): transformed = self.transform_element("database", v.database) + for new_k, new_v in transformed: if top_level: - yield "%s.%s" % (k, new_k), new_v + yield "$%s.%s" % (k, new_k), new_v else: - yield "%s.%s.%s" % (path_string, k, new_k), new_v + yield "$%s.%s.%s" % (path_string, k, new_k), new_v transformed = self.transform_element("collection", v.collection) for new_k, new_v in transformed: if top_level: - yield "%s.%s" % (k, new_k), new_v + yield "$%s.%s" % (k, new_k), new_v else: - yield "%s.%s.%s" % (path_string, k, new_k), new_v + yield "$%s.%s.%s" % (path_string, k, new_k), new_v transformed = self.transform_element("id", v.id) for new_k, new_v in transformed: if top_level: - yield "%s.%s" % (k, new_k), new_v + yield "$%s.%s" % (k, new_k), new_v else: - yield "%s.%s.%s" % (path_string, k, new_k), new_v + yield "$%s.%s.%s" % (path_string, k, new_k), new_v else: transformed = self.transform_element(k, v) for new_k, new_v in transformed: