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}]
-
Do yo mean thisJ.F.– J.F.2021-01-23 16:15:31 +00:00Commented Jan 23, 2021 at 16:15
-
@J.F. No, I only need to output the id in numbersPololo– Pololo2021-01-23 17:49:40 +00:00Commented Jan 23, 2021 at 17:49
-
Can you post an input and output example?J.F.– J.F.2021-01-23 17:55:52 +00:00Commented Jan 23, 2021 at 17:55
-
@J.F. Input: "users": [{"id": 124, "uid": 2, "level": 1}, {"id": 553, "uid": 19, "level": 3}]; Output: 553Pololo– Pololo2021-01-23 18:11:31 +00:00Commented Jan 23, 2021 at 18:11
-
Check this question. Mongo returns an object, the best way is to acces value in the lenguage you use.J.F.– J.F.2021-01-23 18:29:02 +00:00Commented Jan 23, 2021 at 18:29
|
Show 1 more comment
1 Answer
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.