Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

331
Views
Obtener el tamaño de la lista de arreglos de una tabla MongoDB

Estoy tratando de obtener el recuento de seguidores de la tabla de usuarios, que es básicamente una lista de matrices usando la programación de Java.

Estoy usando la siguiente consulta para obtener el conteo usando la interfaz de línea de comandos.

 db.userinfo.aggregate([{ $project : {followersCount : {$size: "$followers"}}}])

Pero no puedo crear la misma consulta en Java porque soy nuevo. A continuación se muestra el código que escribí en Java y obtengo com.mongodb.MongoCommandException: el comando falló con el error 17124: 'El argumento para $ tamaño debe ser una matriz, pero era del tipo: int' Error.

 AggregateIterable<Document> elements = collectionUserInfo.aggregate( Arrays.asList(new BasicDBObject("$project", new BasicDBObject("followers", new BasicDBObject("$size", new BasicDBObject("followers",1).put("followers",new BasicDBObject("$ifNull", "[]")))))));

¿Alguien puede ayudarme con esto?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede probar algo como con la versión Java 8 y Mongo Driver 3.x. Debe intentar no usar el tipo antiguo ( BasicDbObject ) y, cuando sea posible, usar el método api.

 List<Integer> followersCount = collectionUserInfo.aggregate( Arrays.asList(Aggregates.project(Projections.computed( "followersCount", Projections.computed("$size", "$followers")) ) )) .map(follower -> follower.getInteger("followersCount")) .into(new ArrayList<>());
over 4 years ago · Santiago Trujillo Report

0

Intenté esto y funcionó bien.

 AggregateIterable<Document> elements = collectionUserInfo.aggregate( Arrays.asList(new BasicDBObject("$project", new BasicDBObject("followers", new BasicDBObject("$size", "$followers")))));

Gracias

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!