鍍金池/ 問答/數(shù)據(jù)庫(kù)  HTML/ mongoose如何查找摸個(gè)數(shù)組里含有某個(gè)值?

mongoose如何查找摸個(gè)數(shù)組里含有某個(gè)值?

例如

const ProjectSchema = new mongoose.Schema({

name : { type:String },

time : { type:Date, default:Date.now },

url : { type: String},

user : { type: Object},

proId : {type: String},

spaceId : {type: String},

description: {type: String},

participant: {type: Array}

})

我需要 通過 傳入的id 查找,participant里面含有該id的數(shù)據(jù)

回答
編輯回答
陌顏

mongooes我不怎么熟,所以我就寫查詢語(yǔ)句了
我不知道你的participant內(nèi)部是什么結(jié)構(gòu)
假如是

participant:[1,2,3]

然后要找id是1,那么查詢條件是

{participant:{$elemMatch:{$eq:1}}}

假如是

 participant:[{id:1,num:5}]

然后要找id是1,那么查詢條件是

{participant:{$elemMatch:{id:1}}}

$elemMatch

2017年12月29日 01:26