Django 与多个数据库 Mongodb 和 Neo4j



我使用Mongo作为我的主要数据库,使用Neo4j来存储一些关系。希望 Neo4j 可以减少我的应用程序中复杂搜索的查询时间。我对如何维持两者之间的关系感到困惑。

在这里,我的问题在这种情况下,我们如何在来自两个不同数据库的表之间创建关系

我正在研究Python3.6,Django2.1,django-neomodel0.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')

您可以将自动生成的属性id添加到MongoDB实体以及Neo4j实体中,将id分别存储在要链接的其他实体中,并在必要时通过id对象图映射库(neo4j-ogm(加载对象。

1. MongoDB部分(Java版本(

1.1 你的Mongo实体

@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.  
}

1.2 你的MongoEntityDAO

@Repository
public interface YourMongoEntityDAO extends MongoRepository<YourMongoEntity, String> {
YourMongoEntity findById(String id);
}

2. Neo4j部分(Java版本(

2.1 你的新4j实体

@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.  
}

2.2 你的新4j实体DAO

@Repository
public interface YourNeo4jEntityDAO extends Neo4jRepository<YourNeo4jEntity, Long> {
YourNeo4jEntity findId(Long id);
}

最新更新