How do I select only a few fields in the query for a collection with array?

Hello everyone!

In this fictional collection of Customers with arrays:

    [{
      "_id": {
        "$oid": "600f0760679d827471a8240a"
      },
      "is_company": true,
      "company_name": "DI MARMORE INDUSTRIA E COMERCIO LTDA",
      "cnpj": "05391213000105",
      "ie": "675140204112",
      "phones": [
        "1141380000",
        "1141380000",
        "11992690000",
        "11994540000"
      ],
      "address": {
        "street": "RUA SALETE",
        "number": "70",
        "bairro": "JARDIM SALETE",
        "city": "TABOAO DA SERRA",
        "uf": "SP",
        "cep": "06787999"
      },
      "site": "dimarmore.com.br",
      "email": "comer@dimarmore.com.br",
      "nfe_status": true,
      "nfce_status": false,
      "cte_status": false,
      "mdfe_status": false,
      "token": "1-abc123defg4567-algoMaisAqui"
    },{
      "_id": {
        "$oid": "600f0760679d827471a8240b"
      },
      "is_company": false,
      "full_name": "NILTON GONÇALVES MEDEIROS",
      "cpf": "04298414122",
      "rg": "128768792",
      "phones": [
        "1155550108",
        "1195555555"
      ],
      "address": {
        "street": "AV. PREFEITO DONALD",
        "number": "9999",
        "complemento": "ETAPA 3",
        "bairro": "NOVA CAIEIRAS",
        "city": "CAIEIRAS",
        "uf": "SP",
        "cep": "07704999"
      },
      "email": "nilto@sistrome.com.br",
      "nfe_status": false,
      "nfce_status": false,
      "cte_status": true,
      "mdfe_status": true,
      "token": "2-abc123defg4567-algoMaisAqui"
    },{
      "_id": {
        "$oid": "600f0fa9679d827471a8240e"
      },
      "is_company": true,
      "company_name": "SISTROM SISTEMAS WEB LTDA",
      "cnpj": "11000000999999",
      "im": "0009999",
      "phones": [
        "1199999999"
      ],
      "address": {
        "street": "AV. PREFEITO DONALD",
        "number": "9999",
        "complemento": "CASA 1",
        "bairro": "NOVA ORLEANS",
        "city": "CAIEIRAS",
        "uf": "SP",
        "cep": "07704999"
      },
      "site": "sistron.com.br",
      "email": "comer@sistron.com.br",
      "issuers": [
        {
          "company_name": "ALEXPRESS AGENCIAMENTO AEREO LTDA",
          "cnpj": "59999999999999",
          "ie": "111799999999",
          "phones": [
            "1155555555",
            "1155555555"
          ],
          "address": {
            "street": "AV. CUPECÊ",
            "number": "9999",
            "bairro": "JARDIM CUPECÊ",
            "city": "SÃO PAULO",
            "uf": "SP",
            "cep": "04365000"
          },
          "site": "alexpress.com.br",
          "email": "finan@alexpress.com.br",
          "nfe_status": false,
          "nfce_status": false,
          "cte_status": true,
          "mdfe_status": true,
          "token": "3-abc123defg4567-algoMaisAqui"
        },
        {
          "company_name": "LW LOGÍSTICA E TRANSPORTES LTDA ",
          "cnpj": "13599999999999",
          "ie": "146099999999",
          "phones": [
            "1155555555",
            "1155555555"
          ],
          "address": {
            "street": "RUA RANULFO PRATA",
            "number": "9999",
            "bairro": "CIDADE ADEMAR",
            "city": "SÃO PAULO",
            "uf": "SP",
            "cep": "04389999"
          },
          "email": "operacional@lwcargas.com.br",
          "nfe_status": false,
          "nfce_status": false,
          "cte_status": true,
          "mdfe_status": true,
          "token": "4-abc123defg4567-algoMaisAqui"
        }
      ]
    }]

How do I select certain fields inside or outside the array? See, to refer to the token “3-abc123defg4567-somethingHere” that may be inside or outside the array I use the following syntax in MongoDB Compass:

{$or: [{token: “3-abc123defg4567-algoMaisHere” }, {“issuees.token”: “3-abc123defg4567-algoMaisHere”}]}

My question is: How to select some fields using {"company_name":1, "issuers.company_name":1, "full_name": 1, ...}

I tried to put this option in several ways but still returned all fields of the document or returns nothing.

Hello @Nilton_Medeiros, welcome to the MongoDB Community forum.

You can use projection to select specific fields (or exclude some fields). In MongoDB Compass, in the Documents tab, click the Filter Options. The Project area is where you can specify what fields you want in the output document. For example, you can specify {"company_name":1, "issuers.company_name":1, "full_name": 1, ... }. Use 1 to include and 0 to exclude fields.

See Compass documentation topic for details: Set which fields are returned.

1 Like

Thanks! This is fantastic!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.