1

I have the following Document

{
    "_id" : ObjectId("608d5b653979af6a8555f643"),
    "eMail" : "[email protected]",
...
    "billing" : [ 
        {
            "company" : "MoskitoFactory",
            "salutation" : "1",
            "firstname" : "Ben",
            "lastname" : "Water",
            "address_line_1" : "Black 1235",
            "address_line_2" : "",
            "address_line_3" : "",
            "zip" : "555",
            "city" : "Onecity"
        }, 
        {
            "company" : "Witchcraft4U",
            "salutation" : "1",
            "firstname" : "Mike",
            "lastname" : "Phone",
            "address_line_1" : "Sea 4445",
            "address_line_2" : "",
            "address_line_3" : "",
            "zip" : "12345",
            "city" : "Somecity"
        }
    ]
}

how do I get the billing address with array index 0?

I thought this way

$billing = $db->buyer->findOne([

    '_id' => new MongoDB\BSON\ObjectID($_SESSION['uid'])
],[
    
    'projection' => ['billing.0' => 1]
]
);

var_dump($billing->billing);

but it gives me only

object(MongoDB\Model\BSONArray)#28 (1) {
    ["storage":"ArrayObject":private]=>
    array(2) {
      [0]=>
      object(MongoDB\Model\BSONDocument)#26 (1) {
        ["storage":"ArrayObject":private]=>
        array(0) {
        }
      }
      [1]=>
      object(MongoDB\Model\BSONDocument)#27 (1) {
        ["storage":"ArrayObject":private]=>
        array(0) {
        }
      }
    }
  }

I don't know if the query is correct and I simply don't know how to display the result. But If I read this right it gives me two objects back instead of one (billing address No 0)

1 Answer 1

1

try $slice operator to get specific element from array of object, this will return single element object in array.

$billing = $db->buyer->findOne([
    '_id' => new MongoDB\BSON\ObjectID($_SESSION['uid'])
],[   
    'projection' => ['billing' => ['$slice' => [0,1]]]
])

Playground


If you want to get object only instead of array try $arrayElemAt operator to get object from specific index,

This is Starting in MongoDB 4.4, you can use aggregation operator in find / findOne method's projection,

$billing = $db->buyer->findOne([
    '_id' => new MongoDB\BSON\ObjectID($_SESSION['uid'])
],[   
    'projection' => ['billing' => ['$arrayElemAt' => ['$billing', 0] ]]
])

Playground

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you but it's not exactly what I want but I've seen in the docu you can give an array to $slice. "billing": { $slice: [1, 1]} This works but I fail to do it in PHP :-( 'projection' => ['billing' => ['$slice' => [1,1]]] gives me an error (PHP 7.4) But I also consider upgrading MongoDB because I like the result from your second query in the playground even better ;-)
i think you are using wrong syntax of $slice, just try only as per my answer 'projection' => ['billing' => ['$slice' => 1]] and this should work. and might you seen that docu aggregation $slice operator there is a separate syntax for $slice it will support only in aggregation.
It works now with this syntax - It was a wired caching issue letting me think it doesn't. $slize => 1 gives me 1 array from the beginning 2 gives me 2 arrays but $slice => [0, 1] means skip, limit and give me exactly the one I want ok in our case (the first array both results are the same) can you please update your answer the first syntax 'projection' => ['billing' => ['$slice' => [0,1]]]and the playground so I can accept the answer. Thanks again for your input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.