问题
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