Skip to content

Mongo expolorations

JeanBaptisteWATENBERG edited this page Aug 13, 2019 · 1 revision

Mongo expolorations

const parse = require('mongodb-language-model').parse;

const query = {"name": "foo.pdf", "extendedProperties.status": {"$in": ["TEST1", "TEST2"]}, "$or": [{"data.objects.field": "value1", "size": {"$gt": 3}}, {"size": {"$lte": 3}}]};

const queryAst = parse(JSON.stringify(query));
console.log(query)
console.log(JSON.stringify(queryAst, null, 2))
{
  "pos": "expression",
  "clauses": [
    {
      "pos": "leaf-clause",
      "key": "name",
      "value": {
        "pos": "leaf-value",
        "value": "foo.pdf"
      }
    },
    {
      "pos": "leaf-clause",
      "key": "extendedProperties.status",
      "value": {
        "pos": "operator-expression",
        "operators": [
          {
            "pos": "list-operator",
            "operator": "$in",
            "values": [
              {
                "pos": "leaf-value",
                "value": "TEST1"
              },
              {
                "pos": "leaf-value",
                "value": "TEST2"
              }
            ]
          }
        ]
      }
    },
    {
      "pos": "expression-tree-clause",
      "operator": "$or",
      "expressions": [
        {
          "pos": "expression",
          "clauses": [
            {
              "pos": "leaf-clause",
              "key": "data.objects.field",
              "value": {
                "pos": "leaf-value",
                "value": "value1"
              }
            },
            {
              "pos": "leaf-clause",
              "key": "size",
              "value": {
                "pos": "operator-expression",
                "operators": [
                  {
                    "pos": "value-operator",
                    "operator": "$gt",
                    "value": {
                      "pos": "leaf-value",
                      "value": 3
                    }
                  }
                ]
              }
            }
          ]
        },
        {
          "pos": "expression",
          "clauses": [
            {
              "pos": "leaf-clause",
              "key": "size",
              "value": {
                "pos": "operator-expression",
                "operators": [
                  {
                    "pos": "value-operator",
                    "operator": "$lte",
                    "value": {
                      "pos": "leaf-value",
                      "value": 3
                    }
                  }
                ]
              }
            }
          ]
        }
      ]
    }
  ]
}

https://github.com/mongodb-js/mongodb-language-model

Clone this wiki locally