Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. db.coll.update(
  2. { article: 100500 },
  3. {
  4. $set: { a: 555 },
  5. $pull: { arr: { t: { $lt: 20 } } }
  6. }
  7. );
  8.  
  9. {
  10. article: 100500,
  11. a: 400,
  12. arr: [
  13. { t: 30, b: 12, n: 90 },
  14. { t: 10, b: 16, n: 60 }
  15. ]
  16. }
  17.  
  18. {
  19. article: 100500,
  20. a: 555, /* Тут было: 400 */
  21. arr: [
  22. { t: 30, b: 777, n: 90 },
  23. /* Тут был элемент массива */
  24. ]
  25. }
  26.  
  27. {
  28. "_id" : ObjectId("56b71025973d202a52a5e650"),
  29. "article" : 100500,
  30. "a" : 400,
  31. "arr" : [
  32. { "t" : 30, "b" : 12, "n" : 90 },
  33. { "t" : 20, "b" : 16, "n" : 60 }
  34. ]
  35. },
  36. {
  37. "_id" : ObjectId("56b7102b973d202a52a5e651"),
  38. "article" : 100500,
  39. "a" : 400,
  40. "arr" : [
  41. { "t" : 0, "b" : 12, "n" : 90 },
  42. { "t" : 10, "b" : 16, "n" : 60 }
  43. ]
  44. },
  45. {
  46. "_id" : ObjectId("56b710d4973d202a52a5e652"),
  47. "article" : 100500,
  48. "a" : 400,
  49. "arr" : [
  50. { "t" : 30, "b" : 12, "n" : 90 },
  51. { "t" : 27, "b" : 16, "n" : 32 }
  52. ]
  53. }
  54.  
  55. var requests = [];
  56.  
  57.  
  58. db.collection.aggregate([
  59. { "$project": {
  60. "deleteElements": {
  61. "$filter": {
  62. "input": "$arr",
  63. "as": "del",
  64. "cond": { "$lt": [ "$$del.t", 12 ] }
  65. }
  66. },
  67. "updateElements": {
  68. "$filter": {
  69. "input": "$arr",
  70. "as": "upd",
  71. "cond": {
  72. "$and": [
  73. { "$gte": [ "$$upd.t", 12 ] },
  74. { "$eq": [ "$$upd.n", 90 ] }
  75. ]
  76. }
  77. }
  78. }
  79. }}
  80. ]).forEach(function(document) {
  81. document.deleteElements.forEach(function(element) {
  82. requests.push(
  83. {
  84. "updateOne": {
  85. "filter": {
  86. "_id": document._id,
  87. "arr.t": element.t
  88. },
  89. "update": { "$pull": { "arr": element } }
  90. }
  91. }
  92. );
  93. });
  94. document.updateElements.forEach(function(element) {
  95. requests.push(
  96. {
  97. "updateOne": {
  98. "filter": {
  99. "_id": document._id,
  100. "arr.t": element.n
  101. },
  102. "update": { "$set": { "arr.$.b": 777 } }
  103. }
  104. }
  105. );
  106. });
  107. requests.push(
  108. {
  109. "updateOne": {
  110. "filter": { "_id": document._id },
  111. "update": { "$set": { "a": 555 } }
  112. }
  113. }
  114. );
  115. })
  116.  
  117. db.collection.bulkWrite(requests)
  118.  
  119. db.collection.find({"article": 100500}).forEach(function(document) {
  120. document.arr.filter(function(arr) {
  121. return arr.t < 12;
  122. }).forEach(function(element) {
  123. requests.push(
  124. {
  125. "updateOne": {
  126. "filter": {
  127. "_id": document._id,
  128. "arr.t": { "$lt": 12 }},
  129. "update": { "$pull": { "arr": element } }
  130. }
  131. }
  132. );
  133. });
  134. document.arr.filter(function(arr) {
  135. return arr.n === 90;
  136. }).forEach(function(element) {
  137. requests.push(
  138. {
  139. "updateOne": {
  140. "filter": { "_id": document._id, "arr.n": 90 },
  141. "update": { "$set": { "arr.$.b": 777 } }
  142. }
  143. }
  144. );
  145. });
  146. requests.push(
  147. {
  148. "updateOne": {
  149. "filter": { "_id": document._id },
  150. "update": { "$set": { "a": 555 } }
  151. }
  152. }
  153. );
  154. })
  155.  
  156. db.collection.bulkWrite(requests);
  157.  
  158. var bulk = db.collection.initializeOrderedBulkOp();
  159. var count = 0;
  160. db.collection.find({"article": 100500}).forEach(function(document) {
  161. document.arr.filter(function(arr) {
  162. return arr.t < 12;
  163. }).forEach(function(element) {
  164. bulk.find({ "_id": document._id, "arr.t": { "$lt": 12 } } ).updateOne({
  165. "$pull": { "arr": element }
  166. });
  167. count++;
  168. });
  169. document.arr.filter(function(arr) {
  170. return arr.n === 90;
  171. }).forEach(function(element) {
  172. bulk.find({ "_id": document._id, "arr.n": 90 }).updateOne({
  173. "$set": { "arr.$.b": 777 }
  174. });
  175. count++;
  176. });
  177. bulk.find( { "_id": document._id } ).updateOne( { "$set": { "a": 555 } } );
  178. count++;
  179. if (count % 1000 === 0) {
  180. // Выпольнить после 1000 операции
  181. bulk.execute();
  182. bulk = db.collection.initializeOrderedBulkOp();
  183. }
  184. })
  185.  
  186. // Очистить очереди
  187. if (count > 0) { bulk.execute(); }
  188.  
  189. {
  190. "_id" : ObjectId("56b71025973d202a52a5e650"),
  191. "article" : 100500,
  192. "a" : 555,
  193. "arr" : [
  194. { "t" : 30, "b" : 777, "n" : 90 },
  195. { "t" : 20, "b" : 16, "n" : 60 }
  196. ]
  197. }
  198. {
  199. "_id" : ObjectId("56b7102b973d202a52a5e651"),
  200. "article" : 100500,
  201. "a" : 555,
  202. "arr" : [ ]
  203. }
  204. {
  205. "_id" : ObjectId("56b710d4973d202a52a5e652"),
  206. "article" : 100500,
  207. "a" : 555,
  208. "arr" : [
  209. { "t" : 30, "b" : 777, "n" : 90 },
  210. { "t" : 27, "b" : 16, "n" : 32 }
  211. ]
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement