本文介绍了是否可以在 MongoDB 查询中进行转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我有两个这样的 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 表达式:
db.test.find("this.value > 12")
这使用了 JavaScript 从字符串到数字的自动转换.
This uses JavaScript's automatic conversion from string to number.
这篇关于是否可以在 MongoDB 查询中进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!