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

q5iwbnjs  于 2024-01-08  发布在  Go
关注(0)|答案(2)|浏览(201)

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

  1. /* 0 */
  2. {
  3. "name" : "CIMA",
  4. "_id" : ObjectId("563ff96bb61f6b2c0e82f93e"),
  5. "subjects" : [],
  6. "__v" : 0
  7. }
  8. /* 1 */
  9. {
  10. "name" : "TESTDDD",
  11. "_id" : ObjectId("563ffa1db61f6b2c0e82f940"),
  12. "subjects" : [],
  13. "__v" : 0
  14. }
  15. /* 2 */
  16. {
  17. "name" : "testing new thing",
  18. "_id" : ObjectId("564cbc605adf343c0dc49e95"),
  19. "subjects" : [],
  20. "__v" : 0
  21. }
  22. /* 3 */
  23. {
  24. "name" : "asdfsdf",
  25. "_id" : ObjectId("564cc0f45adf343c0dc49e96"),
  26. "subjects" : [],
  27. "__v" : 0
  28. }
  29. /* 4 */
  30. {
  31. "name" : "asdfasdfasdfasdfasdfsd",
  32. "_id" : ObjectId("564ced6ed68ef5d00d5ad4db"),
  33. "subjects" : [],
  34. "__v" : 0
  35. }
  36. /* 5 */
  37. {
  38. "__v" : 0,
  39. "_id" : ObjectId("563ff5de8e3ae0ce0d5f65a7"),
  40. "name" : "TEST EDITED",
  41. "subjects" : []
  42. }
  43. /* 6 */
  44. {
  45. "__v" : 0,
  46. "_id" : ObjectId("563566b572b65918095f1db3"),
  47. "name" : "TEST COURSE",
  48. "subjects" : []
  49. }
  50. /* 7 */
  51. {
  52. "name" : "sub one",
  53. "_id" : ObjectId("564d017d4e13a8640e6b1738"),
  54. "subjects" : [],
  55. "__v" : 0
  56. }
  57. /* 8 */
  58. {
  59. "__v" : 0,
  60. "_id" : ObjectId("563febb75a9909ae0d25d025"),
  61. "name" : "testing course",
  62. "subjects" : []
  63. }
  64. /* 9 */
  65. {
  66. "__v" : 0,
  67. "_id" : ObjectId("563ff95ab61f6b2c0e82f93d"),
  68. "name" : "324234",
  69. "subjects" : []
  70. }
  71. /* 10 */
  72. {
  73. "__v" : 0,
  74. "_id" : ObjectId("563ff8d2b61f6b2c0e82f93b"),
  75. "name" : "asdfasfd",
  76. "subjects" : [
  77. {
  78. "name" : "some suject",
  79. "_id" : ObjectId("564d05842582c8fe0eb4362d"),
  80. "topics" : []
  81. }
  82. ]
  83. }
  84. /* 11 */
  85. {
  86. "__v" : 0,
  87. "_id" : ObjectId("563ff8fbb61f6b2c0e82f93c"),
  88. "name" : "asfdasfasfd",
  89. "subjects" : [
  90. {
  91. "name" : "asdfasdfasdfasdfasdfsd",
  92. "_id" : ObjectId("564d05c82582c8fe0eb4362e"),
  93. "topics" : []
  94. }
  95. ]
  96. }
  97. /* 12 */
  98. {
  99. "__v" : 0,
  100. "_id" : ObjectId("563ff735b61f6b2c0e82f938"),
  101. "name" : "test",
  102. "subjects" : [
  103. {
  104. "_id" : ObjectId("564d04e32582c8fe0eb4362b"),
  105. "name" : "test subject",
  106. "topics" : []
  107. },
  108. {
  109. "name" : "test subject edite",
  110. "_id" : ObjectId("564d46a4adcf28580f631eca"),
  111. "topics" : []
  112. },
  113. {
  114. "_id" : ObjectId("564d46b4adcf28580f631ecb"),
  115. "name" : "test subject edited",
  116. "topics" : []
  117. },
  118. {
  119. "name" : "test subject edite again",
  120. "_id" : ObjectId("564d4759d6fe04640f99701a"),
  121. "topics" : []
  122. },
  123. {
  124. "_id" : ObjectId("564d4793ef24f5670f62ba22"),
  125. "name" : "test subject yet again",
  126. "topics" : []
  127. },
  128. {
  129. "name" : "test subject edited TWO TIMES",
  130. "_id" : ObjectId("564d4989ef24f5670f62ba23"),
  131. "topics" : []
  132. }
  133. ]
  134. }

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

  1. var _ = require('underscore');
  2. module.exports = {
  3. toClient: transformCollection
  4. }
  5. function transformCollection(theObject) {
  6. var result = null, object = {};
  7. if(theObject instanceof Array) {
  8. for(var i = 0; i < theObject.length; i++) {
  9. theObject[i] = process.nextTick(function () {transformCollection(theObject[prop]);}); // <----Assignment
  10. }
  11. }
  12. else
  13. {
  14. for (var prop in theObject) {
  15. if (prop == '_id') {
  16. theObject.id = theObject._id; // <----Typo
  17. delete theObject._id;
  18. delete theObject.__v;
  19. }
  20. else if (theObject[prop] instanceof Array) {
  21. theObject[prop] = process.nextTick(function () {transformCollection(theObject[prop]);}); // <----Assignment
  22. }
  23. }
  24. }
  25. return theObject;
  26. }


被称为:

  1. courseModel.find(function(err,courses){
  2. if(err){
  3. res.json(error.dbRetrievalError);
  4. }
  5. else
  6. {
  7. res.json(utils.toClient(courses));
  8. }
  9. })


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

ocebsuys

ocebsuys1#

看起来你在递归的结果中缺少了一些赋值。在你的for-in中也有一个错别字,你设置了object.id而不是theObject.id
这个有用吗

  1. function transformCollection(theObject) {
  2. var result = null, object = {};
  3. if(theObject instanceof Array) {
  4. for(var i = 0; i < theObject.length; i++) {
  5. theObject[i] = transformCollection(theObject[i]); // <----Assignment
  6. }
  7. }
  8. else
  9. {
  10. for (var prop in theObject) {
  11. if (prop == '_id') {
  12. theObject.id = theObject._id; // <----Typo
  13. delete theObject._id;
  14. }
  15. else if (theObject[prop] instanceof Array) {
  16. theObject[prop] = transformCollection(theObject[prop]); // <----Assignment
  17. }
  18. }
  19. }
  20. return theObject;
  21. }

字符串

展开查看全部
gkl3eglg

gkl3eglg2#

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

  1. function cleanupId(doc, ret) {
  2. if (ret.hasOwnProperty('_id')) {
  3. ret.id = doc._id;
  4. delete ret._id;
  5. }
  6. for (let prop in doc) {
  7. if (Array.isArray(ret[prop])) {
  8. doc[prop].forEach((elem, index) => {
  9. cleanupId(doc[prop][index], ret[prop][index]);
  10. });
  11. }
  12. }
  13. }

字符串

展开查看全部

相关问题