转换mongodb文档和子文档,在递归循环中用id替换_id

时间:2015-11-19 21:51:41

标签: node.js mongodb recursion mongoose

我有一个典型的mongoose原理图集合,如下所示:

timerSource created
timerSource time set
timerSource event set
timerSource resumed

现在我需要客户端接收这个,所有_id替换为id,无论文档在嵌套结构中的哪个级别。所以我写了一个像这样的转换递归函数:

/* 0 */
{
    "name" : "CIMA",
    "_id" : ObjectId("563ff96bb61f6b2c0e82f93e"),
    "subjects" : [],
    "__v" : 0
}

/* 1 */
{
    "name" : "TESTDDD",
    "_id" : ObjectId("563ffa1db61f6b2c0e82f940"),
    "subjects" : [],
    "__v" : 0
}

/* 2 */
{
    "name" : "testing new thing",
    "_id" : ObjectId("564cbc605adf343c0dc49e95"),
    "subjects" : [],
    "__v" : 0
}

/* 3 */
{
    "name" : "asdfsdf",
    "_id" : ObjectId("564cc0f45adf343c0dc49e96"),
    "subjects" : [],
    "__v" : 0
}

/* 4 */
{
    "name" : "asdfasdfasdfasdfasdfsd",
    "_id" : ObjectId("564ced6ed68ef5d00d5ad4db"),
    "subjects" : [],
    "__v" : 0
}

/* 5 */
{
    "__v" : 0,
    "_id" : ObjectId("563ff5de8e3ae0ce0d5f65a7"),
    "name" : "TEST EDITED",
    "subjects" : []
}

/* 6 */
{
    "__v" : 0,
    "_id" : ObjectId("563566b572b65918095f1db3"),
    "name" : "TEST COURSE",
    "subjects" : []
}

/* 7 */
{
    "name" : "sub one",
    "_id" : ObjectId("564d017d4e13a8640e6b1738"),
    "subjects" : [],
    "__v" : 0
}

/* 8 */
{
    "__v" : 0,
    "_id" : ObjectId("563febb75a9909ae0d25d025"),
    "name" : "testing course",
    "subjects" : []
}

/* 9 */
{
    "__v" : 0,
    "_id" : ObjectId("563ff95ab61f6b2c0e82f93d"),
    "name" : "324234",
    "subjects" : []
}

/* 10 */
{
    "__v" : 0,
    "_id" : ObjectId("563ff8d2b61f6b2c0e82f93b"),
    "name" : "asdfasfd",
    "subjects" : [ 
        {
            "name" : "some suject",
            "_id" : ObjectId("564d05842582c8fe0eb4362d"),
            "topics" : []
        }
    ]
}

/* 11 */
{
    "__v" : 0,
    "_id" : ObjectId("563ff8fbb61f6b2c0e82f93c"),
    "name" : "asfdasfasfd",
    "subjects" : [ 
        {
            "name" : "asdfasdfasdfasdfasdfsd",
            "_id" : ObjectId("564d05c82582c8fe0eb4362e"),
            "topics" : []
        }
    ]
}

/* 12 */
{
    "__v" : 0,
    "_id" : ObjectId("563ff735b61f6b2c0e82f938"),
    "name" : "test",
    "subjects" : [ 
        {
            "_id" : ObjectId("564d04e32582c8fe0eb4362b"),
            "name" : "test subject",
            "topics" : []
        }, 
        {
            "name" : "test subject edite",
            "_id" : ObjectId("564d46a4adcf28580f631eca"),
            "topics" : []
        }, 
        {
            "_id" : ObjectId("564d46b4adcf28580f631ecb"),
            "name" : "test subject edited",
            "topics" : []
        }, 
        {
            "name" : "test subject edite again",
            "_id" : ObjectId("564d4759d6fe04640f99701a"),
            "topics" : []
        }, 
        {
            "_id" : ObjectId("564d4793ef24f5670f62ba22"),
            "name" : "test subject yet again",
            "topics" : []
        }, 
        {
            "name" : "test subject edited TWO TIMES",
            "_id" : ObjectId("564d4989ef24f5670f62ba23"),
            "topics" : []
        }
    ]
}

并称为:

var _ = require('underscore');

module.exports = {
    toClient: transformCollection
}

function transformCollection(theObject) {
var result = null, object = {};

if(theObject instanceof Array) {
    for(var i = 0; i < theObject.length; i++) {
        theObject[i] = process.nextTick(function () {transformCollection(theObject[prop]);}); // <----Assignment
    }
}
else
{
    for (var prop in theObject) {
        if (prop == '_id') {
            theObject.id = theObject._id; // <----Typo
            delete theObject._id;
            delete theObject.__v;
        }
        else if (theObject[prop] instanceof Array) {
            theObject[prop] = process.nextTick(function () {transformCollection(theObject[prop]);}); // <----Assignment
        }
    }
}
return theObject;
   }

这里有几个问题:

  1. 这是实现这一目标的正确方法吗?

  2. 如果是,那么任何人都可以将我的递归函数指向正确的方向吗?一世 我的最大调用堆栈大小超出错误。

1 个答案:

答案 0 :(得分:0)

看起来你错过了递归结果中的几项任务。在你的for-in中还有一个拼写错误,你设置object.id而不是theObject.id

这有用吗?

function transformCollection(theObject) {
    var result = null, object = {};

    if(theObject instanceof Array) {
        for(var i = 0; i < theObject.length; i++) {
            theObject[i] = transformCollection(theObject[i]); // <----Assignment
        }
    }
    else
    {
        for (var prop in theObject) {
            if (prop == '_id') {
                theObject.id = theObject._id; // <----Typo
                delete theObject._id;
            }
            else if (theObject[prop] instanceof Array) {
                theObject[prop] = transformCollection(theObject[prop]); // <----Assignment
            }
        }
    }
    return theObject;
}