Adam
Adam

Reputation: 3288

MongoDB Match Engine in Javascript

Context:

I have a nodejs app which is inserting documents to a mongoDB.

I have users who create custom queries to this mongodb to find documents. I also use change streams to live feed updates to them.

I'm using the mongodb 3.6.2 nodejs driver.

Question:

It could be possible for me to remove the change streams if I could check if the document I was inserting matched one of my user's queries.

For example, let's say a user was interested in documents that matched the query { "foo": "bar" }. If I insert a document { "baz": 1, "foo": "bar" }, I could send that document down the wire to the user and not need to rely on my change stream to listen for the insert.

Is there any way for me (in javascript) to match an object against a query so that I don't need to rely on my change stream?

EDIT

  1. minimongo might be an option for me? It looks like they're just using lodash after transforming the selector.
  2. I could also use the minimongo package from meteor.js which the above was inspired / forked from. var matcher = new Minimongo.Matcher({a: {$gt: 5}}); if (matcher.documentMatches({a: 7})) ...

Upvotes: 1

Views: 165

Answers (1)

Adam
Adam

Reputation: 3288

I ended up going with minimongo

import minimongo from 'minimongo';

const query = { ... }
const docForInsert = { ... }

const found = minimongo.utils.processFind([docForInsert], query)

if (found?.length) {
   ...
}

Upvotes: 1

Related Questions