Node.js + MongoDB - findOne() 返回一个字段不起作用 [英] Node.js + MongoDB - findOne() return one field not working

查看:48
本文介绍了Node.js + MongoDB - findOne() 返回一个字段不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能使用findOne()"只返回一个字段?在下面的代码中,返回所有字段.我也试过find()",但还是不行.有人能告诉我是我犯了错误还是什么?

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

文档看起来像这样:

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

推荐答案

您没有使用投影选项:

{projection: { info: true }}

{projection: { info: true }}

你的方式

{信息:1}

这意味着您请求对信息使用索引(如果存在)

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

这篇关于Node.js + MongoDB - findOne() 返回一个字段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆