db.foo.find();
_id | type
-------------
10001 1
10002 'a'
10003 [1, 2, 3, 4]
As you know, the $type will match the type code in mongo query, like this:
db.foo.find({type: {$type: 4}});
_id | type
———-
10003, [1, 2, 3, 4]
and then, I write a javascript shell script called test.js
var curs = db.foo.find();
curs.forEach(showTypeCode);
function showTypeCode(cur) {
print(cur.type + '-' + typeof(cur.type));
};
results:
1-number
a-string
1,2,3,4-object (this is an array, it's 4 in mongo)
here is my question, how can I get the array type code in the mongo shell
Your first query of:
Will not actually work due to a bug. This is a known bug within MongoDB whereby it reads an array as an object when using the
$typeoperator. You can vote and put your support to this JIRA: https://jira.mongodb.org/browse/SERVER-1475As for solving the issue with the JS, this question might be of help to you: Detect if parameter passed is an array? Javascript
Arrays are Objects of the class Array so that is why you are getting object back. If you test for an instance of
Arraythen it should work.