Mongodb 查询与字符串字段中的单词匹配的对象数组

Mongodb 查询与字符串字段中的单词匹配的对象数组,mongodb,mongodb-query,Mongodb,Mongodb Query,在使用Jena对大型本体执行查询之后,我将结果以JSON格式导出到名为galileo的数据库中名为items的MongoDB集合中。 现在我想查询集合,以按名称查找项目(名称位于标题字段),特别是我想搜索“Astrolabio”,我可以检索标题字段中包含单词“Astrolabio”的所有对象(例如,“Astrolabio”、“Astrolabio Piano”等)。我感兴趣的对象包含在@graph数组中。 我试过了 但是它会返回很多不包含搜索词的对象。 我也试过了 db.items.find(

在使用Jena对大型本体执行查询之后,我将结果以JSON格式导出到名为galileo的数据库中名为items的MongoDB集合中。
现在我想查询集合,以按名称查找项目(名称位于标题字段),特别是我想搜索“Astrolabio”,我可以检索标题字段中包含单词“Astrolabio”的所有对象(例如,“Astrolabio”、“Astrolabio Piano”等)。我感兴趣的对象包含在@graph数组中。
我试过了

但是它会返回很多不包含搜索词的对象。

我也试过了

db.items.find({},{"@graph":{$elemMatch:{"title":{$regex: /Astrolabio$/}}}})
但是,正如我在尝试之后才发现的那样,它只返回与请求匹配的第一个对象。
那么,对于我要做的事情,正确的查询是什么呢?
为了提供帮助,这里有一小部分文档

{
"_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"
    }, 

如果结果中只需要@graph数组中与查询匹配的元素(如果标题包含单词Astrolabio),则可以通过以下聚合框架查询实现:

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

您的正则表达式
{$regex:/Astrolabio$/}
将只返回标题为“Astrolabio”的文档,因为最后一个单词是一个句子(“Astrolabio Piano”将不包括在内,因为“Piano”是这里的最后一个单词)。

谢谢您的回答,我尝试了您建议的两个查询,但不幸的是,它们都没有提供我想要的效果。在第一次继续显示整个文档时,我还尝试了一个变体
db.items.find({},{@graph.title:/Astrolabio/})
,它返回数组的所有元素,只打印title字段。相反,第二个查询没有给出任何结果。@HJuls2,请为上述文档编写所需的输出(使用
“\u id”:ObjectId(“59e07632b5d295462b330c4c”)
)。也许我误解了你的问题。您是否只需要输出中的title字段(或带有_id字段)?我需要@graph数组中与查询匹配的所有元素的所有字段。Istead我不希望只匹配第一个元素,或者,另一方面,如果元素中存在匹配项,则匹配数组的所有元素。例如,假设在数组中有上面的两个元素加上另一个元素,它有自己的字段和标题字段“望远镜”,结果必须包含星盘和星盘钢琴的所有字段,但不是望远镜的字段。好的,我明白你的意思了。我更改了答案,请再试一次。从mongo shell开始,它对我很有效。前面您提到过“第二个查询不会给出任何结果”。如果您在更新的查询中遇到类似的问题,请尝试将其转换为单行,多行查询可能会出现问题。
db.items.aggregate([     
    {$match: {"@graph.title": /Astrolabio/}},      
    {$unwind: "$@graph"},      
    {$match: {"@graph.title": /Astrolabio/} }, 
    {$group: {"_id": "$_id", "@graph": {"$push": "$@graph" } }}  
]);