I have some code that looks very similar to this in a create function:
if(req.body.test !== undefined)
{
if(--req.body.test EXISTS IN test (model)--)
{
DO STUFF
}
else
{
DO OTHER STUFF
}
}
I've been brainstorming for a while now, and I can't seem to figure out what code I would need to use to figure out the --req.body.test EXISTS IN test (model)--
part....I know it can't be as hard as I'm thinking it is.
Any help appreciated....
Thanks
答案 0 :(得分:1)
它可以像count()
一样简单吗?
if(db.collection.count({test: "test"}) > 2)
答案 1 :(得分:-1)
Have you tried a unique index at the MongoDB level and then catch a mongoose uniqueness error?
db.models.createIndex({ test: 1 }, { unique: true })
Then insert and check for error code 11001
. You can also findOne
and see if a document comes back.