0

My MongoDB data structure looks like this:

    {
        "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."
             }
         ]
    }

I want to request the article-id_pmid and the text in the same subdocument, also the text of subdocument which contains in infons a field with section_type : ABSTRACT and type: abstract.
I had tried this request but the result was not what I am searching for:

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})
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Ramzi Bouchair
  • 915
  • 5
  • 7
  • `passeges` is an array so doing `passages.infons...` won't work. Will there always be just 2 elements in the `passages` array? Meaning will there always be just 2 `infons` subdocs in it? – codemonkey Feb 12 '21 at 21:04
  • 1
    No, ```passages``` could have more than 2 elements in the same time, and not all ```infons``` contain the ```article-id_pmid``` and that's why i am trying to filter them – Ramzi Bouchair Feb 12 '21 at 22:18

1 Answers1

1

Each of the top-level conditions is treated independently.

Use https://docs.mongodb.com/manual/reference/operator/query/elemMatch/ to specify multiple conditions on the same array element.

D. SM
  • 13,584
  • 3
  • 12
  • 21
  • ```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} )``` I had tried this it work thanks but the new problem is how to show only the ```article-id_pmid``` and the text of the other subDocument ```infons``` with the type ```abstract``` !!! – Ramzi Bouchair Feb 13 '21 at 11:45