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

333
Vistas
Getting the size of arraylist from a MongoDB table

I am trying to get the count of followers from user table which is basically a array list using java programming.

I am using below query to get the count using command line interface.

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

But I am not able to create the same query in java as I am new. Below is the code I wrote in java and I am getting com.mongodb.MongoCommandException: Command failed with error 17124: 'The argument to $size must be an array, but was of type: 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", "[]")))))));

Can Anyone please help me with this

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

0

You can try something like with Java 8 and Mongo Driver 3.x version. You should try not to use old type ( BasicDbObject) and where possible use api method.

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 Denunciar

0

I tried this and it worked fine.

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

Thanks

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