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 badges2 Answers
Reset to default 6You 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);
});