json-mongo-condition

1.0.3 • Public • Published

Example

var Convertor = require('json-mongo-condition');
var condition = {};
var conditionObj = {
				    type: "and",
				    elements: [
				        {
				            type: "or",
				            elements: [
				                {
				                    type: "and",
				                    elements: [
				                        {
				                            type: "condition",
				                            map_key: "",
				                            map_value: "value_1",
				                            logic: "=",
				                            compare: 2
				                        },
				                        {
				                            type: "condition",
				                            map_key: "",
				                            map_value: "value_2",
				                            logic: ">",
				                            compare: 5
				                        }
				                    ]
				                },
				                {
				                    type: "condition",
				                    map_key: "",
				                    map_value: "value_3",
				                    logic: "<",
				                    compare: 6
				                },
				                {
				                    type: "condition",
				                    map_key: "",
				                    map_value: "value_4",
				                    logic: "=",
				                    compare: 2
				                }
				            ]
				        },
				        {
				            type: "or",
				            elements: [
				                {
				                    type: "condition",
				                    map_key: "",
				                    map_value: "value_5",
				                    logic: "=",
				                    compare: 7
				                },
				                {
				                    type: "condition",
				                    map_key: "",
				                    map_value: "value_6",
				                    logic: "or",
				                    compare: [
				                        3,
				                        4,
				                        7
				                    ]
				                }
				            ]
				        }
				    ]
				}
Convertor.convert(conditionObj, [], condition);	
console.log(JSON.stringify(condition));

this will print in screen:

{
    "$and": [
        {
            "$or": [
                {
                    "$and": [
                        {
                            "value_1": 2
                        },
                        {
                            "value_2": {
                                "$gt": 5
                            }
                        }
                    ]
                },
                {
                    "value_3": {
                        "$lt": 6
                    }
                },
                {
                    "value_4": 2
                }
            ]
        },
        {
            "$or": [
                {
                    "value_5": 7
                },
                {
                    "value_6": {
                        "$in": [
                            3,
                            4,
                            7
                        ]
                    }
                }
            ]
        }
    ]
}

Installation

$ npm install json-mongo-condition

Dependents (0)

Package Sidebar

Install

npm i json-mongo-condition

Weekly Downloads

0

Version

1.0.3

License

MIT

Last publish

Collaborators

  • samicelus