Flavien Volken
Flavien Volken

Reputation: 21359

Meteor: Can I check if a plain js object pass a MongoDB query?

I would need to filter a plain JS object using a MongoDB request (Meteor server side) this ideally without inserting it into a DB, is this possible ?

Something like Mongo.match(myPlainObject, {"fieldName":"valueExpected"});

Upvotes: 1

Views: 202

Answers (1)

Thomas Goorden
Thomas Goorden

Reputation: 280

Why would you do this within Mongo? You can test JSON objects using the built-in Check functionality in Meteor. A big advantage is that this test can be made available on both client and server, which you often want to do for efficiency/security.

You can use Check for date ranges or anything else as well. Whatever is not covered by the predefined matches, can be done like this:

NonEmptyString = Match.Where(function (x) {
  check(x, String);
  return x.length > 0;
});
check(arg, NonEmptyString);

You could write any date related matches you need. (Keeping in mind that you can also make sure it is in fact a Date by stating:

check(arg,Date);

Upvotes: 1

Related Questions