0

In the MongoDb (v4.0) docs it says we can pass a javascript function to a where clause. I would expect the following trivial example to return zero docs...

let receipts = 
    await db
        .collection<IReceipt>("receipt_txs")
        .find({$where: function () { return false; } } )
        .toArray();

However this returns every doc.

The following syntax works, but for operational reasons i would rather use the above syntax.

let receipts = 
    await db
        .collection<IReceipt>("receipt_txs")
        .find({$where: "function () { return false; }" } )
        .toArray();

let receipts = 
    await db
        .collection<IReceipt>("receipt_txs")
        .find({$where: "false" } )
        .toArray();

Can anyone help me to understand where I'm going wrong? The docs are quite clear, and this syntax has been valid since v2 so I'm sure it can't be a bug.

2
  • Which language you are using? The first snippet will work only in javascript/mongo shell Commented Aug 30, 2018 at 16:21
  • I'm using typescript, hadnt considered the implications of the compile down, will check that now :/ Commented Aug 30, 2018 at 16:23

1 Answer 1

1

$where with function literals is not compatible with all frameworks. For example it is a feature request in Meteor and not supported out of the box.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I see now. Pass a function in directly is only supported by the mongo shell. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.