vazco / sparrowql

Declarative MongoDB aggregations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mongodb $and & $or query operators

ralphievolt opened this issue · comments

Documents does not show how to use the following function/operators in SparrowQL thought it can be done

$and and $or

So, as each operator looks differently, and handling each might be problematic, that usage is definitely different. You can see it in this test:

const pipeline = build({
    query: {
        $and: {
            required: [`${collectionA.collectionName}._id`],
            perform: relative => [{[relative(`${collectionA.collectionName}._id`, false)]: 0}]
        }
    },
    start: collectionA.collectionName
});

How it works:

  • required defines a list of fields, which are needed to compute this operator,
  • perform is a function, which has to return the body of given operator,
  • relative(name, isPath) function is useful to make sure, that the perform works for each start collection
  • isPath parameter prefixes returned path with $, which is sometimes needed, for example in $ifNull

If it's working for you, @ralphievolt, and that is completely clear, I'll write it down and include in the docs (or link it at least for now).

this is what I came up with to emulate this

$match: { $and: [{ _id:"umXM2JtWwXCtMhz4m" },{status: "confirmed" }, ] }

to

const query = {
    $and: {
      required: ["${Orders}._id", "${Orders}.status"],
      perform: relative => [
        {
          [relative("${Orders}._id", false)]: "umXM2JtWwXCtMhz4m"
        },
        {
          [relative("${Orders}.status", false)]: "confirmed"
        }
      ]
    }
  };

but its not working. I am not sure what I am missing.

It's because there's no "${Orders}._id" field in your schema. In this example a template string is being used. Here, `${Orders}._id` means '' + Orders + '._id'. I'm not sure, if your Orders, when casted to a string will result in the collection name - you have to check it.

got it.