# MongoDB/Virtual Graph Problem with SPARQL

**URL:** <https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494>\
**Category:** Support\
**Created:** [January 29, 2022, 2:17pm UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494 "2022-01-29T14:17:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![DennisThiery](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DennisThiery](https://community.stardog.com/u/DennisThiery)\
**Post date:** [January 29, 2022, 2:17pm UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494/1 "2022-01-29T14:17:29Z")

</div>

Hello all,  
I have created a mapping for a Mongo DB to virtualise data which is split into messages and observations. The mapping seems to work, but I can't access the data properties of the observations via SPARQL. I only get to the individual and then I don't retrieve anything.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/3/3b3c04b70dd07ce5cabd54c744d18715e163efc6.png)

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/5/5cc023e0f41336903873660c754b5ecae50c414d.png)

If I now search directly for the appropriate observation and want to retrieve the data property, it gives me the following result

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/5/57ad163b53df1cf37012086f57b54966f869dc4d.png)

The mapping code can be found below.

PREFIX rdf: [http://www.w3.org/1999/02/22-rdf-syntax-ns#](http://www.w3.org/1999/02/22-rdf-syntax-ns#)

PREFIX rdfs: [http://www.w3.org/2000/01/rdf-schema#](http://www.w3.org/2000/01/rdf-schema#)

PREFIX sosa: [http://www.w3.org/ns/sosa/](http://www.w3.org/ns/sosa/)

PREFIX FTOnto: [http://iot.uni-trier.de/FTOnto#](http://iot.uni-trier.de/FTOnto#)

PREFIX StreamDataAnnotationOnto: [http://iot.uni-trier.de/StreamDataAnnotationOnto#](http://iot.uni-trier.de/StreamDataAnnotationOnto#)

MAPPING urn:Oven

FROM JSON {

"Oven":{

```
"_id":"?Oven",

"ID":"?O_ID",

"TIMESTAMP":"?Timestamp",

"I1_POSITION_SWITCH_PRESSED":"?I1_POSITION_SWITCH_PRESSED",

"I2_POSITION_SWITCH_PRESSED":"?I2_POSITION_SWITCH_PRESSED",

"I5_LIGHT_BARRIER_INTERRUPTED":"?I5_LIGHT_BARRIER_INTERRUPTED",

"M1_SPEED":"?M1_SPEED",

"O7_VALVE_OPEN":"?O7_VALVE_OPEN",

"O8_COMPRESSOR_POWER_LEVEL":"?O8_COMPRESSOR_POWER_LEVEL",

"CURRENT_STATE":"?CURRENT_STATE",

"CURRENT_TASK":"?CURRENT_TASK",

"CURRENT_TASK_ELAPSED_SECONDS_SINCE_START":"?CURRENT_TASK_ELAPSED_SECONDS_SINCE_START",

"CURRENT_SUB_TASK":"?CURRENT_SUB_TASK",

"BUSINESS_KEY":"?BUSINESS_KEY",

"PROCESS_DEFINITION_ID":"?PROCESS_DEFINITION_ID" 

```

}

}

TO {

?Message a FTOnto:Kafka\_Message\_OV1;

FTOnto:timestamp ?Timestamp ;

StreamDataAnnotationOnto:isPublishedToTopic StreamDataAnnotationOnto:Kafka\_Topic\_OV\_1 ;

```
FTOnto:has_Information ?ID ;

FTOnto:has_Information ?I1 ;

FTOnto:has_Information ?I2 ;

FTOnto:has_Information ?LBO ;

FTOnto:has_Information ?M1 ;

FTOnto:has_Information ?O7 ;

FTOnto:has_Information ?O8 ;

FTOnto:has_Information ?CS ;

FTOnto:has_Information ?CT ;

FTOnto:has_Information ?CTESSS ;

FTOnto:has_Information ?CST ;

FTOnto:has_Information ?BK ;

FTOnto:has_Information ?PDI .

```

?ID a FTOnto:OV\_1\_ID;

```
FTOnto:timestamp ?Timestamp ;

sosa:hasSimpleResult ?O_ID ;

FTOnto:is_Information ?Message .

```

?I1 a FTOnto:Position\_Switch\_I1\_Observation;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?I1_POSITION_SWITCH_PRESSED ;

FTOnto:is_Information ?Message .

```

?I2 a FTOnto:Position\_Switch\_I2\_Observation;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?I2_POSITION_SWITCH_PRESSED ;

FTOnto:is_Information ?Message .

```

?LBO a FTOnto:Light\_Barrier\_5\_Observation;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?I5_LIGHT_BARRIER_INTERRUPTED ;

FTOnto:is_Information ?Message .

```

?M1 a FTOnto:Motor\_Speed\_M1\_Observation;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?M1_SPEED ;

FTOnto:is_Information ?Message .

```

?O7 a FTOnto:Valve\_7\_Observation;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?O7_VALVE_OPEN ;

FTOnto:is_Information ?Message .

```

?O8 a FTOnto:Compressor\_Power\_Level\_O8\_Observation;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?O8_COMPRESSOR_POWER_LEVEL ;

FTOnto:is_Information ?Message .

```

?CS a FTOnto:OV\_1\_CURRENT\_STATE;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?CURRENT_STATE ;

FTOnto:is_Information ?Message .

```

?CT a FTOnto:OV\_1\_CURRENT\_TASK;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?CURRENT_TASK ;

FTOnto:is_Information ?Message .

```

?CTESSS a FTOnto:OV\_1\_CURRENT\_TASK\_ELAPSED\_SECONDS\_SINCE\_START;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?CURRENT_TASK_ELAPSED_SECONDS_SINCE_START ;

FTOnto:is_Information ?Message .

```

?CST a FTOnto:OV\_1\_CURRENT\_SUB\_TASK;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?CURRENT_SUB_TASK ;

FTOnto:is_Information ?Message .

```

?BK a FTOnto:OV\_1\_BUSINESS\_KEY;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?BUSINESS_KEY ;

FTOnto:is_Information ?Message .

```

?PDI a FTOnto:OV\_1\_PROCESS\_DEFINITION\_ID;

```
FTOnto:timestamp ?Timestamp; 

sosa:hasSimpleResult ?PROCESS_DEFINITION_ID ;

FTOnto:is_Information ?Message .

```

}

WHERE {

BIND (template("mongodb://localhost:27017/Oven\_Message\_{Oven}") AS ?Message)

BIND (template("mongodb://localhost:27017/Oven\_ID\_{Oven}") AS ?ID)

BIND (template("mongodb://localhost:27017/Oven\_I1\_{Oven}") AS ?I1)

BIND (template("mongodb://localhost:27017/Oven\_I2\_{Oven}") AS ?I2)

BIND (template("mongodb://localhost:27017/Oven\_LBO\_{Oven}") AS ?LBO)

BIND (template("mongodb://localhost:27017/Oven\_M1\_{Oven}") AS ?M1)

BIND (template("mongodb://localhost:27017/Oven\_O7\_{Oven}") AS ?O7)

BIND (template("mongodb://localhost:27017/Oven\_O8\_{Oven}") AS ?O8)

BIND (template("mongodb://localhost:27017/Oven\_CS\_{Oven}") AS ?CS)

BIND (template("mongodb://localhost:27017/Oven\_CT\_{Oven}") AS ?CT)

BIND (template("mongodb://localhost:27017/Oven\_CTESSS\_{Oven}") AS ?CTESSS)

BIND (template("mongodb://localhost:27017/Oven\_CST\_{Oven}") AS ?CST)

BIND (template("mongodb://localhost:27017/Oven\_BK\_{Oven}") AS ?BK)

BIND (template("mongodb://localhost:27017/Oven\_PDI\_{Oven}") AS ?PDI)

}

does anyone know why this "triple jump" does not work ?

Thanks in advance for the help

Greetings  
Dennis

---

<div class="post-metadata">

**Author:** ![PaulJackson](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pauljackson/32/243_2.png) [@PaulJackson](https://community.stardog.com/u/PaulJackson)\
**Post date:** [January 31, 2022, 10:47pm UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494/2 "2022-01-31T22:47:03Z")

</div>

Hi Dennis,

Can you share a text version of the query that's failing and its query plan in text format?

I speculating but it might have to do with the data types of the MondoDB fields you're joining on.

See [Specific Data Source Considerations | Stardog Documentation Latest](https://docs.stardog.com/virtual-graphs/data-sources/specific-data-source-considerations#specifying-mongodb-data-types)

-Paul

---

<div class="post-metadata">

**Author:** ![DennisThiery](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DennisThiery](https://community.stardog.com/u/DennisThiery)\
**Post date:** [February 1, 2022, 4:38pm UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494/3 "2022-02-01T16:38:59Z")

</div>

Hi Paul,  
Thanks for the help,  
Attached is the query plan and the query

[Untitled-7\_sparql\_explanation.txt](https://community.stardog.com/uploads/short-url/rfcYYuaSYKxc0gSMh0KYYJ1zdn7.txt) (61.4 KB)

The Query is

```auto
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sosa: <http://www.w3.org/ns/sosa/>
PREFIX FTOnto: <http://iot.uni-trier.de/FTOnto#>
PREFIX StreamDataAnnotationOnto: <http://iot.uni-trier.de/StreamDataAnnotationOnto#>
Prefix : <http://api.stardog.com/> 

SELECT Distinct ?I ?S ?P ?O ?Data {
    GRAPH <virtual://Oven> {
?I a FTOnto:Kafka_Message_OV1 .
?I FTOnto:timestamp '2021-11-08 14:12:10.44' .
?I FTOnto:has_Information ?S .
?S ?P ?O .
}

```

It would be great if the problem was this

Greetings  
Dennis

---

<div class="post-metadata">

**Author:** ![PaulJackson](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pauljackson/32/243_2.png) [@PaulJackson](https://community.stardog.com/u/PaulJackson)\
**Post date:** [February 1, 2022, 7:58pm UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494/4 "2022-02-01T19:58:32Z")

</div>

Hi Dennis,

What is the data type of the `_id` field in the MongoDB collection? By default MongoDB uses `ObjectID` but that data type is not indicated in your `FROM JSON` clause (Stardog defaults ALL MongoDB fields to `string`):

```json
FROM JSON {
    "Oven":{
        "_id":"?Oven",
        "ID":"?O_ID",
        "TIMESTAMP":"?Timestamp",
...

```

If it is `ObjectId` you'll want to change that to:

```json
FROM JSON {
    "Oven":{
        "_id":"?Oven:objectId",
        "ID":"?O_ID",
        "TIMESTAMP":"?Timestamp",
...

```

If I'm wrong about that or if this change doesn't fix the problem, could you try the same query without the `?s ?p ?o` and resend the plan? Something like:

```sql
SELECT DISTINCT ?I ?S {
    GRAPH <virtual://Oven> {
        ?I a FTOnto:Kafka_Message_OV1 .
        ?I FTOnto:timestamp '2021-11-08 14:12:10.44' .
        ?I FTOnto:has_Information ?S .
    }
}

```

... or whatever is the simplest query with the simplest plan that still exhibits the problem.

Thanks,  
-Paul

---

<div class="post-metadata">

**Author:** ![DennisThiery](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DennisThiery](https://community.stardog.com/u/DennisThiery)\
**Post date:** [February 2, 2022, 11:38am UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494/5 "2022-02-02T11:38:40Z")

</div>

Hello Paul,  
I think we are on the right track.  
The \_id is created as ObjectId, when I want to change it, as in your exampleI, I get always an error message  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/c/cebb1e069e5740737862c861b45f17aff1826d5b.png)

I have tried to bind the variable, but it doesn't really work. I tried to copy the example in your tutorial, but it always complains that only 2 brackets open but 3 brackets close.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/4/4596c7a0325fea3082812649c2856d72ca016151.png)

I then tried to work with the O\_ID and created an individual here, as binding did not work either.  
Now, when I run the Sparql, I get the individual back.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/c/c8af2528585d116f715ce28f83d53ed032f4e188.png)

The question I have now is what am I doing wrong with the binding?

Greetings and thanks  
Dennis

---

<div class="post-metadata">

**Author:** ![PaulJackson](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pauljackson/32/243_2.png) [@PaulJackson](https://community.stardog.com/u/PaulJackson)\
**Post date:** [February 2, 2022, 1:55pm UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494/6 "2022-02-02T13:55:49Z")

</div>

Sorry about the error in the doc. It should be:

```auto
BIND (strDt(?size, xsd:double) AS ?doubleSize)

```

I'm fixing that.

Could you share a text version of the mappings that are causing the Unbound variable Oven error? And also the mappings that are returning the individual in the `?O` column.

Thanks,  
-Paul

---

<div class="post-metadata">

**Author:** ![DennisThiery](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DennisThiery](https://community.stardog.com/u/DennisThiery)\
**Post date:** [February 2, 2022, 2:25pm UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494/7 "2022-02-02T14:25:53Z")

</div>

Hello Paul  
Thank you very much for the effort,

Attached is the mapping, I have put a comment in line 11 and 136 where I have changed something or where the objectId should go.

The last bind is only for mapping the O?  
[Mapping FTOnto Stardog.txt](https://community.stardog.com/uploads/short-url/xNSJge9oVn8bx4jx39V0mhhvsP8.txt) (5.0 KB)

Greetings  
Dennis

---

<div class="post-metadata">

**Author:** ![PaulJackson](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pauljackson/32/243_2.png) [@PaulJackson](https://community.stardog.com/u/PaulJackson)\
**Post date:** [February 2, 2022, 4:46pm UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494/8 "2022-02-02T16:46:52Z")

</div>

The `objectId` should work. Support for adding this type was added in version 7.8.0 of Stardog. Are you using an older version?

To understand why you are getting back that highlighted value for `?O`, consider this portion of your mapping:

```auto
FROM JSON {
  "Oven":{
    "_id":"?Oven:objectId",
    "ID":"?O_ID",
  }
}
TO {
  ?Message FTOnto:has_Information ?ID .
  ?ID sosa:hasSimpleResult ?O_ID .
}
WHERE {
  BIND(template("mongodb://localhost:27017/Oven_Message_{Oven}") AS ?Message)
  BIND(template("mongodb://localhost:27017/Oven_ID_{Oven}") AS ?ID)
  BIND(template("mongodb://localhost:27017/Oven_{Oven}") AS ?O_ID)
}

```

And this portion of your query:

```auto
?I FTOnto:has_Information ?S .
?S ?P ?O

```

The solution with the highlighted result matched `?I` to `?Message`, `?S` to `?ID`, `?P` to `sosa:hasSimpleResult`, and `?O` to `?O_ID`. (Notice that `?I` and `?O` are _NOT_ the same IRI - `?I` has `Oven_Message` in the IRI and `?O` has only `Oven`.

-Paul

---

<div class="post-metadata">

**Author:** ![DennisThiery](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DennisThiery](https://community.stardog.com/u/DennisThiery)\
**Post date:** [February 3, 2022, 9:59am UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494/9 "2022-02-03T09:59:45Z")

</div>

Hello Paul  
thanks for the explanation,  
I have now re-installed Stardog with version 7.8.3 and now everything works.

Thanks for the support. Really great of you guys what you are doing 🙂

Greetings and thanks  
Dennis

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [February 17, 2022, 9:59am UTC](https://community.stardog.com/t/mongodb-virtual-graph-problem-with-sparql/3494/10 "2022-02-17T09:59:56Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
