0

After executing a query on a huge ontology using Jena, I exported the results in JSON format in a MongoDB collection named items in a database named galileo.
Now I want to query on the collection to find items by their names (names are in the title field), in particular I want that searching for "Astrolabio", I can retrieve all the objects that contain the word "Astrolabio" in the title field (e.g. "Astrolabio", "Astrolabio Piano" etc...).The objects that interest me are contained in the @graph array.
I tried

db.items.find({"@graph":{$elemMatch:{"title":{$regex: /Astrolabio$/}}}})

but it returns lots of objects that don't contain the searched word too.

I tried also

db.items.find({},{"@graph":{$elemMatch:{"title":{$regex: /Astrolabio$/}}}})

but, as I discovered only after my try, it returns only the first object that match the request.
So what's the correct query for what I'm trying to do?
In order to provide an help, here there is a little slice of the document

{
"_id" : ObjectId("59e07632b5d295462b330c4c"),
"@graph" : [ 
    {
        "@id" : "http://minerva.atcult.it/rdf/000000016001",
        "@type" : [ 
            "bibo:Book", 
            "bibo:MultiVolumeBook"
        ],
        "P1053" : [ 
            "1 astrolabe", 
            "1 astrolabio"
        ],
        "abstract" : [ 
            "This astrolabe presently comprises two tympanums, for latitudes 30° and 33°, the other for latitudes 36° and 42° (corresponding to the regions between Persia and the Black Sea). There is an alidade, a rule, and a rete. The back carries a double shadow square and the zodiacal calendar. The instrument comes with a tooled black leather case (cover missing) containing a sixteenth-century manuscript note stating that the astrolabe was brought from Spain and dates from 1252. The astronomical data inscribed on the astrolabe suggest it may have been built before 1000. According to tradition, the instrument dates from the period of Charlemagne (9th C. ). A very similar Arab astrolabe is documented in a drawing by Antonio da Sangallo il Giovane [the Younger] (c.  1520?) at the Gabinetto dei Disegni e delle Stampe (Department of Drawings and Prints) of the Uffizi. Provenance: Medici collections", 
            "Questo astrolabio contiene attualmente due timpani, uno per le latitudini 30° e 33°, e l'altro 36° e 42° (corrispondenti alle regioni comprese tra la Persia e il Mar Nero). È completo di alidada, di regolo e di rete. Nel dorso presenta un doppio quadrato delle ombre e il calendario zodiacale. Lo strumento, proveniente dalle collezioni medicee, è completo di custodia di pelle nera lavorata (coperchio mancante) che porta all'interno una nota manoscritta del XVI secolo nella quale si ricorda che l'astrolabio fu portato dalla Spagna e che risale al 1252. I dati astronomici riportati sullo strumento suggeriscono di anticiparne la costruzione a prima del 1000. Secondo la tradizione si tratterebbe di uno strumento del tempo di Carlo Magno (IX secolo). Un astrolabio arabo molto simile a questo è documentato in un disegno di Antonio da Sangallo il Giovane (c. 1520?) conservato presso il Gabinetto dei Disegni e delle Stampe degli Uffizi. Proviene dalle collezioni medicee"
        ],
        "contributor" : "http://minerva.atcult.it/rdf/ed494c3a-2ba6-3464-b34a-a57e4f70c5e0",
        "creator" : [ 
            "http://minerva.atcult.it/rdf/d481cbac-209b-3741-bba4-906590d805b3", 
            "http://minerva.atcult.it/rdf/36e6efa2-6c8f-350e-ae37-479dade48850", 
            "http://minerva.atcult.it/rdf/47734211-2637-3895-a690-4f33412931ec"
        ],
        "identifier" : "000000016001",
        "issued" : "sec. X",
        "publisher" : "http://minerva.atcult.it/rdf/90310a84-1133-3356-bb3b-647ae1a7d14d",
        "title" : "Astrolabio piano",
        "numPages" : [ 
            "1 astrolabio", 
            "1 astrolabe"
        ],
        "placeOfPublication" : "Fattura araba",
        "label" : "Astrolabio piano"
    }, 
    {
        "@id" : "http://minerva.atcult.it/rdf/000000016002",
        "@type" : [ 
            "bibo:MultiVolumeBook", 
            "bibo:Book"
        ],
        "P1053" : [ 
            "1 astrolabe", 
            "1 astrolabio"
        ],
        "abstract" : [ 
            "Questo piccolo astrolabio contiene quattro timpani per le latitudini 24° e 30°, 31° e 35°, 32° e 36° (corrispondenti alla Persia) e per le latitudini 0° (cioè il circolo dell'equatore) e 66°. È completo di alidada e di rete. Il dorso della madre presenta il calendario lunare, secondo l'uso islamico, un quadrato delle ombre e un quadrante. Lo strumento reca la data 496 dell'Egira (1102-1103 dell'età Cristiana) ed è firmato dal suo artefice, Muhammad 'Ibn Abi'l Qasim 'Ibn Bakran, del quale non si hanno notizie. Fu donato al Museo di Storia della Scienza dal Principe fiorentino Tommaso Corsini", 
            "This small astrolabe carries four tympanums for latitudes 24°/30°, 31°/35°, and 32°/36° (corresponding to Persia), and for latitude 0° (i. e. , the circle of the equator) and 66°. There is an alidade and a rete. The back of the mater displays a lunar calendar, in accordance with Islamic use, a shadow square, and a quadrant. The instrument is dated 496 of the Hegira (1102-1103 of the Christian era) and is signed by its maker, Muhammad 'Ibn Abi'l Qasim 'Ibn Bakran, on whom we have no information. Donated to the Museo di Storia della Scienza by the Florentine Prince Tommaso Corsini"
        ],
        "creator" : [ 
            "http://minerva.atcult.it/rdf/c5738e64-fb77-354a-8fc8-47164105b5f7", 
            "http://minerva.atcult.it/rdf/3fa79916-cb7f-3574-a3fb-589ca42ebf17"
        ],
        "identifier" : "000000016002",
        "issued" : "1102-1103",
        "publisher" : "http://minerva.atcult.it/rdf/90310a84-1133-3356-bb3b-647ae1a7d14d",
        "title" : "Astrolabio piano",
        "numPages" : [ 
            "1 astrolabio", 
            "1 astrolabe"
        ],
        "placeOfPublication" : "Fattura araba",
        "label" : "Astrolabio piano"
    }, 

1 Answer 1

1

if you need to have in a result only elements of the @graph array that match the query (if title contains word Astrolabio), you can reach that with the following aggregation framework query:

db.items.aggregate([     
    {$match: {"@graph.title": /Astrolabio/}},      
    {$unwind: "$@graph"},      
    {$match: {"@graph.title": /Astrolabio/} }, 
    {$group: {"_id": "$_id", "@graph": {"$push": "$@graph" } }}  
]);

your regex {$regex: /Astrolabio$/} will return only documents with titles that have 'Astrolabio' as the last word is a sentence ("Astrolabio Piano" will not be included as 'Piano' is the last word here).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer, I tried both the queries you suggested, but unfortunatly none of them provide the effect I want. The first continue to show the whole document, I also tried a variation on it db.items.find({},{"@graph.title": /Astrolabio/ }) that returns all elements of the array printing only the title field. Instead, the second query doesn't give any result.
@HJuls2, please write the desired output for document described above (with "_id" : ObjectId("59e07632b5d295462b330c4c")). maybe I misunderstand your question. do you need only title field in the output (or with _id field)?
I need all the fields of all the elements of the @graph array that match the query. Istead I don't want only the first element matched or , on the other hand, all the elements of the array if there is a match in the element. So, for example, supposing in the array there are the two elements above plus another element with his own fields and title field "Telescopio", the results must contain all the fields of Astrolabio and Astrolabio piano, but not the fields of Telescopio.
ok, I got your point. I changed my answer, please try again. it works fine for me from mongo shell. previously you mentioned "the second query doesn't give any result." if you will have similar problems with updated query, try to transform it into single line, maybe something broken for multiline query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.