首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Java Driver 3.8的ColdFusion & MongoDB 4

使用Java Driver 3.8的ColdFusion & MongoDB 4
EN

Stack Overflow用户
提问于 2018-08-03 00:08:50
回答 1查看 243关注 0票数 2

我正尝试在Mongo中使用Java11和MongoDB 4.0上的ColdFusion MongoDB驱动程序3.8执行通配符搜索。

下面的代码显示无法找到方法countDocuments()或方法find()的错误。

代码语言:javascript
复制
<cfset Mongo  = CreateObject("java","com.mongodb.MongoClient").init("localhost")>

<cffunction name="m" returntype="any">
    <cfargument name="value" type="any">
    <cfif IsJSON(arguments.value)>
        <cfset local.retrun = CreateObject("java","com.mongodb.util.JSON").parse(arguments.value)>
    <cfelse>
        <cfset local.retrun = CreateObject("java","com.mongodb.util.JSON").parse( SerializeJSON(arguments.value) )>     
    </cfif>
    <cfreturn local.retrun>
</cffunction>

<cfset db = Mongo.getDatabase('fda')>
<cfset DrugInfo = db.getCollection("druginfo")>

<cfset searchCount=druginfo.countDocuments(m({'openfda.brand_name':/Ty/ }))>

<cfset results  = DrugInfo.find(m({'openfda.brand_name': /Ty/})).iterator()>

当尝试执行精确匹配搜索时,一切工作正常。

代码语言:javascript
复制
<cfset searchCount=druginfo.countDocuments(m({'openfda.brand_name':'Tylenol'}))>

<cfset results  = DrugInfo.find(m({'openfda.brand_name': 'Tylenol'})).iterator()>

我基本上是在Mongo Compass中测试我的所有查询,并将它们粘贴到我的代码中,但它并没有像预期的那样工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-03 05:30:22

该错误消息的描述性不强。

Java驱动程序不会像Compass那样接受正则表达式(或者至少不是通过ColdFusion对象),所以您必须使用引号之间的模式进行$regex,如下所示:

{ <field>: { $regex: 'pattern', $options: '<options>' } }

例如,不是

m({'openfda.brand_name':/Ty/ })

使用

m({ 'openfda.brand_name': { '$regex': '^Ty', '$options': 'i' } })

这里有更多关于如何使用$regex的信息:

(https://docs.mongodb.com/manual/reference/operator/query/regex/)

我希望这能帮到你。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51658025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档