Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

82
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda