0

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}]

6
  • Do yo mean this Commented Jan 23, 2021 at 16:15
  • @J.F. No, I only need to output the id in numbers Commented Jan 23, 2021 at 17:49
  • Can you post an input and output example? Commented Jan 23, 2021 at 17:55
  • @J.F. Input: "users": [{"id": 124, "uid": 2, "level": 1}, {"id": 553, "uid": 19, "level": 3}]; Output: 553 Commented 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. Commented Jan 23, 2021 at 18:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

And is it possible to somehow output the value without .then?
You can use async/await. var result = await yourModel.findOne(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.