Return actual type of a field in MongoDB

Starting from MongoDB 3.4, you can use the $type aggregation operator to return a field’s type.

db.posts.aggregate( 
    [ 
        { "$project": { "fieldType": {  "$type": "$date2"  } } } 
    ]
)

which yields:

{ 
    "_id" : ObjectId("4c0ec11e8fd2e65c0b010000"), 
    "fieldType" : "string" 
}

Leave a Comment