【发布时间】:2012-10-31 16:33:12
【问题描述】:
db.foo.find();
_id | type
-------------
10001 1
10002 'a'
10003 [1, 2, 3, 4]
如您所知,$type 将匹配 mongo 查询中的类型代码,如下所示:
db.foo.find({type: {$type: 4}});
_id |输入
----------
10003, [1, 2, 3, 4]
然后,我编写了一个名为 test.js
的 javascript shell 脚本var curs = db.foo.find();
curs.forEach(showTypeCode);
function showTypeCode(cur) {
print(cur.type + '-' + typeof(cur.type));
};
结果:
1-number
a-string
1,2,3,4-object (this is an array, it's 4 in mongo)
这是我的问题,如何在 mongo shell 中获取数组类型代码
【问题讨论】:
-
这是一个已知的错误,MongoDB 实际上将数组作为对象读取。
-
这里是它的 JIRA:jira.mongodb.org/browse/SERVER-1475
-
@Sammaye,我不应该用这个demo,在javascript中[]和{}都是对象类型。我想得到mongo数据类型4。cur有什么方法可以显示这种类型代码?:)
-
我相信我最初读错了你的问题,我添加了一个指向另一个应该有帮助的问题的链接,基本上你必须测试
array的类
标签: javascript shell mongodb