mongo-dot-notation-tool

Tool for mongo dot-notation objects

Usage no npm install needed!

<script type="module">
  import mongoDotNotationTool from 'https://cdn.skypack.dev/mongo-dot-notation-tool';
</script>

README

Mongo dot-notation tool

Master Build Status

Develop Build Status

Convert dot-notation to simple JS object and back.

Install

npm install mongo-dot-notation-tool --save

Use

var dotNotationTool = require('mongo-dot-notation-tool');

Methods

encode

Convert JS object to mongo dot-notation object

Use

dotNotationTool.encode({name: {last: 'Foo', first: 'Bar'}});
// returns: {'name.last': 'Foo', 'name.first': 'Bar'}

dotNotationTool.encode({$and: [{name: {last: 'Foo'}}, {position: 'CTO'}]});
// returns: {$and: [{'name.last': 'Foo'}, {position: 'CTO'}]}

dotNotationTool.encode({
  $and: [
    {
      $or: [
        {
          name: {
            last: 'Foo'
          }
        },
        {
          name: {
            last: 'Bar'
          }
        }
      ],
      position: 'CTO'
    ]
  }
});
/*
returns: {
  $and: [
    {
      $or: [
        {
          'name.last': 'Foo'
        },
        {
          'name.last': 'Bar'
        }
      ],
      position: 'CTO'
    ]
  }
}
*/

decode

Convert mongo dot-notation object to JS object

Use

dotNotationTool.decode({'name.last': 'Foo', 'name.first': 'Bar'});
// returns: {name: {last: 'Foo', first: 'Bar'}}

dotNotationTool.decode({$and: [{'name.last': 'Foo'}, {position: 'CTO'}]});
// returns: {$and: [{name: {last: 'Foo'}}, {position: 'CTO'}]}

dotNotationTool.decode({
  $and: [
    {
      $or: [
        {
          'name.last': 'Foo'
        },
        {
          'name.last': 'Bar'
        }
      ],
      position: 'CTO'
    ]
  }
});
/*
returns: {
  $and: [
    {
      $or: [
        {
          name: {
            last: 'Foo'
          }
        },
        {
          name: {
            last: 'Bar'
          }
        }
      ],
      position: 'CTO'
    ]
  }
}
*/

Run tests

npm test
# or
npm i
./node_modules/.bin/mocha ./test.js
# or
npm i -g mocha
mocha ./test.js