question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Projections for nested documents in query

See original GitHub issue

The problem

Current syntax of projections comes from relational world where everything is a flat table. However there is links and nested records in document model. And there is no possibility to project fields of linked or nested documents and keep nesting.

There is a possibility to prevent loading of some fields of linked documents using fetch plan. But it is not allow to include only specific fields.

For example you have a Shop, Merchandise and Producer. And you want to select all shops with merchandise and its producers, but producer have a lot of properties and you need to load only its name and country.

SELECT name, include(merchandise, 'name', 'producer') FROM Shop

The query above loads all properties of producer.

fetchPlan and include/exclude functions Some use-cases can be implemented by fetchPlan and include/exclude functions, however this is less convenient and provide less flexibility.

Proposed syntax

selectQuery ::= SELECT <projections> FROM ...
projections ::= <projection>(, <projection>)*
projection ::= <fieldName>(':{' <projections> '}')? (as <alias>)?

Examples

SELECT 
  name, 
  address:{country:{name, capital} as country, city.name as city, street, building} 
FROM Customer

Result example:

{
  name: "Customer1"
  address: {
    country: {
      name: "Ukraine", 
      capital: "Kiev"
    }
    town: "Dnipropetrovsk"
    street: "..."
    building: 99
  }
}

Graph example:

SELECT 
  name, 
  out('repositories').out('commits'):{date, message} as commits 
FROM Developer

Result example:

{
  name: "Martin",
  commits: [
    {date: "12-08-2013", message: "Fix minor bug"}
    {date: "13-08-2013", message: "Fix major bug"}
  ]
}

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:24 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
luigidellaquilacommented, Jun 9, 2017

The nested projection syntax is implemented in develop branch (3.0 SNAPSHOT). It will be released with 3.0 M2.

For more details https://github.com/orientechnologies/orientdb-docs/blob/3.0.x/sql/SQL-Projections.md

0reactions
thinklinuxcommented, Mar 26, 2018

Is there a way for doing this with the projection api?

https://stackoverflow.com/questions/49495351/orientdb-returning-json-with-embedded-vertices-from-a-match-query

I’m trying for hours now and I can’t find the right syntax or any examples of this use case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query on Embedded/Nested Documents — MongoDB Manual
This page provides examples of query operations on embedded/nested documents using the db.collection.find() method in mongosh . The examples on this page ...
Read more >
Projection on nested object mongodb find query
I was trying to fetch the region of apikey 2. I tried below find query I tried find query i.e. db.collection.find({ "apikey": " ......
Read more >
MongoDB projection on specific nested properties?
For projection on specific nested properties, use aggregate() in MongoDB. Let us first create a collection with documents − > db.demo379.
Read more >
Efficient projection of a single nested document array to ...
I've written a query to retrieve a parent document by its key, and nest many child documents of specific types. The child documents...
Read more >
MongoDB - Query Embedded Documents Using Mongo Shell
projection : It is an optional parameter. It specifies that only those fields return to the document that matches the given query filter....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found