Skip to content

Commit 8d8aab2

Browse files
committed
Check for _id property in subdocument
If a subdocument contains an _id property, do not treat it as a reference/relationship, instead ignore the _id property as the _id from the root level document should take precedence. See #56
1 parent 7655b1a commit 8d8aab2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

mongo_connector/doc_managers/nodes_and_relationships_builder.py

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ def build_node_with_reference(self, root_type, key, doc_id, document_key):
5252
if document_key is None:
5353
return
5454
doc_type = key.split("_id")[0]
55+
56+
# ignore _id property of subdocuments
57+
if not doc_type or doc_type == "":
58+
return
59+
5560
parameters = {'_id':document_key}
5661
statement = "MERGE (d:Document:`{doc_type}` {{ _id: {{parameters}}._id}})".format(doc_type=doc_type)
5762
self.query_nodes.update({statement: {"parameters":parameters}})

tests/test_neo4j.py

+31
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def setUpClass(cls):
3030
cls.docman = DocManager('http://localhost:7474/db/data', auto_commit_interval=0)
3131

3232
def _count(cls):
33+
"""
34+
Return the number of nodes in the graph
35+
"""
3336
return cls.neo4j_conn.order
3437

3538
def query(cls):
@@ -93,6 +96,34 @@ def test_insert(self):
9396
self.assertEqual(result_set_1['name'], result_set_2['name'])
9497
self.connector.doc_managers[0].graph.delete_all()
9598

99+
def test_subdocument_with_id(self):
100+
"""
101+
Test inserting a document with a subdocument containing an _id property.
102+
In the current version of translating from document data model to property graph, the root level _id
103+
field is included in all subdocument nodes in the graph. This test verifies there is no error when
104+
inserting a document containing a subdocument with an _id property.
105+
106+
See https://github.com/neo4j-contrib/neo4j_doc_manager/issues/56
107+
108+
"""
109+
110+
doc = {
111+
'_id': 'root_level_object_id',
112+
'name': 'Bob',
113+
'location': {
114+
'_id': 'sub_document_object_id',
115+
'city': 'Missoula',
116+
'state': 'Montana'
117+
}
118+
}
119+
120+
self.connector.doc_managers[0].upsert(doc, "test.test_id", 1)
121+
assert_soon(lambda: self._count() > 0)
122+
result_set = self.neo4j_conn.find_one("location")
123+
self.assertNotEqual(result_set, None)
124+
self.assertEqual(result_set['_id'], 'root_level_object_id') # expect subdocument _id to be ignored
125+
126+
96127
def test_remove(self):
97128
"""Tests remove operations."""
98129
self.conn['test']['test'].insert({'name': 'paulie'})

0 commit comments

Comments
 (0)