0

I want to remove the items only contain white spaces.

eg: " ", " "

I want to remove them from buy_items array.

How to do it ? thanks

sample document

{
  "_id": "mark jordan",
  "records": [
    {
      "date": new Date("2012-05-04T08:00:00+0800"),
      "buy_items": [
        "6829           ",
        "               ",
        "68600          ",
        "8830           "
      ]
    },
    {
      "date": new Date("2011-02-10T08:00:00+0800"),
      "buy_items": [
        "4659 ",
        "     ",
        "4660 "
      ]
    },
    {
      "date": new Date("2011-01-09T08:00:00+0800"),
      "buy_items": [
        "     ",
        "4659 "
      ]
    }
  ]
}
newBike
  • 14,385
  • 29
  • 109
  • 192

2 Answers2

0

You basically have the same situation as described in this question: How to check if a line is blank using regex

It's simply a matter of applying it to the search through the array in monogodb:

http://docs.mongodb.org/manual/reference/operator/query/regex/

Community
  • 1
  • 1
Deminth
  • 75
  • 7
0

I believe your best approach is going to be iterating through each element of the arrays. If a non-white space character is detected within the inner array, move on to the next element of the outer array; if all elements of the inner array are scanned and no non-white space character occurs, this element will be removed.

Removing an array element can be done by moving each subsequent element in the array up by one space and then reducing the array size by 1 (if array size is something you are explicitly tracking).

This is a fairly processor/time intensive process, so unnecessary executions of this procedure should be avoided. You can also trim processing time by scanning each element for non-blank characters prior to shifting them and removing all white space only elements in one pass if necessary.