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

285
Views
¿Cómo establecer limit() en MongoQuery usando PHP?

Tengo que usar Mongodb con php e intenté obtener datos de mongocollection usando php. la siguiente mongoquery con el registro de retorno de php con éxito. pero quiero establecer un límite para la siguiente consulta.

Código PHP:

 $query = array("\$and"=>array(array('fld'=> array("\$in"=> array('4', '14', '20'))), array('stat'=>array("\$eq"=>"A")))); $cursor = $this->collection->find($query);

también he intentado de la siguiente manera

 $query = array("\$and"=>array(array('fld'=> array("\$in"=> array('4', '14', '20'))), array('stat'=>array("\$eq"=>"A")))).limit(2);

Pero tengo un error fatal

Llamada al límite de función indefinido ()

¿Cómo usar limit() en la consulta anterior?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

La nueva biblioteca PHP MongoDB no tiene los métodos limit() y sort() en los cursores como tenía la antigua biblioteca PHP Mongo. Ahora deben especificarse en el momento de la consulta, por ejemplo:

 $collection->find($query, [ 'limit' => 10, 'sort' => [ 'date' => -1 ], 'projection' => [ '_id' => 1, 'description' => 1, ] ];
over 4 years ago · Santiago Trujillo Report

0

En PHP, limit() es un método de la clase MongoCursor y no de una matriz. Primero debe obtener un cursor y luego llamar a su método limit() :

 $collection->find($query)->limit(2);

También puede agregar una matriz de opciones a su llamada find() con el parámetro de limit :

 $collection->find($query, ['limit' => 2]);
over 4 years ago · Santiago Trujillo Report

0

Si está utilizando el controlador MongoDB con la biblioteca MongoDB PHP , entonces

 require 'vendor/autoload.php'; $client = new MongoDB\Client("mongodb://localhost:27017"); //SELECT * FROM YOUR_TABLE_NAME LIMIT 10 //db.YOUR_COLLECTION_NAME.find({}).limit(10); $result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array(),array('limit'=>10));
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!