MongoDB查询构造函数采用原始查询字符串Java [英] Mongodb query constructor to take raw query string Java

查看:331
本文介绍了MongoDB查询构造函数采用原始查询字符串Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提出一个概念,以获取查询字符串,并通过查询对象将其传递给类似这样的内容:

I am trying to come up with a concept to take a query string and have it passed into something like this via a query object:

returnList = mongoTemplateTracking.find(query,TrackingData.class, COLLECTION_NAME);

我一直在查询Query和Criteria类的构造函数,以查看它们是否可以采用原始字符串,例如:

I've been looking at the constructors of both the Query and Criteria classes to see if they could take a raw string such as:

 "ID" : "32399a"

而不是通过Criteria.where().is()等来建立Criteria对象.

Instead of building up the Criteria object via Criteria.where().is() etc..

我已经看到了方法

protected <T> List<T> doFind(String collectionName,
                 com.mongodb.DBObject query,
                 com.mongodb.DBObject fields,
                 Class<T> entityClass)

但是,当我将mongoTemplateTracking自动装配到类中时,无法访问此方法.

However as I am autowiring the mongoTemplateTracking into my class I cannot access this method.

推荐答案

MongoDB查询语言("MQL")易于以JSON形式表示.因此,如果您有一个字符串表达式,例如:

MongoDB query language ("MQL") is easily expressed in JSON form. So if you have a string expression like:

String s = "{\"$or\": [ {\"name\": \"buzz\"}, {\"age\": {\"$lt\": 20 }} ] }";

然后您可以使用此实用工具轻松解析它:

then you can easily parse it with this util:

import com.mongodb.util.JSON;
DBObject query = (DBObject) JSON.parse(s);
yourCollection.find(query);

看看

Take a look at In Java, is there a way to write a string literal without having to escape quotes? to make the escaping of quotes in long query expressions a little easier.

这篇关于MongoDB查询构造函数采用原始查询字符串Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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