我使用 Mongo 作为我的主数据库,并使用 Neo4j 存储一些关系。希望Neo4j可以减少我的应用程序中复杂搜索的查询时间。如何维护两者之间的关系。
这是我的问题在这种情况下如何在两个不同数据库的表之间建立关系?
我正在使用 Python3.6,Django2.1 ,django-neomodel 0.0.4和Djongo 1.2.30
这是我的models.py示例:
class Listing(models.Model):
''' Listing Model for mongo database '''
create_time = models.DateTimeField()
category = models.EmbeddedModelField(
model_container=Category,
)
subcategory = models.EmbeddedModelField(
model_container=Subcategory,
model_form_class=SubcategoryForm
)
...
class Listingnode(DjangoNode):
uid = UniqueIdProperty()
list_id = StringProperty()
status = StringProperty()
created = DateTimeProperty(default=datetime.utcnow)
price_range = RelationshipTo('PricerangeNodes','PRICE_RANGE')
tags = RelationshipTo('TagNodes','TAGS')
答案 0 :(得分:1)
您可以将自动生成的属性id
添加到MongoDB实体以及Neo4j实体,将id
存储在分别链接的其他实体中,并通过对象图加载对象必要时,通过存储的id
映射库(neo4j-ogm)。
@Document
public class YourMongoEntity {
@Id
private String id;
@Indexed(unique = true)
private String furtherIdentifier;
// For reasons of clarity the default constructor, getter and setter are omitted.
}
@Repository
public interface YourMongoEntityDAO extends MongoRepository<YourMongoEntity, String> {
YourMongoEntity findById(String id);
}
@NodeEntity
public class YourNeo4jEntity {
@Id
@GeneratedValue
private Long id;
@Index(unique = true)
private Long furtherIdentifier;
// For reasons of clarity the default constructor, getter and setter are omitted.
}
@Repository
public interface YourNeo4jEntityDAO extends Neo4jRepository<YourNeo4jEntity, Long> {
YourNeo4jEntity findId(Long id);
}