# How to create MongoDB Virtual Graph mapping for SQL equivalent "WHERE" & "LIMIT" clause

**URL:** <https://community.stardog.com/t/how-to-create-mongodb-virtual-graph-mapping-for-sql-equivalent-where-limit-clause/4671>\
**Category:** Stardog Studio\
**Created:** [October 18, 2023, 10:27am UTC](https://community.stardog.com/t/how-to-create-mongodb-virtual-graph-mapping-for-sql-equivalent-where-limit-clause/4671 "2023-10-18T10:27:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashish\_Pal](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/ashish_pal/32/1658_2.png) [@Ashish\_Pal](https://community.stardog.com/u/Ashish_Pal)\
**Post date:** [October 18, 2023, 10:27am UTC](https://community.stardog.com/t/how-to-create-mongodb-virtual-graph-mapping-for-sql-equivalent-where-limit-clause/4671/1 "2023-10-18T10:27:08Z")

</div>

Hello Stardog Team,

I would need help in writing a MongoDB JSON virtual graph mapping for the corresponding SQL virtual mapping as shared below, with the focus on replicating the "WHERE" and "LIMIT" clauses in MongoDB mapping syntax. The requirement from this kind of mapping is that we pick a specific subset of data from our MongoDB data source based on the filters and then use it to enrich our knowledge graph -

```auto
MAPPING :AlbumMapping
FROM SQL {
  SELECT * 
  FROM Album
  **WHERE** artist = 'John_Lennon'
  **LIMIT** 5
}
TO {
  ?album a :Album ;
           :name ?name ;
           :artist ?artist ;
           :date ?release_date 
} 
WHERE {
  BIND(template("http://stardog.com/tutorial/Album{id}") AS ?album)
  BIND(template("http://stardog.com/tutorial/Artist{artist}") AS ?artist)
}

```

Thanks & Regards,  
Ashish

---

<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:** [October 18, 2023, 3:34pm UTC](https://community.stardog.com/t/how-to-create-mongodb-virtual-graph-mapping-for-sql-equivalent-where-limit-clause/4671/2 "2023-10-18T15:34:23Z")

</div>

Hi Ashish,

Mapping to MongoDB does not support any kind of WHERE or LIMIT clauses. Each mapping must have a `FROM JSON` clause where you describe the schema of a single collection. Multiple mappings can be included in the same SMS file, separated by semicolons. Each mapping is one-to-one - any filtering or limits must be applied in your SPARQL query. See [here](https://docs.stardog.com/virtual-graphs/data-sources/specific-data-source-considerations#example-mapping) for an example mapping. If you have some standard filters that you always want to apply, you might consider creating a [stored query](https://docs.stardog.com/operating-stardog/database-administration/stored-queries) and using it like a [stored procedure](https://docs.stardog.com/query-stardog/stored-query-service).

-Paul

---

<div class="post-metadata">

**Author:** ![Ashish\_Pal](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/ashish_pal/32/1658_2.png) [@Ashish\_Pal](https://community.stardog.com/u/Ashish_Pal)\
**Post date:** [October 19, 2023, 3:52am UTC](https://community.stardog.com/t/how-to-create-mongodb-virtual-graph-mapping-for-sql-equivalent-where-limit-clause/4671/3 "2023-10-19T03:52:55Z")

</div>

Thank you Paul for your answer. I have a few follow-up questions on this;

Currently, I'm materializing the virtual graph into stardog database using the below SPARQL update query in Workspace -  
`ADD <virtual://mongodb_vg> TO <mongodb:materialized_named_graph>`

My question is - using the solution you provided in the last reply, can I use that filters applied SPARQL query while materializing that virtual graph? In short, is it possible to use stored query along with [sparql update queries](https://docs.stardog.com/external-compute/vg-materialization-external-compute#sparql-update-queries)?  
The requirement is to materialize a specific subset of documents from our MongoDB collection.

Looking forward to hearing from you. Thank you

Regards,  
Ashish

---

<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:** [October 19, 2023, 2:27pm UTC](https://community.stardog.com/t/how-to-create-mongodb-virtual-graph-mapping-for-sql-equivalent-where-limit-clause/4671/4 "2023-10-19T14:27:09Z")

</div>

Yes, your query is a shortcut for this more general query:

```auto
INSERT {
  GRAPH <mongodb:materialized_named_graph> {
    ?s ?p ?o
  }
}
WHERE {
  GRAPH <virtual://mongodb_vg> {
    # Whatever query you like (though the vars here must match the vars used above)
    ?s ?p ?o
    # FILTER (?o = "for example, a filter like this")
  }
}

```

-Paul
