Depth of nested fields in MongoDB

var spaceChar = ‘ ‘; function getParentNames(jsstr, n, pos) { if (n <= 1) { return “”; } else { var spacesBeforeParent = jsstr.substr(pos, n) + ‘”‘; var indexParent = jsstr.lastIndexOf(spacesBeforeParent, pos); if (indexParent > 0) { var posParent = indexParent + spacesBeforeParent.length; var endParent = jsstr.indexOf(‘”‘, posParent); var parentName = “”; if (n > 2) { parentName = getParentNames(jsstr, n-1, indexParent) + “.”; } return parentName + jsstr.substr(posParent, endParent – posParent); } else { return “”; } } }

db.<COLLECTION NAME >.find().forEach(function(doc) { // Get a JSON representation of the document with a single space for each level var jsstr = JSON.stringify(doc, null, 1); var pos = jsstr.search(/”<FIELD TO FIND >”/); var count = 0; if (pos > 1) { while (jsstr[–pos] == spaceChar) count++; } print(doc._id + ” ” + count + ” ” + getParentNames(jsstr, count, pos)); });


> var spaceChar = ‘ ‘;
> function getParentNames(jsstr, n, pos) {
… if (n <= 1) {
… return “”;
… } else {
… var spacesBeforeParent = jsstr.substr(pos, n) + ‘”‘;
… var indexParent = jsstr.lastIndexOf(spacesBeforeParent, pos);
… if (indexParent > 0) {
… var posParent = indexParent + spacesBeforeParent.length;
… var endParent = jsstr.indexOf(‘”‘, posParent);
… var parentName = “”;
… if (n > 2) {
… parentName = getParentNames(jsstr, n-1, indexParent) + “.”;
… }
… return parentName + jsstr.substr(posParent, endParent – posParent);
… } else {
… return “”;
… }
… }
… }



> db.mapsdata.find().forEach(function(doc) {
…     // Get a JSON representation of the document with a single space for each level
…     var jsstr = JSON.stringify(doc, null, 1);
…     var pos = jsstr.search(/”PlaceId”/);
…     var count = 0;
…     if (pos > 1) {
…         while (jsstr[–pos] == spaceChar) count++;
…     }
…     print(doc._id + ” ” + count + ” ” + getParentNames(jsstr, count, pos));
… });
5c444605f76744a01b4cf263 2 Identity
5c444609f76744a01b4cf268 2 Identity
5c44460cf76744a01b4cf26d 2 Identity





> db.mapsdata.find().forEach(function(doc) {
…     // Get a JSON representation of the document with a single space for each level
…     var jsstr = JSON.stringify(doc, null, 1);
…     var pos = jsstr.search(/”BaseName”/);
…     var count = 0;
…     if (pos > 1) {
…         while (jsstr[–pos] == spaceChar) count++;
…     }
…     print(doc._id + ” ” + count + ” ” + getParentNames(jsstr, count, pos));
… });
5c444605f76744a01b4cf263 7 LocationList.Location.Address.ParsedList.Parsed.StreetName
5c444609f76744a01b4cf268 7 LocationList.Location.Address.ParsedList.Parsed.StreetName
5c44460cf76744a01b4cf26d 7 LocationList.Location.Address.ParsedList.Parsed.StreetName







> db.mapsdata.find().forEach(function(doc) {
…     // Get a JSON representation of the document with a single space for each level
…     var jsstr = JSON.stringify(doc, null, 1);
…     var pos = jsstr.search(/”#text”/);
…     var count = 0;
…     if (pos > 1) {
…         while (jsstr[–pos] == spaceChar) count++;
…     }
…     print(doc._id + ” ” + count + ” ” + getParentNames(jsstr, count, pos));
… });
5c444605f76744a01b4cf263 5 LocationList.Location.MapReferenceList.MapReference
5c444609f76744a01b4cf268 8 LocationList.Location.Address.ParsedList.Parsed.StreetName.StreetType
5c44460cf76744a01b4cf26d 6 LocationList.Location.Address.UnparsedList.Unparsed




> db.mapsdata.aggregate({$project:{“LocationList.Location.Address.ParsedList.Parsed.StreetName.BaseName”:1}})
{ “_id” : ObjectId(“5c444605f76744a01b4cf263”), “LocationList” : { “Location” : { “Address” : { “ParsedList” : { “Parsed” : { “StreetName” : { “BaseName” : “Queen St” } } } } } } }
{ “_id” : ObjectId(“5c444609f76744a01b4cf268”), “LocationList” : { “Location” : { “Address” : { “ParsedList” : { “Parsed” : { “StreetName” : { “BaseName” : “Grosvenor” } } } } } } }
{ “_id” : ObjectId(“5c44460cf76744a01b4cf26d”), “LocationList” : { “Location” : { “Address” : { “ParsedList” : { “Parsed” : { “StreetName” : { “BaseName” : “Anzac” } } } } } } }


> db.mapsdata.aggregate({$project:{“BaseName_”:{“LocationList.Location.Address.ParsedList.Parsed.StreetName.BaseName”:1}, _id:0}})
2019-01-20T15:35:13.580+0530 E QUERY    [js] Error: command failed: {
“ok” : 0,
“errmsg” : “Invalid $project :: caused by :: cannot use dotted field name ‘LocationList.Location.Address.ParsedList.Parsed.StreetName.BaseName’ in a sub object: { BaseName_: { LocationList.Location.Address.ParsedList.Parsed.StreetName.BaseName: 1.0 }, _id: 0.0 }”,
“code” : 40183,
“codeName” : “Location40183”
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:534:17
assert.commandWorked@src/mongo/shell/assert.js:618:16
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1056:12
@(shell):1:1





> db.mapsdata.aggregate({$project:{BaseName_:”LocationList.Location.Address.ParsedList.Parsed.StreetName.BaseName”}, _id:0}})
2019-01-20T15:37:44.464+0530 E QUERY    [js] SyntaxError: missing ) after argument list @(shell):1:121


> db.mapsdata.aggregate({$project:{BaseName_:”LocationList.Location.Address.ParsedList.Parsed.StreetName.BaseName”, _id:0}})
{ “BaseName_” : “LocationList.Location.Address.ParsedList.Parsed.StreetName.BaseName” }
{ “BaseName_” : “LocationList.Location.Address.ParsedList.Parsed.StreetName.BaseName” }
{ “BaseName_” : “LocationList.Location.Address.ParsedList.Parsed.StreetName.BaseName” }



> db.mapsdata.aggregate({$project:{BaseName_:”$LocationList.Location.Address.ParsedList.Parsed.StreetName.BaseName”, _id:0}})
{ “BaseName_” : “Queen St” }
{ “BaseName_” : “Grosvenor” }
{ “BaseName_” : “Anzac” }

  • Ask Question