是否可以在MongoDB-Query中进行转换? [英] Is it possible to cast in a MongoDB-Query?

查看:154
本文介绍了是否可以在MongoDB-Query中进行转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有两个这样的MongoDB文档...

When I have two MongoDB documents like this...

db.test.insert( {"value" : "10123"} );
db.test.insert( {"value" : "160"} );

查询结果如下:

db.test.find({"value" :{$gt : "12"} });

{ "_id" : ObjectId("4c6d1b92304326161b678b89"), "value" : "160" }

很明显,进行了字符串比较,因此我的第一个值不返回。
在查询中是否有任何方式?

It's obvious, that a string comparison is made, so that my first value is not returned. Is there any way to cast within the query?

类似:

db.test.find({ (int) "value" :{$gt : 12} });

db.test.find({"value" :{$gt : 12} }); // without the quotes around "12"

不返回任何内容。

推荐答案

您可以使用以下 JavaScript expression

db.test.find("this.value > 12")

这使用JavaScript从字符串到数字的自动转换。

This uses JavaScript's automatic conversion from string to number.

这篇关于是否可以在MongoDB-Query中进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆