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

532
Vistas
¿Cómo arreglar $sort no funciona para la matriz dentro de la matriz en mongoDB?

Mi documento es el siguiente:

 { "_id" : ObjectId("622136811b68d9136e48ba4e"), "brand_name" : "iPhone", "brand_rating" : [ { "cust_name" : "Sham K", "rating" : 5 }, { "cust_name" : "Nil", "rating" : 5 } ], "models" : [ { "model_name" : "iPhone 7Plus", "RAM" : "4GB", "ROM" : "64GB", "price" : 98000, "buyer" : [ { "cust_name" : "Anu", "rating" : 3 }, { "cust_name" : "Kiran", "rating" : 4 } ] }, { "model_name" : "iPhone 2", "RAM" : "3GB", "ROM" : "32GB", "price" : 58000, "buyer" : [ { "cust_name" : "Kiran", "rating" : 4 } ] } ] }

La pregunta es Enumere todos los clientes en orden descendente que compraron el iPhone 7plus.

Intento esto pero la clasificación no funciona

db.brand.aggregate({$unwind : "$models"},{$match : {"models.model_name" :"iPhone 7Plus" }}, {$project : {_id : 0, "models.buyer.cust_name" : 1}}, {$ordenar: {"modelos.comprador.nombre_cliente": -1} })

Producción :

 { "models" : { "buyer" : [ { "cust_name" : "Anu" }, { "cust_name" : "Kiran" } ] } } > [5]

¿Cuál es la forma más fácil de resolverlo?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

¿Qué tal $unwind antes $sort

 db.collection.aggregate({ $unwind: "$models" }, { $match: { "models.model_name": "iPhone 7Plus" } }, { $project: { _id: 0, "models.buyer.cust_name": 1 } }, { $unwind: "$models.buyer" }, { $sort: { "models.buyer.cust_name": -1 } })

parque infantil mongo

over 4 years ago · Santiago Trujillo Denunciar

0

Aquí hay una variación con solo 3 etapas y el solo $unwind se empuja "hacia abajo" en la canalización lo más lejos posible para minimizar tanto la cantidad de documentos como el tamaño de los mismos. La clave en esta variación es usar $filter seguido de una expresión de notación de puntos para llegar a los datos que queremos sin usar $unwind demasiado pronto.

 db.foo.aggregate([ {$project: { _id:false, cust_name: // Explaining this "inside out": // 1. Start by filtering the models array for iphone 7plus. // This will yield an array of 1 item (or zero). // 2. We want to get the cust_name from buyer array. Use // $map on the array of 1 to extract the names using the // dot notation trick buyer.cust_name to create an array // of just names. // 3. We have that list -- but it is a list inside the array // of one, eg [ [ name1, name2 ] ] . So we lift out // the inner array with $arrayElemAt: [ inner, 0 ]. // Note: v>=4.4 you can use $first: { inner } as a convenient // substitute for $arrayElemAt: [ inner, 0 ] {$arrayElemAt: [ {$map: { input: {$filter: { input: "$models", cond: {$eq:['$$this.model_name','iPhone 7Plus']} }}, in: "$$this.buyer.cust_name" }}, 0] } }} // At this stage we have: // { "cust_name" : [ "Anu", "Kiran" ] } // Now it's easy: ,{$unwind: '$cust_name'} ,{$sort: {'cust_name':-1}} ]);
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