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

q5iwbnjs  于 9个月前  发布在  Go
关注(0)|答案(2)|浏览(119)

我有一个典型的 Mongoose 示意图集合,如下所示:

/* 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" : []
        }
    ]
}

字符串
现在我需要客户端接收这个,所有的_id都替换为id,不管文档在嵌套结构中处于什么级别。所以我写了一个转换递归函数,如下所示:

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


被称为:

courseModel.find(function(err,courses){
            if(err){
                res.json(error.dbRetrievalError);
            }
            else
            {
                res.json(utils.toClient(courses));
            }
        })


这里有几个问题:
1.这是实现这一目标的正确方法吗?
1.如果是,那么任何人都可以指出我的递归函数在正确的方向?我得到了一个最大调用堆栈大小超过错误。

ocebsuys

ocebsuys1#

看起来你在递归的结果中缺少了一些赋值。在你的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;
}

字符串

gkl3eglg

gkl3eglg2#

我也遇到了同样的问题,并编写了这个递归函数来完成这项工作:

function cleanupId(doc, ret) {
  if (ret.hasOwnProperty('_id')) {
    ret.id = doc._id;
    delete ret._id;
  }
  
  for (let prop in doc) {
    if (Array.isArray(ret[prop])) {
      doc[prop].forEach((elem, index) => {
        cleanupId(doc[prop][index], ret[prop][index]);
      });
    }
  }
}

字符串

相关问题