I have a doc
{_id:NumberLong(1),gender:"M",vip:false}.
How to extract the type of individual field in Mongo with a query.. How to use typeof operator:
I have a doc
{_id:NumberLong(1),gender:"M",vip:false}.
How to extract the type of individual field in Mongo with a query.. How to use typeof operator:
> db.test.findOne()
{ "_id" : NumberLong(1), "gender" : "M", "vip" : false }
> db.test.findOne().gender
M
> typeof db.test.findOne().gender
string
Question title asks for all fields and answers tell how to make it with one field. Here's an all document fields approach (just change your_collection part at the beginning):
[db.your_collection.findOne()].forEach( function(my_doc) { for (var key in my_doc) { print(key + ': ' + typeof my_doc[key]) } } )
First you get an document from the collection, then convert it to an array with [] so we can apply a function with forEach, and finally in the function we iterate over the document fields to print their key and type as we want. This should output something like:
_id: object
gender: string
vip: boolean
You can for example query the type of gender
field:
typeof db.getCollection('your_collection').findOne({"_id": NumberLong(1)}).gender
© 2022 - 2024 — McMap. All rights reserved.