18293

How to add arrayIndex position to the unwinded documents in mongo db of version 3.0.4

Question:

Having aggregattion:

{ $unwind: { path: <field path>, includeArrayIndex: <string>, preserveNullAndEmptyArrays: <boolean> } }

Currently, I'm using this "$unwind" concept in mongodb version 3.2.It is working fine. But in production I have mongodb version 3.0.4. How can I use this concept in 3.0.4 version ?

Can anyone please help me out regarding this issue ...

Answer1:

there are two ways to fix it.

<ol><li>UPGRADE :-)</li> <li>Change document structure to add a position value - it will act as a kind of entryID field - please see snippet below:</li> </ol>

Change documnet structure in forEach loop:

db.dev777_copy.find({}).forEach(function (doc) { var empArraySize = doc.employees.length; for (var i = 0; i < empArraySize; i++) { var innerArraySize = doc.employees[i].Salary.length; for (var j = 0; j < innerArraySize; j++) { doc.employees[i].Salary[j] = { position : NumberInt(j), value : doc.employees[i].Salary[j] }; } } db.dev777_copy.save(doc); })

and then our document will get new shape:

{ "_id" : ObjectId("57862763a63c006944fb508b"), "employees" : [{ "empId" : 100.0, "Salary" : [{ "position" : 0, "value" : 1000.0 }, { "position" : 1, "value" : 2000.0 }, { "position" : 2, "value" : 3000.0 } ] }, { "empId" : 101.0, "Salary" : [{ "position" : 0, "value" : 3000.0 }, { "position" : 1, "value" : 4000.0 }, { "position" : 2, "value" : 500.0 } ] } ] }

Recommend

  • Can't install scipy
  • .Settings file in Win Form App, Best Practice
  • Dynamically Impersonate a remote user - c# and asp.net
  • Unable to connect to remote MySql server in Asp.Net
  • Mercurial vs Subversion. Whose performance is better?
  • Why does IE8 fail to resolve my JQuery selector for a checked radio option?
  • Rails AREL .where statement
  • Is there a way to link a linux's thread TID and a pthread_t “thread ID”
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Unable to connect to OnVif enabled camera using C#
  • What is the use of a session store?
  • .NET video play library which allows to change the playback rate?
  • Using Laravel 5.4 pusher
  • Getting different value with placeholder over CPU/GPU
  • Getting media player state in windows phone 7
  • CSS bleed-through with cfinput type=“datefield”
  • Possible to get mouse events fired when cursor is outside page?
  • What is the purpose of TaskExecutor in spring?
  • Tamper-proof configuration files in .NET?
  • MySQL Order by column = x, column asc?
  • Blackberry - Custom EditField Cursor
  • Body moving without any force applied? (Box2d)
  • Display issues when we change from one jquery mobile page to another in firefox
  • Different response to non-authenticated users and AJAX calls
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Bug in WPF DataGrid
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • TFS: Get latest causes slow project reloading
  • Javascript Callbacks with Object constructor
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Javascript + PHP Encryption with pidCrypt
  • How to make Safari send if-modified-since header?
  • How can I estimate amount of memory left with calling System.gc()?
  • WOWZA + RTMP + HTML5 Playback?
  • Data Validation Drop Down Box Arrow Disappearing
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Hits per day in Google Big Query
  • Android Google Maps API OnLocationChanged only called once
  • How does Linux kernel interrupt the application?