Simon February 2016

Mongodb: check value equality in any document

I have a form on my site where users can enter a number into the subNum field. When the user submits the form, I want to check if the value they've entered already exists as a value in any existing document. This is all done in javascript/meteor

All form data gets put into an object like this:

participantObject = {
  subNum: parseInt(number)
}

This object then gets inserted as a document into the Participants collection

When the submit button is clicked, I need to check whether the number they've entered has already been entered (i.e. exists as the value for subNum in any document). If so, prevent form submit and show an error

I'm trying the check with something like this:

var existingSubs = Participants.find(
  {subNum: {$eq: participantObject.subNum}}
).count();

console.log(existingSubs);

I am hoping that the above will find me all documents where subNum is equal to the value entered (participantObject.subNum), and console log the count of the number of matching documents

The problem is that in the console log I see Unrecognized operator: $eq

Am I using the equality operator incorrectly?

Is there a more efficient way to perform a check like this?

Answers


Alex Blex February 2016

You can omit $eq operator:

var existingSubs = Participants.find(
    {subNum: participantObject.subNum}
).count();

Post Status

Asked in February 2016
Viewed 2,888 times
Voted 6
Answered 1 times

Search




Leave an answer