2013-10-12 14 views
2

可以說我有一些在mongoDB中的集合, ,我想用rdflib創建所有可能的關係圖。 例如,如果我在DB 3項:通過使用rdflib獲取所有DB關係

FIRST{color:red, name:Ben, age: 29} 
SECOND{color :blue ,name:David, age:29} 
THIRD{color :blue,name:Mark,age:34} 

那麼首先會涉及到SECOND(歲),三分之一將涉及到SECOND(顏色) 另外,我怎麼可以將結果保存爲RDF文件和查看它與一些rdf查看器(例如rdf重力) 我感謝您的幫助。

+1

你有沒有考慮過使用graphDB? –

回答

1

對於這個應用程序,A graph database可能比MongoDB更好。與MongoDB的做到這一點最簡單的方法將採取1個+ N次查詢:

# Get a cursor for the entire collection 
docs = db.collection.find() 

for doc in docs: 
    # Get all documents that have a common element 
    related_docs = db.collection.find({"$or": [ 
     {"color": doc["color"]}, 
     {"name": doc["name"]}, 
     {"age": doc["age"]}, 
     ]}) 

    # Record the relationships in whatever structure you're using 
    for related_doc in related_docs: 
     store_relationship(doc, related_doc) 

你可以把通過跟蹤文件對您已經看到了,而忽略重複這種更有效率。正如你寫的,你會看到每個邊緣兩次。

相關問題