简体   繁体   中英

Unable to use agrregate operators in MongoDB(storageEngine=mmapv1)

I am getting following error while using $convert operator, mongodb storageEngine is mmapv1 .

db.persons.aggregate([
  {
    $project: {
      registred: { $convert: { input: "registered.age", to: "double" } }
    }
  }
]);

Error: command failed: { "ok": 0, "errmsg": "invalid operator '$convert'", "code": 15999 }

The operator $convert is available only for MongoDB versions >= 4.0.

That said, you have two options:

1- Updating you MongoDB

2- Converting the value on the result of the aggregation. In Javascript you could do the following:

var result = db.persons.aggregate([
    {
        $project:{
            registred:{
                $convert:{input:"registered.age",to:"double"}
            }
        }
    }
]);

result.forEach(
    function(document) {
        document.registred.age = parseFloat(document.registred.age);
    }
);

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