How to output a value from array Mongoose?

痞子三分冷 提交于 2021-01-29 15:37:05

问题


I have an array that contains the values: id, uid, level, I need to output the user id with level = 3 how do I do this? Sample code: "users": [{"id": 124, "uid": 2, "level": 1}, {"id": 553, "uid": 19, "level": 3}]


回答1:


Using mongoose you can do this using findOne:

yourModel.findOne({level:3},{id:1}).then(result => {
  console.log("result = ",result.id)
}).catch(e => {
  // error
})

Response is 553

Example how mongo query works here. This example output an array but ising findOne only one value will be returned, so you can do response.name directly and get the value.



来源:https://stackoverflow.com/questions/65860664/how-to-output-a-value-from-array-mongoose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!