javascript - How to convert String to Array in MongoDB? - Stack Overflow

I'm stuck at the situation when the type of an object was changed. How can I convert this:{ "

I'm stuck at the situation when the type of an object was changed.

How can I convert this:

{ 
"_id" : NumberLong(257),
"address" : "street  Street, house 50, appartment 508, floor 5"
}

to this:

{ 
"_id" : NumberLong(257),
 "userAddressList" : [{
        "street" : "Street",
        "house" : "50",
        "building" : "",
        "appartment " : NumberLong(508),
        "entrance" : NumberLong(0),
        "floor" : NumberLong(5),
        "inter" : ""
    }]
}

using mongo shell?

I need to convert about 350 entries, hope it can be done by the script.

I'm stuck at the situation when the type of an object was changed.

How can I convert this:

{ 
"_id" : NumberLong(257),
"address" : "street  Street, house 50, appartment 508, floor 5"
}

to this:

{ 
"_id" : NumberLong(257),
 "userAddressList" : [{
        "street" : "Street",
        "house" : "50",
        "building" : "",
        "appartment " : NumberLong(508),
        "entrance" : NumberLong(0),
        "floor" : NumberLong(5),
        "inter" : ""
    }]
}

using mongo shell?

I need to convert about 350 entries, hope it can be done by the script.

Share Improve this question edited Feb 6, 2016 at 16:23 user5892393 176 bronze badges asked Apr 7, 2015 at 20:37 D. L.D. L. 912 silver badges7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You could try this:

db.collection.find().forEach( function (x) {   
    lines = x.address.split(",");
    obj = {};
    userAddressList = [];
    lines.forEach( function (address){
        addressArray = address.replace(/^\s\s*/, '').replace(/\s\s*$/, '').split(" ");
        obj[addressArray[0]] = !isNaN(parseInt(addressArray[1])) ? parseInt(addressArray[1]) : addressArray[1];        
    });
    obj.building = "";
    obj.inter = "";
    userAddressList.push(obj);
    x.userAddressList = userAddressList; // convert field to string
    db.collection.save(x);
});

You can also use the MongoDB aggregation framework to convert the given document into the desired format. You'll want to use the $addFields, $regexFind, and $convert operators to extract, transform, and load the new fields.

The following aggregation pipeline should give you the desired results:

db.collection.aggregate([
  { $addFields: {
      userAddressList: {
        $let: {
          vars: {
            street: {
              $regexFind: { input: "$address", regex: /(\w+)\s+Street/ }
            },
            house: {
              $regexFind: { input: "$address", regex: /house\s+(\d+)/ }
            },
            appartment: {
              $regexFind: { input: "$address", regex: /appartment\s+(\d+)/ }
            },
            floor: {
              $regexFind: { input: "$address", regex: /floor\s+(\d+)/ }
            }
          },
          in: [
            {
              street: "$$street.match",
              house: "$$house.match",
              building: "",
              appartment: {
                $convert: {
                  input: "$$appartment.match",
                  to: "long",
                  onError: NumberLong(0),
                }
              },
              entrance: NumberLong(0),
              floor: {
                $convert: {
                  input: "$$floor.match",
                  to: "long",
                  onError: NumberLong(0)
                }
              },
              inter: ""
            }
          ]
        }
      }
    } },
    { $project: { address: 0 } }
]);

you can use a foreach in the update like this

db.test.find( { } ).forEach( function (x) {
x.userAddressList = x.address.split(" "); db.test.save(x); });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744580147a4581986.html

相关推荐

  • javascript - How to convert String to Array in MongoDB? - Stack Overflow

    I'm stuck at the situation when the type of an object was changed. How can I convert this:{ "

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信