简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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