简体   繁体   中英

Add new field to a MongoDB document parsing from String to Int using updateMany

In my MongoDB collection, all documents contain a mileage field which currently is a string. Using PHP, I'd like to add a second field which contains the same content, but as an integer value. Questions like How to change the type of a field? contain custom MongoDB code which I don't want to run using PHP, and questions like mongodb php Strings to float values retrieve all documents and loop over them.

Is there any way to use \MongoDB\Operation\UpdateMany for this, as this would put all the work to the database level? I've already tried this for static values (like: add the same string to all documents), but struggle with getting the data to be inserted from the collection itself.

Some further hints:

  • I'm looking for a pure PHP solution that does not rely on any binary to be called using exec . This should avoid installing more packages than needed on the PHP server
  • Currently, I have to use MongoDB in v4.0. Yes, that's not the most recent version, but I'm not in the position to perform an upgrade

You could use $set like this in 4.2 which supports aggregation pipeline in update.

$set stage creates a mileageasint based on the previous with $toInt value

db.collection.updateMany(
   <query>,
   [{ $set: { "mileageasint":{"$toInt":"$mileage" }}}],
   ...
)

Php Solution ( Using example from here )

$updateResult = $collection->updateMany(
    [],
    [['$set' => [ 'mileageasint' =>  [ '$toInt' => '$mileage']]]]
);

Try this, please:

01) MongoDB Aggregate reference:

db.collectionName.aggregate(
    [
        { "$addFields": { 
            "intField": { "$toInt": "$stringFieldName" } 
        }},
        { "$out": "collectionName" }
    ]
)

02) Possible PHP solution (Using as reference https://www.php.net/manual/en/mongocollection.aggregate.php ):

$pipeline = array(
    array(
        '$addFields' => array(
            'integerField' => array('$toInt' => '$mileage')
        )
    ),
    array(
        '$out' => 'collection'        
    ),
);
$updateResult = $collection->aggregate(pipeline);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM