在使用MongoDB-Node.JS本机驱动器通过查询从MongoDB中获取字段时,如何重命名/别名字段? [英] How to rename/alias field(s) while fetching it from MongoDB through query using MongoDB-Node.JS native drive?

查看:72
本文介绍了在使用MongoDB-Node.JS本机驱动器通过查询从MongoDB中获取字段时,如何重命名/别名字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码,我正在使用这些代码从本地MongoDB服务器中获取数据.

Consider the following code, which I am using to fetch the data from my local MongoDB server.

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');
var db = new Db('test', new Server('localhost', 27017)); 
db.open(function(err, db) {
  db.createCollection('simple_limit_skip_find_one_query', function(err, collection) {
    assert.equal(null, err);

    collection.insert([{a:1, b:1}, {a:2, b:2}, {a:3, b:3}], {w:1}, function(err, result) {
      assert.equal(null, err); 
      collection.findOne({a:1}, {fields:{b:1}}, function(err, doc) {
        // I got the read document in the object 'doc'
      });
    });
  });
});

现在,我想重命名一个字段名称,而仅检索(不在数据库中),例如,使用上面的代码,我在返回的对象doc中有一个名为b的字段,我希望它是baseID而不是b

Now, I want to rename a field name while retrieving only (not in the DB), for example with the above code, I have a field named b in the returned object doc I want it to be baseID instead of b

有什么办法吗?

注意:我无法对检索到的对象doc进行操作来重命名JSON密钥重命名之类的字段.我希望查询它,而MongoDB也会一样.

Note: I cannot take action on the retrieved object doc to rename field like JSON key renaming. I want it to be queried and MongoDB will the same.

推荐答案

使用升级 MongoDB服务器实例到> = 2.1).

Use aggregate framework of MonogDB (But you need to upgrade the MongoDB server instance to >= 2.1).

以下是上面示例的内容

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');
db.open(function (err, db) {
    if (err) console.dir(err);
    db.createCollection('simple_limit_skip_find_one_query', function (err, collection) {
        if (err) console.dir(err);

        collection.insert([{ a: 1, b: 1 }, { a: 2, b: 2 }, { a: 3, b: 3}], { w: 1 }, function (err, doc) {
            if (err) console.dir(err);

            collection.aggregate([
            { $project: {
                a: 1,
                _id:0,
                baseID: "$b"
            }
            }
          ], function (err, doc) {
              if (err) console.dir(err);
              console.log(doc);
          });
        });
    });
});

输出:

[ { a: 1, baseID: 1 },
  { a: 2, baseID: 2 },
  { a: 3, baseID: 3 }]

这篇关于在使用MongoDB-Node.JS本机驱动器通过查询从MongoDB中获取字段时,如何重命名/别名字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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