<bdo id='LiqvY'></bdo><ul id='LiqvY'></ul>

  1. <legend id='LiqvY'><style id='LiqvY'><dir id='LiqvY'><q id='LiqvY'></q></dir></style></legend>
    1. <i id='LiqvY'><tr id='LiqvY'><dt id='LiqvY'><q id='LiqvY'><span id='LiqvY'><b id='LiqvY'><form id='LiqvY'><ins id='LiqvY'></ins><ul id='LiqvY'></ul><sub id='LiqvY'></sub></form><legend id='LiqvY'></legend><bdo id='LiqvY'><pre id='LiqvY'><center id='LiqvY'></center></pre></bdo></b><th id='LiqvY'></th></span></q></dt></tr></i><div id='LiqvY'><tfoot id='LiqvY'></tfoot><dl id='LiqvY'><fieldset id='LiqvY'></fieldset></dl></div>
      <tfoot id='LiqvY'></tfoot>

      <small id='LiqvY'></small><noframes id='LiqvY'>

    2. 在 NodeJS 中,如何从 mongodb 输出具有不同字段名称的结果?

      In NodeJS, how to output results from mongodb with different field names?(在 NodeJS 中,如何从 mongodb 输出具有不同字段名称的结果?)
          <tbody id='GAWdj'></tbody>
        1. <legend id='GAWdj'><style id='GAWdj'><dir id='GAWdj'><q id='GAWdj'></q></dir></style></legend>
            <bdo id='GAWdj'></bdo><ul id='GAWdj'></ul>

              <tfoot id='GAWdj'></tfoot>

              <small id='GAWdj'></small><noframes id='GAWdj'>

                <i id='GAWdj'><tr id='GAWdj'><dt id='GAWdj'><q id='GAWdj'><span id='GAWdj'><b id='GAWdj'><form id='GAWdj'><ins id='GAWdj'></ins><ul id='GAWdj'></ul><sub id='GAWdj'></sub></form><legend id='GAWdj'></legend><bdo id='GAWdj'><pre id='GAWdj'><center id='GAWdj'></center></pre></bdo></b><th id='GAWdj'></th></span></q></dt></tr></i><div id='GAWdj'><tfoot id='GAWdj'></tfoot><dl id='GAWdj'><fieldset id='GAWdj'></fieldset></dl></div>
              • 本文介绍了在 NodeJS 中,如何从 mongodb 输出具有不同字段名称的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 nodejs 查询 mongodb 并希望输出带有自定义字段名称的 json.

                I'm using nodejs to query mongodb and want to output json with customized field names.

                例如,可能是来自 MongoDB 的原始 json

                For example, original json from MongoDB maybe

                    {id:1, text:"abc"}
                

                我想把它输出为

                    {ObjectID:1, DisplayText:"abc"};
                

                我了解 MongoDB 在其聚合框架中有 $project 运算符,但不确定如何在 NodeJS 中使用它们.

                I understand MongoDB has the $project operator in its aggregate framework but not sure how to use them in NodeJS.

                我使用的 mongodb nodejs 包是

                The mongodb nodejs packages I'm using are

                    var mongo = require('mongodb');
                    var monk = require('monk');
                    var db = monk('server:port/mydb');
                

                感谢您对此的任何建议.

                Appreciate any advice on this.

                推荐答案

                如果您使用的是和尚,那么您可以通过 .col 访问器访问底层节点本机驱动程序集合类型在您选择的集合对象上:

                If you are using monk as you appear to be then you can access the underlying node native driver collection type via the .col accessor on your selected collection object:

                  var db = require('monk')('localhost/test')
                    , collection = db.get('example');
                
                  collection.col.aggregate(
                    [
                      { "$project": {
                        "_id": 0,
                        "ObjectID": "$_id",
                        "DisplayText": "$text"
                      }}
                    ],
                    function(err,result) {
                
                      console.log( JSON.stringify( result, undefined, 4 ) );
                
                    }
                  );
                

                注意 .aggregate() 以这种方式检索的不会像标准的僧侣集合对象那样包装在承诺对象中.但至少这向您展示了如何访问和使用 $project 重新塑造您的文档.

                Note that methods such as .aggregate() retrieved in this way are not wrapped in the promise object as the standard monk collection objects are. But at least this shows you how to access and use $project to re-shape your document.

                这篇关于在 NodeJS 中,如何从 mongodb 输出具有不同字段名称的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                Ordinals in words javascript(javascript中的序数)
                getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
              • <small id='Yb4Qp'></small><noframes id='Yb4Qp'>

                  <i id='Yb4Qp'><tr id='Yb4Qp'><dt id='Yb4Qp'><q id='Yb4Qp'><span id='Yb4Qp'><b id='Yb4Qp'><form id='Yb4Qp'><ins id='Yb4Qp'></ins><ul id='Yb4Qp'></ul><sub id='Yb4Qp'></sub></form><legend id='Yb4Qp'></legend><bdo id='Yb4Qp'><pre id='Yb4Qp'><center id='Yb4Qp'></center></pre></bdo></b><th id='Yb4Qp'></th></span></q></dt></tr></i><div id='Yb4Qp'><tfoot id='Yb4Qp'></tfoot><dl id='Yb4Qp'><fieldset id='Yb4Qp'></fieldset></dl></div>

                    • <tfoot id='Yb4Qp'></tfoot>

                      • <bdo id='Yb4Qp'></bdo><ul id='Yb4Qp'></ul>
                          <tbody id='Yb4Qp'></tbody>

                        <legend id='Yb4Qp'><style id='Yb4Qp'><dir id='Yb4Qp'><q id='Yb4Qp'></q></dir></style></legend>