How to use mongoose to query embedded data?

const userSchema = new Schema({
  name:{type:String},
  clubnumber:{type:String},
  memo:[{
    memos:{type:String}
  }]
})

the data structure is as above. Now you want to match name and clubnumber, to query all the memo under the library

db.userModel.findOne({name:name,clubnumber:clubnumber},(e,d)=>{
            console.log(d.memo);
        })

if I write this, the data from the console is

.
[
    { memos:xxx,id:xxxx},
    { memos:xxx1,id:xxxxx}
]

the data structure I want is


    {memos1,memos2,memos3}//memos

now I can think of recreating an array by traversing, but do any great gods know what mongoose can do to get such an array directly? thank you very much.


two methods:

1. Remove the option of _ id when creating an array model.
  mongo official website  

Note:
  • the above is the result of local operation.
  • The version of mongoose used by
  • is 5.2.5
  • mongo version: 3.4
Menu