Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
search in mongodb embedded records

We have a mongodb document as given below, and we configured text index on messageTopic, messageTopicQuestion and answer fields, if i search with a text string then I expect only matched embedded records in the results not the entire document.

For example in below document if i search with word "private", then results should only return the first embedded document not both the records. How to retrieve only matched embedded documents and exclude unmatched ones.

{
  "_id": ObjectId("586e8efdde81e56032000084"),
"messageTopic": "My Private",
"messageText": [{
    "messageTopicQuestion": "agent private",
    "answer": "agent private",
    "_id": ObjectId("586e8efdde81e56032000085"),
    "keywords": ["private"]
}, {
    "messageTopicQuestion": "Greetings Checking",
    "answer": "Heloo I am good What about u",
    "_id": ObjectId("586fc80ccced739407000f4e"),
    "keywords": ["Hi-Good", "Heloo"]
}],
"__v": 3

}

I am using below script

      db.getCollection('messagetemplates').aggregate([{
             $match: {

                 $text: {$search: 'private'},
                 visible: 'PUB'
             }
         },{ $sort: { score: { $meta: "textScore" } } }])

Appreciate help. Thanks.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I believe the question is a variation of this problem How to get a specific embedded document inside a MongoDB collection? The issue is how to get the single embedded document and exclude the rest. My suggestion is to use db.collection.find() instead of aggregation. Something in that sense

     db.collection.find({ 'messageText.keyword': 'private' }, {'messageText.$': 1});

, as indicated by the answer above.

messageText.keyword can be replaced with whichever field you want to be searched.

I can confirm that the scenario works on my database.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda