简体   繁体   中英

Node.js + MongoDB - findOne() return one field not working

Why can I not be able to return only one field using "findOne()"? In the code below, all fields are returned. I also tried "find()", but still not working. Can someone tell me whether I made a mistake or what?

In this case, I want to return only "info" field

const mongodb = require('mongodb').MongoClient
...
db_main.collection('info').findOne({ _id: '123456789' }, { info: 1 }, function(err, result) {
    console.log(result)
})

The document look something like this:

_id: '123456789',
title: 'I love title',
content: 'content here',
info: {
    date: '1/1/2018',
    user: 'username'
}

You are not using the projection option:

{projection: { info: true }}

The way you are doing

{info:1}

It means you are requesting to use an index on info (if it exist)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM