## Decimal128

You can define Decimal128 type properties as

```js
// in model.json file
// 1st level property
"count": {
  "type": "String",
  "mongodb": {"dataType": "Decimal128"}
},
// nested array
"lines": [
  {
    "unitPrice": {
      "type": "string",
      "title": "The unitPrice Schema ",
      "mongodb": {
        "dataType": "Decimal128"
      }
    }
  }
],
// nested object
"summary": {
  "totalValue": {
    "type": "string",
    "title": "The totalValue Schema ",
    "mongodb": {
      "dataType": "Decimal128"
    }
  }
},
```

Suppose model `Order` has the model definition above, read the sections below for the usage of CRUD operations.

### Create

```js
const sample = {
  count: '0.0005',
  summary: {totalValue: '100.0005'},
  lines: [{unitPrice: '0.0005'}],
};
Order.create(sample, function(err, order) {
  // order: {
  //  _id: '5bc8cc7f71cade4a8b5af886', 
  //  count: '0.0005',
  //  summary: {totalValue: '100.0005'},
  //  lines: [{unitPrice: '0.0005'}],
  // }
});
```
The created record in the mongodb database will be:

```js
{ 
  "_id" : ObjectId("5bc8cc7f71cade4a8b5af886"), 
  "count" : NumberDecimal("0.0005"),
  "lines" : [ { "unitPrice" : NumberDecimal("0.0006") ],
  "summary": { "totalValue": NumberDecimal("100.0005")}
}
```

The returned model instance is generated by the `create` method in `loopback-datasource-juggler/lib/dao.js`, connector only
returns the `id` field. Therefore even the data is inserted as decimal, you still get a string in the callback function.
If you need to continue processing with a decimal data, a workaround is doing the conversion manually:

```js
var Decimal128 = require('mongodb').Decimal128;
Order.create({count: '0.0005'}, function(err, order) {
  // convert the string to decimal
  order.count = Decimal128.fromString(order.count);
  // ... same conversion for nested properties
});
```

### Find

You can filter a first level decimal property like

```js
OrderDecimal.find({where: {count: '0.0005'}}, function(err, orders) {
  // `orders` are all the data with `count` equivalent to NumberDecimal("0.0005")
});
```

The connector automatically converts the condition from string to decimal.

When query nested properties, you still need to do the conversion yourself, as follows

```js
// query decimal property in a nested array
const arrCond = {where: {lines: {elemMatch: {unitPrice: Decimal128.fromString('0.0005')}}}};
OrderDecimal.find(arrCond, function(err, orders) {});

// query decimal property in a nested object
const objCond = {where: {'summary.totalValue': Decimal128.fromString('100.0005')}};
OrderDecimal.find(objCond, function(err, orders) {});
```

### DestroyAll

You can destroy data with a filter contains decimal property. For example:

```js
OrderDecimal.destroyAll({count: '0.0005'}, cb);
// or
const arrCond = {where: {lines: {elemMatch: {unitPrice: Decimal128.fromString('0.0005')}}}};
OrderDecimal.destroyAll(arrCond, cb);
// or
const objCond = {where: {'summary.totalValue': Decimal128.fromString('100.0005')}};
OrderDecimal.destroyAll(objCond, cb);
```
