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

I'm in the process of learning WPF coming from WinForms development. I have a
In the process of learning P/Invoke, I asked this previous question: How to P/Invoke
I have the following makefile that I use to build a program (a kernel,
I'm learning to use Grails and have run into a situation I don't understand
I'm in the process of learning LINQ and would like some help the following
im learning php and to keep it simple (i think), this is what im
Imagine that I have the following two methods: void AddEvenNumbers() { for(int i=0; i<10000000;
I have been learning how to use the Python multiprocessing module recently, and reading
I believe in learning along the process and the following code segment is not
I have encountered a major problem for myself in the learning process of WPF

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.