newBike
newBike

Reputation: 15002

How could I sort the items of a array and update to the original document

I want to sort the array records and remove the " ", " " items only containing white space.

How to get it with aggregation.

This is my rough idea, but not working

pipeline = [
  {'$unwind': '$records'}
  { '$sort': 
      "records.date": 1 
  }
]  
db[source_collection].runCommand('aggregate',
  pipeline: pipeline
  allowDiskUse: true)    

Sample document format

{
  "_id": "0005dc158e68b0a2e4f2a8d96ca733f1",
  "records": [
    {
      "date": new Date("2012-12-04T08:00:00+0800"),
      "items": [
        "               ",
        "4659           ",
        "463            "
      ]
    },
    {
      "date": new Date("2012-12-01T08:00:00+0800"),
      "items": [
        "               ",
        "4658           "
      ]
    },
    {
      "date": new Date("2012-10-18T08:00:00+0800"),
      "items": [
        "               ",
        "78900          "
      ]
    },
    {
      "date": new Date("2012-08-07T08:00:00+0800"),
      "items": [
        "               ",
        "5230           "
      ]
    }
  ],
  "gender": "F",
  "birthday": new Date("1990-01-01T08:00:00+0800"),
  "birthday_type": "D"
}

Upvotes: 0

Views: 36

Answers (1)

Neo-coder
Neo-coder

Reputation: 7840

For sorting records array with date check following aggregation query :

db.collectionName.aggregate({"$unwind":"$records"},{"$sort":{"records.date":1}}).pretty() 

And For changing items array value you need to write programming code using mongo Bulk I write following script which update every items array value check as below :

var bulk = db.collectionName.initializeOrderedBulkOp();
var counter = 0;
db.collectionName.find({
        "records": {
            "$exists": true
        }
    }).forEach(function(data) {
        for(var ii = 0; ii < data.records.length; ii++) {
            var items = data.records[ii].items;
            var arr = [];
            for(var j = 0; j < items.length; j++) {
                arr[j] = data.records[ii].items[j].trim()
            }
            var updoc = {
                "$set": {}
            };
            var updateKey = "records." + ii + ".items";
            updoc["$set"][updateKey] = arr;
            // queue the update
            bulk.find({
                "_id": data._id
            }).update(updoc);
            counter++;
            // Drain and re-initialize every 1000 update statements
            if(counter % 1000 == 0) {
                bulk.execute();
                bulk = db.collectionName.initializeOrderedBulkOp();
            }
        }
    })
    // Add the rest in the queue
if(counter % 1000 != 0) bulk.execute();

Upvotes: 1

Related Questions