Within my mongo collection, there is a string labeled range = "2000 - 3000 INR". I am aiming to convert this string into an object where range = { min: 2000, max: 3000, currency: "INR"}. The structure of my collection is as follows:
{
"id": "ABC",
"places": [
{
"isSearchable": true,
"locations": [
{
"id": "DEL",
"loc": {
"range": "2000-5000 INR"
}
},
{
"id": "BLG",
"loc": {
"range": "1000-3000 INR"
}
}
]
}
]
}
I am aware that a JavaScript function can be implemented in the mongo shell to achieve this, but I am uncertain about the next steps. My initial attempt looks like:
cursor = db.getCollection('locations').find({"places.isSerachable":true});
// Iterate over Cursor object
while(cursor.hasNext()) {
item = cursor.next();
keys = Object.keys(item);
// Iterate over MongoDB doc fields
for (i=0; i-keys.length; i++) {
let field = keys[i];
print( "field:", field, "---",item[keys[i]]);
}
}
Your guidance on this matter would be greatly appreciated!