如何在Mongodb文档中获取字符串值? [英] How to get string value inside a Mongodb document?

查看:26
本文介绍了如何在Mongodb文档中获取字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此文档存储在 mongodb 文档中:

I have this document stored in mongodb document:

{
    "_id":ObjectId("4eb7642ba899edcc31000001")
    "hash":"abcd123"
    "value":"some_text_here"
}

我正在使用 NodeJS 访问数据库:

I am using NodeJS to access the database:

collection.findOne({'hash' : req.param('query') }, 
    function(err, result){ 
    console.log(res);   
});

此查询的结果是整个文档,但我只需要获取值"文本:some_text_here"

The result of this query is the whole document, however I need to get only the "value" text: "some_text_here"

这是怎么做到的?

推荐答案

您可以指定您感兴趣的字段(尽管 _id 将始终返回):

You can specify the fields that you are interested in (_id will always be returned, though):

collection.findOne({'hash': req.param('query') },
                   {fields: ['value'] },
                   callbackFunction );

这篇关于如何在Mongodb文档中获取字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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