Looking for equivalent query in Mongodb

I am trying to search for an meteor query for the following mondb query:
db.stocks.find({"History.payment_transferred":false},{History:{$elemMatch: {payment_transferred:false}}})

I tried the following

  let params = {
    "History.payment_transferred": false
  };
  let ctx = {
    sort: { 'History.startDate': 1 },
    fields: { History: { $elemMatch: { payment_transferred: false } } }
  }
  if (subscribe.ready()) {
    stocks = Stocks.find(params, ctx).fetch();
  }

Now this is supposed to work i assume as i saw helpful people suggesting in another thread. But i am getting the following error
MinimongoError: Minimongo doesn't support operators in projections yet.

Does this have soemthing to do with auto publish ? Right now i am using it but i plan to remove it next week.

Any suggestion is much appreciated and Thank you in advance!! :slight_smile:

Assuming I’ve got all the braces and brackets in the right order (typing witrhout checking), I think you probably want want something like:

db.stocks.find({ $or: [{ "History.payment_transferred": false }, { "History.payment_transferred": { $elemMatch: false } }] })

A word of warning: you seem to be saying that History.payment_transferred can be either false, or an array. If that’s what you have done, such inconsistent typing is going to plague you further down the line. :wink:

1 Like

Thanks for the reply ,

I am actually looking for Meteor equilvalnt code for
db.stocks.find({"History.payment_transferred":false},{History:{$elemMatch: {payment_transferred:false}}})

The above code works perfectly for Mongo Console. But the one for Meteor is not working.

And its true or false, will not be array :slight_smile:

Cheers!!

Minimongo (Meteor’s mongodb client-side cache) does not support $ operators with projections (fields): https://github.com/meteor/meteor/blob/046de405da50d89e4dc59084393f93a3d2561e2a/packages/minimongo/projection.js#L164-L165 .

You can query as you have, but return the whole History array document. Then just search for the matching array object with an alternative (for example using underscore findWhere). In Meteor server you should be able to query as you intended.

1 Like