projection - MongoDB - Projecting a field that doesn't always exist -


is there way project fields may or may not exist? such having defined null or undefined?

for instance, doing query with:

      $project: {          date: 1,          name: "$person.name",          age: "$person.age"       }               

not documents guaranteed have $person.age, instead of ones without age being returned { date: today, name: "bill" }, { date: today, name: "bill", age: null }. or similar.

is there better way iterating through data afterwards , creating fields if don't exist?

use $ifnull

  $project: {      date: 1,      name: "$person.name",      age: { $ifnull: [ "$person.age", "null" ] }   }   

you can find more here


Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -