简体   繁体   English

如何检查 mongoose / mongo db 中特定文档数组中的元素是否存在?

[英]How to check if element exixts in a array of a especific document in mongoose / mongo db?

look, I have this schema:看,我有这个架构:

_id: 'id'
info: {
   array: ['a', 'b', 'c']
}

I want to check something like this:我想检查这样的事情:

doc = schema.find({ _id: 'id' });
if ( doc.info.array.includes('a') ) //returning true

I don't know how to make that.我不知道该怎么做。 Can someone help me?有人能帮我吗?

there are many ways to check this but here are a few:有很多方法可以检查这一点,但这里有一些:

 var data = schema.find({ _id: "id", array: { $in: ["a"] }, }); if (;data) { return false; } return true

Another way is by:另一种方法是:

 var data = schema.find({ _id: "id", array: { $in: ["a"] }, }).count(); if (data == 0) { return false; } return true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM