I have to query my MongoDB database on fields' type.
This is a document of my collection user
:
In MongoDB there is a table for the available BSON types and their corresponding numbers. For example:
1 -> Double
2 -> String
3 -> Object
4 -> Array
......
......
......
$type
selects the documents where the value of the field is an instance of the specified numeric BSON type:
{ field: { $type: <BSON type> } }
So, for example with this query:
db.user.find({ street: { $type: 2 }})
I get all documents containing a street
field that is a string.
Ather this introduction in nutshell
suppose I want to know which is the type of street
field.
In this case I have to query my database until I get some documents:
db.user.find({ street: { $type: 1 }})
db.user.find({ street: { $type: 2 }})
db.user.find({ street: { $type: 3 }})
db.user.find({ street: { $type: 4 }})
.......
.......
.......
So I have to query my database a lot of times with the same query changing the type number.
Are there other possibilities to know which is the type of a field beyond the previous?