Pregunta

Lets say i have collection of some kind in mongoDB, and i want to create graph with ALL possible relations using rdflib. For example if I have 3 entries in my DB:

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

Then FIRST will relate to SECOND(age), THIRD will relate to SECOND(color) Also, how i can save results as rdf file and view it with some rdf viewer(for example rdf-gravity) I appreciate your help.

¿Fue útil?

Solución

A graph database is probably a better tool for this application than MongoDB. The simplest way to do it with MongoDB would take 1+N queries:

# 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)

You could make this more efficient by keeping track of which document pairs you've already seen and ignoring repeats. As written you'll see each edge twice.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top