A
A
astorme2018-06-28 19:19:52
MongoDB
astorme, 2018-06-28 19:19:52

How to get data in mongoose?

Please tell me how to get the data, not in the form of an array with all the information suitable for the search result, but specific, as in this scheme, for example, get only the value of the name field?

const modelSchema = new Schema (
    {
        id: {
            type: String,
            required: true
        },
        name: {
            type: String,
        },
        age: {
            type: Number,
            min: 18,
            max: 90
        },
        cite: {
            type: String
        }
    }
)

not something like this..
{ _id: 5b34ff4b3d677b2be4f7e684, name: 'Melis', age: 19, __v: 0 }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
astorme, 2018-06-28
@astorme

Actually I found a solution, here it is

Person.find({age:19})
    .then(persons => {console.log(persons[0].name)})

in this case, it outputs the string "Melis" directly, instead of { _id: 5b34ff4b3d677b2be4f7e684, name: 'Melis', age: 19, __v: 0 }, as I understand it, the problem here is that the result is an array, and 0 in its dimension is not limitation (in any case, this is the only way it gives out a name from all positions in the database)

J
Jumandjilos, 2018-06-28
@Jumandjilos

and what hinders then simply to address to property name from the received object?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question