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

152
Views
Updating and pushing an array to mongo collection

Hello all I am trying to create a query in order to update user's by adding another product in MongoDB.

The structure of the table orders is:

{
  "username": "mike",
  "products": [
    {
      "_id": {
        "$oid": "61f14c42855800006f003062"
      },
      "number": "3",
      "name": "Honduras",
      "price": 7,
      "stock": 10
    }
  ],
  "status": "UNPAID"
}

My query in PHP code is the following:

$collection -> updateOne(
  "username"=>"$username",'$push'=>["products" => "$json"]
);

But I get the same error everytime:

Parse error: syntax error, unexpected token "=>", expecting ")" 

Any suggestions?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

i use a third party service/library to do that - MongoDBDriverManager PHP library

<?php

    $query = new MongoDBDriverBulkWrite;
    $query->update(
       ['username' => $username],
       ['$set' => ['products' => $json]],
    );

    $db_manager = new MongoDBDriverManager('mongodb://localhost:27017');
    $dbresult= $manager->executeBulkWrite('dbname.orders', $query);

?>

you can get more understanding i get my resources from here..

https://docs.mongodb.com/drivers/php/

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!