【问题标题】:ColdFusion & MongoDB 4 using Java Driver 3.8使用 Java 驱动程序 3.8 的 ColdFusion 和 MongoDB 4
【发布时间】:2018-08-02 16:08:49
【问题描述】:

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

下面的代码给了我错误,提示找不到方法countDocuments() 或找不到方法find()

<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()>

尝试进行完全匹配搜索时,一切正常。

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

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

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

【问题讨论】:

    标签: mongodb coldfusion mongodb-java


    【解决方案1】:

    该错误消息描述性不强。

    Java 驱动程序不会像 Compass 那样采用正则表达式(或至少不会通过 ColdFusion 对象),因此您必须在 $regex 使用引号之间的模式,例如:

    { &lt;field&gt;: { $regex: 'pattern', $options: '&lt;options&gt;' } }


    例如代替

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

    使用

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

    这里有更多关于如何使用$regex的信息:
    (https://docs.mongodb.com/manual/reference/operator/query/regex/)


    我希望这有帮助。

    【讨论】:

      猜你喜欢
      • 2016-03-13
      • 2017-03-10
      • 2018-12-19
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      相关资源
      最近更新 更多