K
K
KnightForce2017-01-20 03:22:43
MongoDB
KnightForce, 2017-01-20 03:22:43

How to filter query result from Mongoose?

Using Mongoose with find({}) I get all the documents, but when iterating in a loop, there are a lot of auxiliary things (I looked, outputting to the console), but I need ONLY the fields.
Is there a way to filter?
At:

User.find({}, function(err, docs){
      for(let key in docs) {
        console.log(docs[key]);
      }
    });

Here is part of the output docs[key]:
{ status: 'active',
    ID: '2',
    _id: 5877e9f30d64858af7b3dd6c }
    { [Function] numAsyncPres: 1 }
    [Function: wrappedPointCut]
    { '$__original_save': 
    [ { [Function: fn] isAsync: false },
    { [Function: fn] isAsync: true } ],
    '$__original_validate': [ { [Function: fn] isAsync: true } ],
    '$__original_remove': [ { [Function: fn] isAsync: true } ] }
    { '$__original_save': [],
    '$__original_validate': [],
    '$__original_remove': [] }
    { [Function] numAsyncPres: 1 }
    [Function: wrappedPointCut]
    { [Function] numAsyncPres: 1 }
    [Function: wrappedPointCut]
    NativeConnection {
    base: 
    Mongoose {
    connections: [ [Object], [Object], [Object], [Object], [Circular] ],
    plugins: [],
    models: {},
    modelSchemas: { User: [Object], Patient: [Object] },
    options: { pluralization: true } },
    collections: 
    { users: 
    NativeCollection {
    collection: [Object],
    opts: [Object],
    name: 'users',
    collectionName: 'users',
    conn: [Circular],
    queue: [],
    buffer: false,
    emitter: [Object] } },
    models: 
    { User: 
    { [Function: model]
    hooks: [Object],
    base: [Object],
    modelName: 'User',
    model: [Function: model],
    db: [Circular],
    discriminators: undefined,
    schema: [Object],
    collection: [Object],
    Query: [Object],
    '$__insertMany': [Function],
    insertMany: [Function] } },
    config: { autoIndex: true },

I only need this:
{ status: 'active',
        ID: '2',
        _id: 5877e9f30d64858af7b3dd6c }

There are ideas on how to filter it yourself, but I don’t want to write a crutch.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KnightForce, 2017-01-20
@KnightForce

Found a way out:
find({}).lean().exec(function(err, docs){});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question