本文介绍了是否可以在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"
不返回任何内容。
推荐答案
您可以使用以下:
db.test.find("this.value > 12")
这使用JavaScript从字符串到数字的自动转换。
This uses JavaScript's automatic conversion from string to number.
这篇关于是否可以在MongoDB-Query中进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!