Good morning, I am learning MongoDB but when trying to make a query I do not achieve the desired results.
The structure of the documents used is as follows:
use empresa
db.departamento_dos.insert (
{
"Codigo": 2,
"Nombre": "Contabilidad",
"Presupuesto": 10000,
"Empleados":
{
"Empleado": { "DNI": "10340", "Nombre": "Andres", "Apellido": "Lopez"},
"Empleado": { "DNI": "10341", "Nombre": "Paula", "Apellido": "Tovar"},
"Empleado": { "DNI": "10342", "Nombre": "Andres", "Apellido": "Perez"},
"Empleado": { "DNI": "10343", "Nombre": "Camila", "Apellido": "Perez"},
"Empleado": { "DNI": "10344", "Nombre": "Cristian", "Apellido": "Forero"}
}
}
)
and
db.departamento_tres.insert (
{
"Codigo": 77,
"Nombre": "Marketing",
"Presupuesto": 30000,
"Empleados":
{
"Empleado uno": { "DNI": "10350", "Nombre": "Maria", "Apellido": "Perez"},
"Empleado dos": { "DNI": "10351", "Nombre": "Angie", "Apellido": "Gómez"},
"Empleado tres": { "DNI": "10352", "Nombre": "Andrea", "Apellido": "Lopez"},
"Empleado cuatro": { "DNI": "10353", "Nombre": "Cristina", "Apellido": "Riaño"},
"Empleado cinco": { "DNI": "10354", "Nombre": "Annie", "Apellido": "Castellanos"}
}
}
)
My question is: how can I make a query by "Apellido" by eliminating repeated "Apellidos"?
I tried to use the different operator with a regular expression to auto-complete the query, but not work.
db.departamento_uno.distinct( "Empleados.Empleado", { /.*".Apellido"} )
and it always shows me an empty vector.
The idea is to be able to consult the two documents simultaneously. For example, the result I'm looking for would be:
[ "Lopez", "Tovar", "Perez", "Forero", "Gómez", "Riaño", "Castellanos"]
since the idea is to show each surname only once no matter how many times it appears
Any idea how you can make this query?
Thanks for the help.