question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[mongo2js] Bug: field level operators must support arrays

See original GitHub issue

I’m currently testing mongo2js and noticed that gt and gt doesn’t works on nested arrays. I’m using sift to compare results.


const sift = require("sift");
const { filter } = require("@ucast/mongo2js");

const query = { "foo.bar.baz": { $lt: 0 } };

const uQuery = filter(query);
const siftQuery = sift(query);

const data = { foo: [{ bar: [{ baz: 1 }] }] };

const uResult = uQuery(data);
// true
// value returned by "getField" : [ 1 ] and comapred against query value "1"

const siftResult = siftQuery(data);
// false

This is because in @ucast/js, in this case, the comparison interpreters like lt compare array of values against a single value. This is due to the getField function that returns an array of value.

I thought about a solution and because the actual value could be an array, we would need to know if values have been aggregated into an array or if the value is an array. But there might be a simpler solution.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
stalniycommented, Aug 14, 2020

fixed in @ucast/js@2.1.1

run npm update and it will fetch new deps for @ucast/mongo2js

0reactions
J3m5commented, Aug 14, 2020

That’s right, it’s there.

Arrays

With arrays, a less-than comparison or an ascending sort compares the smallest element of arrays, and a greater-than comparison or a descending sort compares the largest element of the arrays. As such, when comparing a field whose value is a single-element array (e.g. [ 1 ]) with non-array fields (e.g. 2), the comparison is between 1 and 2. A comparison of an empty array (e.g. [ ]) treats the empty array as less than null or a missing field.

What I saw is this, but it’s only mentioning that it doesn’t works with objects.

So I think it’s safe to do it this way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter-4 Lab 2: Array Operators - M001: MongoDB Basics
The first slice is fine, but $all is not a valid expression operator. We could re-write the query as: db.coll.find({ "property_type":"House", " ...
Read more >
import does not work on json arrays files. #433 - GitHub
I am getting the same error: "MongoError: operation passed in cannot be an Array" i used the modification and doesnt help at all, ......
Read more >
Im getting an "Unknown top level operator error" in MongoDB
The data is in an array in the collection called sales and the array's name is items and I then want to match...
Read more >
@ucast/mongo2js - npm
Start using @ucast/mongo2js in your project by running `npm i ... In order to implement a custom operator, you need to create a...
Read more >
MongoDB - Array Update Operators - YouTube
In this video, shows you different operators on update an array value.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found