stalniy/ucast

MongoQueryParser Or is Parsed as And

Closed this issue · 1 comments

I've noticed that this top level "or" is parsed as "and" incorrectly. Is there something wrong with my condition syntax? Thanks!

// Example 1:
import { Condition } from '@ucast/core';
import { MongoQueryParser, allParsingInstructions } from '@ucast/mongo';

const parser = new MongoQueryParser(allParsingInstructions);
const condition = { $or: [{ id: { $eq: 1 }, name: { $eq: 'abc' } }] };
const ast = parser.parse(condition);
console.log(ast);

// ast shows operator "and". Expecting operator "or".
/*
{
  operator: "and",
  value: [
    {
      operator: "eq",
      value: 1,
      field: "id",
    },
    {
      operator: "eq",
      value: "abc",
      field: "name",
    },
  ],
}
*/

// Example 2:
import { Condition, FieldCondition, CompoundCondition } from '@ucast/core';
import { MongoQueryParser, allParsingInstructions } from '@ucast/mongo';

const parser = new MongoQueryParser(allParsingInstructions);
const condition = new CompoundCondition('or', [
    new FieldCondition('gt', 'x', 5),
    new FieldCondition('lt', 'y', 10),
]);
const ast = parser.parse(condition);
console.log(ast);

// again, ast shows operator "and". Expecting operator "or".
{
  operator: "and",
  value: [
    {
      operator: "eq",
      value: 1,
      field: "id",
    },
    {
      operator: "eq",
      value: "abc",
      field: "name",
    },
  ],
}

My MongoDB $or condition was incorrect.

// Fixed condition.
const condition = { $or: [{ id: { $eq: 1 } }, { name: { $eq: 'abc' } }] }

// ast shows operator "or" as expected now.
/*
{
  operator: "or",
  value: [
    {
      operator: "eq",
      value: 1,
      field: "id",
    },
    {
      operator: "eq",
      value: "abc",
      field: "name",
    },
  ],
}
*/