$userDoc = $collection->findOne(array('userID' => $id)); //$userMas = $userDoc['userMas']; $userMas10 = ? 

Suppose we have an array of 1 million elements, so as not to clog resources, we need to take not the entire array from the database, but the array of the first 10 elements so that 1 million is not pulled out of the database.

  • You have a condition findOne , how can you get an array of 1 million elements? - Egor Smolyakov Nov.
  • findOne finds a document by id, this document has a field array of 'userMas' of 1 million elements. It turns out like this $ userMas = $ userDoc ['userMas']; Only I do not need to receive it all, but only an array of its first 10 elements. - Gennady
  • Edited your answer - Egor Smolyakov

1 answer 1

You can do this as follows:

Mongo Shell:

 > db.testdata.find() { "_id" : ObjectId("583c12b16495812685144ed5"), "mas" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] } > db.testdata.findOne( { "_id": ObjectId("583c12b16495812685144ed5") } , { mas: { $slice: 5 } } ) { "_id" : ObjectId("583c12b16495812685144ed5"), "mas" : [ 1, 2, 3, 4, 5 ] } 

In the code:

 $userDoc = $collection->findOne(array('userID' => $id)); $userMas = $collection->findOne(array('userID' => $id), array('userMas' => array( '$slice' => 10 ) )); 
  • Thank! Corrected your question. - Gennady