【问题标题】:MongoDB search multiple Field in multiple subDocumentMongoDB在多个子文档中搜索多个字段
【发布时间】:2021-05-16 14:07:54
【问题描述】:

我的 MongoDB 数据结构如下所示:

    {
        "id": "7118592",
        "passages": [
             {
                 "infons": {
                     "article-id_pmid": "32292259",
                     "title": "Keywords",
                     "type": "front",
                     "section_type": "TITLE"
                  },
                  "text": "Understanding of guidance for acupuncture and moxibustion interventions on COVID-19 (Second edition) issued by CAAM"
             },
             {
                 "infons": {
                     "section_type": "ABSTRACT",
                     "type": "abstract",
                     "section": "Abstract"
                 },
                 "offset": 116,
                 "text": "At present, the situation of global fight against COVID-19 is serious. WHO (World Health Organization)-China Joint Mission fully confirms the success of \"China's model\" against COVID-19 in the report. In fact, one particular power in \"China's model\" is acupuncture and moxibustion of traditional Chinese medicine. To better apply \"non-pharmaceutic measures\":the external technique of traditional Chinese medicine, in the article, the main content of Guidance for acupuncture and moxibustion interventions on COVID-19 (Second edition) issued by China Association of Acupuncture-Moxibution is introduced and the discussion is stressed on the selection of moxibustion device and the duration of its exertion."
             }
         ]
    }

我想在同一个子文档中请求article-id_pmidtext,以及在infons 中包含section_type : ABSTRACTtype: abstract 的字段的子文档的text
我已经尝试过这个请求,但结果不是我正在寻找的:

db.mydata.find({$and:[{"passages.infons.article-id_pmid$":{$exists: true}},{"passages.infons.section_type":"ABSTRACT"},{"passages.infons.type":"abstract"}]},{"passages.infons.article-id_pmid.$":1,_id:0, "passages.text":1})

【问题讨论】:

  • passeges 是一个数组,所以 passages.infons... 是行不通的。 passages 数组中是否总是只有 2 个元素?意思是永远只有 2 个infons 子文档吗?
  • 不,passages 可以同时拥有超过 2 个元素,并且并非所有 infons 都包含 article-id_pmid,这就是我尝试过滤它们的原因

标签: mongodb nosql bigdata projection subdocument


【解决方案1】:

每个顶级条件都被独立处理。

使用https://docs.mongodb.com/manual/reference/operator/query/elemMatch/ 对同一数组元素指定多个条件。

【讨论】:

  • db.mydata.find( { $and: [ {"passages" : { "$elemMatch" : { "infons.article-id_pmid": {"$exists": true}}}}, {"passages" : { "$elemMatch" : { "infons.section_type":"ABSTRACT", "infons.type": "abstract"}}} ]}, {"passages.infons.article-id_pmid":1} ) 我试过了,谢谢,但新问题是如何只显示article-id_pmid 和其他子文档infons 的文本,类型为abstract !!!
猜你喜欢
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2022-11-28
  • 2018-07-13
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
相关资源
最近更新 更多