My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Related to MongoDB check Unique query!!!!

PRANJAL AGNIHOTRI's photo
PRANJAL AGNIHOTRI
·Dec 15, 2018

Hii there,

I am using MongoDB with NodeJS and using a module Mongoose(a wrapper around Mongodb) around it to do my database operations. I need to check 3-4 values to be unique in the collection. I can simply do it by

MyModel.find();

but it checks for the given conditions to be present in a single document.For e.g. I have three values passed in the conditions they should be checked across all documents and if single one is met it should return a document.

Like if i have passed field1,field2 ,field3 if a document matches just field1 it should be returned.

I want to do this because then I have to write 3 different

MyModel.find();

queries for three fields and it will be quite messy.

Note: I have marked unique property to true in schema, but I need to write a explicit check.