Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- db.coll.update(
- { article: 100500 },
- {
- $set: { a: 555 },
- $pull: { arr: { t: { $lt: 20 } } }
- }
- );
- {
- article: 100500,
- a: 400,
- arr: [
- { t: 30, b: 12, n: 90 },
- { t: 10, b: 16, n: 60 }
- ]
- }
- {
- article: 100500,
- a: 555, /* Тут было: 400 */
- arr: [
- { t: 30, b: 777, n: 90 },
- /* Тут был элемент массива */
- ]
- }
- {
- "_id" : ObjectId("56b71025973d202a52a5e650"),
- "article" : 100500,
- "a" : 400,
- "arr" : [
- { "t" : 30, "b" : 12, "n" : 90 },
- { "t" : 20, "b" : 16, "n" : 60 }
- ]
- },
- {
- "_id" : ObjectId("56b7102b973d202a52a5e651"),
- "article" : 100500,
- "a" : 400,
- "arr" : [
- { "t" : 0, "b" : 12, "n" : 90 },
- { "t" : 10, "b" : 16, "n" : 60 }
- ]
- },
- {
- "_id" : ObjectId("56b710d4973d202a52a5e652"),
- "article" : 100500,
- "a" : 400,
- "arr" : [
- { "t" : 30, "b" : 12, "n" : 90 },
- { "t" : 27, "b" : 16, "n" : 32 }
- ]
- }
- var requests = [];
- db.collection.aggregate([
- { "$project": {
- "deleteElements": {
- "$filter": {
- "input": "$arr",
- "as": "del",
- "cond": { "$lt": [ "$$del.t", 12 ] }
- }
- },
- "updateElements": {
- "$filter": {
- "input": "$arr",
- "as": "upd",
- "cond": {
- "$and": [
- { "$gte": [ "$$upd.t", 12 ] },
- { "$eq": [ "$$upd.n", 90 ] }
- ]
- }
- }
- }
- }}
- ]).forEach(function(document) {
- document.deleteElements.forEach(function(element) {
- requests.push(
- {
- "updateOne": {
- "filter": {
- "_id": document._id,
- "arr.t": element.t
- },
- "update": { "$pull": { "arr": element } }
- }
- }
- );
- });
- document.updateElements.forEach(function(element) {
- requests.push(
- {
- "updateOne": {
- "filter": {
- "_id": document._id,
- "arr.t": element.n
- },
- "update": { "$set": { "arr.$.b": 777 } }
- }
- }
- );
- });
- requests.push(
- {
- "updateOne": {
- "filter": { "_id": document._id },
- "update": { "$set": { "a": 555 } }
- }
- }
- );
- })
- db.collection.bulkWrite(requests)
- db.collection.find({"article": 100500}).forEach(function(document) {
- document.arr.filter(function(arr) {
- return arr.t < 12;
- }).forEach(function(element) {
- requests.push(
- {
- "updateOne": {
- "filter": {
- "_id": document._id,
- "arr.t": { "$lt": 12 }},
- "update": { "$pull": { "arr": element } }
- }
- }
- );
- });
- document.arr.filter(function(arr) {
- return arr.n === 90;
- }).forEach(function(element) {
- requests.push(
- {
- "updateOne": {
- "filter": { "_id": document._id, "arr.n": 90 },
- "update": { "$set": { "arr.$.b": 777 } }
- }
- }
- );
- });
- requests.push(
- {
- "updateOne": {
- "filter": { "_id": document._id },
- "update": { "$set": { "a": 555 } }
- }
- }
- );
- })
- db.collection.bulkWrite(requests);
- var bulk = db.collection.initializeOrderedBulkOp();
- var count = 0;
- db.collection.find({"article": 100500}).forEach(function(document) {
- document.arr.filter(function(arr) {
- return arr.t < 12;
- }).forEach(function(element) {
- bulk.find({ "_id": document._id, "arr.t": { "$lt": 12 } } ).updateOne({
- "$pull": { "arr": element }
- });
- count++;
- });
- document.arr.filter(function(arr) {
- return arr.n === 90;
- }).forEach(function(element) {
- bulk.find({ "_id": document._id, "arr.n": 90 }).updateOne({
- "$set": { "arr.$.b": 777 }
- });
- count++;
- });
- bulk.find( { "_id": document._id } ).updateOne( { "$set": { "a": 555 } } );
- count++;
- if (count % 1000 === 0) {
- // Выпольнить после 1000 операции
- bulk.execute();
- bulk = db.collection.initializeOrderedBulkOp();
- }
- })
- // Очистить очереди
- if (count > 0) { bulk.execute(); }
- {
- "_id" : ObjectId("56b71025973d202a52a5e650"),
- "article" : 100500,
- "a" : 555,
- "arr" : [
- { "t" : 30, "b" : 777, "n" : 90 },
- { "t" : 20, "b" : 16, "n" : 60 }
- ]
- }
- {
- "_id" : ObjectId("56b7102b973d202a52a5e651"),
- "article" : 100500,
- "a" : 555,
- "arr" : [ ]
- }
- {
- "_id" : ObjectId("56b710d4973d202a52a5e652"),
- "article" : 100500,
- "a" : 555,
- "arr" : [
- { "t" : 30, "b" : 777, "n" : 90 },
- { "t" : 27, "b" : 16, "n" : 32 }
- ]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement