mogui / pyorient

Orientdb driver for python that uses the binary protocol.
Apache License 2.0
166 stars 127 forks source link

OGM Queries #236

Open marigonda opened 7 years ago

marigonda commented 7 years ago

Hello !!! I'm stuck with OGM, trying to do queries! Could you please give a few examples?

I have something like:

Node = declarative_node()

class testeJon(Node): element_plural = "testeJons" attrib1 = String() attrib2 = DateTime()

and trying to

query = query.Query ( graph, [ testeJoao1, XXXX ] )

In XXXX I could see that it accepts a few things, an instance of a Property for example... but how?

Thanks in advance! Regards

Joao

TropicalPenguin commented 7 years ago

Whew, it's been a while. Have needed to rekindle my memory of these interfaces.

I suggest avoiding instantiating Query directly. Usage is a lot simpler through the graph, or what is termed the Node's 'broker' in the graph.

Through the graph: query = graph.query(testeJon.attrib1, testeJon.attrib2)

The resulting Query instance, query, will allow you to see just the two properties attrib1 and attrib2 (in case you happen to add more) of instances of testeJon.

Through the broker, I think the following approach works: query = graph.testeJons.query(testeJon.attrib1)

The broker being named according to the element_plural, this allows queries of just attrib1.

Using the interface also meant for more sophisticated queries (like functions on properties):

query = graph.testeJons.query().what(testeJon.attrib1)

One advantage the broker interface has over the graph interface is the specifying of filters passed to Query.filter_by. Passing a dictionary to the query:

query = graph.testeJons.query(attrib1='foo')

Gives you all vertexes (not just specific properties) in the graph with attrib1 equal to 'foo'.