Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:38:23+00:00 2026-05-30T12:38:23+00:00

In the process of learning to use Mongo and PHP, and got stuck with

  • 0

In the process of learning to use Mongo and PHP, and got stuck with this.
I have a collection in Mongo with the following structure:

array(3){
["timehack"]=>int(..),  
["_id"]=>object..,  
["series"]=>  
array(3){  
    [0]=>int(..),  
    [1]=>int(..),  
    [2]=>int(..)  
}  
}  

I am trying to query the collection so i get all of the samples with first item in “series”, something like:

$cursor = $collection->find();
$cursor->fields(array=>("timehack"=>true, "_id"=>false, "series.0"=>true));
$arr = $cursor->getNext();
var_dump($arr);

The resulting series array is empty. How can I get just the desired index? (I realize that I can get all samples in the series then filter with code, but I would like to know how to accomplish this with a query). Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:38:24+00:00Added an answer on May 30, 2026 at 12:38 pm

    In the comments above you have already confirmed that this works with the $slice operator in the shell. So, to give you an example of how $slice works in PHP, I refer you to none other than the PHP tests in the driver itself.

    Take a look at line 33 in the tests/MongoCollectionTest2.php file on github:

    https://github.com/mongodb/mongo-php-driver/blob/master/tests/MongoCollectionTest2.php

    The code goes like this:

    $m = new Mongo();
    $db = new MongoDB($m, "phpunit");
    $this->object = $db->selectCollection('c');
    ...
    ...
    $results = $this->object->find(array(),array("x" => array('$slice' => 3)))->getNext();
    

    It looks a little convoluted compared to the shell with a lot of casting as an object going on (if I am reading that right), but hopefully enough to get you started 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im the process of learning C++. I got stuck debugging some of my code
I am in the process of learning the Yii Framework. I have been following
I'm in the process of learning to use OOP in php, I'm having a
Right now, I am in the process of learning PHP and I have some
I am in the process of learning how to use SWIG on Windows. The
On the process of learning Assembly i got one question If i do the
I'm in the process of learning Databases and SQL. From what I have read
I'm in the process of learning jQuery and the tutorial I'm following uses Dreamweaver
In the process of learning P/Invoke, I asked this previous question: How to P/Invoke
I'm in the process of learning TFS2010 Team Build, Workflow and MSBuild. I've got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.