mongo-dot-notation-tool

1.0.1 • Public • Published

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

Package Sidebar

Install

npm i mongo-dot-notation-tool

Weekly Downloads

69

Version

1.0.1

License

ISC

Last publish

Collaborators

  • dmitry_smv
  • denisbezrukov
  • zbit
  • alekns
  • takatan
  • vasiliykuznetsov