const userSchema = new Schema({
name:{type:String},
clubnumber:{type:String},
memo:[{
memos:{type:String}
}]
})
数据结构如上,现在是想匹配name和clubnumber,来查询库下的所有memo
db.userModel.findOne({name:name,clubnumber:clubnumber},(e,d)=>{
console.log(d.memo);
})
小弟这样写的话,控制台出来的数据是
[
{ memos:xxx,id:xxxx},
{ memos:xxx1,id:xxxxx}
]
小弟想得到的数据结构是
{memos1,memos2,memos3}//所有memos的一个数组
小弟现在能想到用遍历重新创建一个数组,但是有大神知道mongoose有什么操作能直接获得这样的吗,感激不尽~
两种做法:
1、在创建数组模型的时候去掉_id的选项。
返回结果:
2、mongo里可以只返回匹配的数组中的记录。
具体做法参考:mongo官网
注: