组合多个阵列以使用MongoDB Java驱动程序进行查询

时间:2017-05-18 15:20:21

标签: java mongodb aggregation-framework mongo-java-driver

我有以下JSON:

{"pid":"b00l16vp","categories":{"category1":["factual", "arts culture and the media", "history"]}}
{"pid":"b0079mpp","categories":{"category2":["childrens", "entertainment and comedy", "animation"],"category1":["signed"]}}
{“pid":"b00htbn3"}
{“pid":"b00gdhqw","categories":{"category2":["factual"],"category3":["scotland"],"category4":["lifestyle and leisure", "food and drink"],"category1":["entertainment", "games and quizzes"]}}

我的目的是通过组合单个数组中的所有数组,使用String数组查询categories对象。 我有以下代码:

String [] cats = ["childrens", "signed"]
BasicDBObject theProjections = new BasicDBObject()
for (int i = 1; i <= 5; i++) {
    String identifier = "categories.category" + i
    String cleanIdentifier = "\$" + identifier
    //If the category does not exist, put in a blank category
    def temp = [cleanIdentifier, []]
    theMegaArray.add(new BasicDBObject('$ifNull', temp))
}
//The megaArray is the array created in the above loop which combines all arrays
BasicDBObject theData = new BasicDBObject('$setUnion', theMegaArray)
BasicDBObject theFilter = new BasicDBObject('input', theData)
theFilter.put("as", "megaArray")
//all of the values found in cats should match the megaArray
theFilter.put("cond", new BasicDBObject('$all', ["\$\$megaArray", cats]))
theProjections.put('$filter', theFilter)
FindIterable iterable = collection.find(criteria).projection(theProjections)

到目前为止,我已经使用this问题来编写此代码。 $setUnion期望所有字段出现在我的JSON中,有不同数量的类别数组,因此我使用$ifNull用[]填充空类别。 $filter已被用于查询megaArray上的cats数组。

运行时,我收到以下错误:

Caused by: com.mongodb.MongoQueryException: Query failed with error code 2 and error message '>1 field in obj: { input: { $setUnion: [ { $ifNull: [ "$categories.category1", [] ] }, { $ifNull: [ "$categories.category2", [] ] }, { $ifNull: [ "$categories.category3", [] ] }, { $ifNull: [ "$categories.category4", [] ] }, { $ifNull: [ "$categories.category5", [] ] } ] }, as: "megaArray", cond: { $all: [ "$$megaArray", [ "factual" ] ] } }'

我不完全确定这看起来是什么意思。我还应该注意,类别对象并不总是存在,但我不确定这是否重要。

2 个答案:

答案 0 :(得分:1)

您可以使用聚合框架

获得相同的结果

为此,您可以先设置数据以创建'megaArray',然后匹配新数组。

String [] cats = new String[] {"childrens", "signed"};

List<DBObject> theMegaArray = new ArrayList<>();

BasicDBObject theProjections = new BasicDBObject();

for (int i = 1; i <= 5; i++) {
  String identifier = "categories.category" + i;
  String cleanIdentifier = "$" + identifier;
  //If the category does not exist, put in a blank category
  Object[] temp = new Object[] {cleanIdentifier, new Object[]{}};
  theMegaArray.add(new BasicDBObject("$ifNull", temp));
}

theProjections.put("_id", 1);
theProjections.put("pid", 1);
theProjections.put("categories",1);
theProjections.put("allCategories", new BasicDBObject("$setUnion", theMegaArray));

BasicDBObject theFilter = new BasicDBObject("allCategories", new BasicDBObject("$all", cats));

List<BasicDBObject> pipeline = new ArrayList<>();
pipeline.add(new BasicDBObject("$project", theProjections));
pipeline.add(new BasicDBObject("$match", theFilter));

AggregateIterable iterable = collection.aggregate(pipeline);

上面的代码示例在项目阶段添加了一个名为“allCategories”的新数组,然后匹配这个新文档。

可以添加另一个投影阶段以从最终输出中删除allCategories数组

答案 1 :(得分:1)

您可以使用以下聚合管道。

Shell查询供参考:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA....);

Java代码:

db.collection.aggregate([{
    "$project": {
        "pid": 1,
        "categories": 1,
        "filter": {
            "$eq": [{
                    "$setUnion": [{
                        "$ifNull": ["$categories.category1", []]
                    }, {
                        "$ifNull": ["$categories.category2", []]
                    }, {
                        "$ifNull": ["$categories.category3", []]
                    }, {
                        "$ifNull": ["$categories.category4", []]
                    }, {
                        "$ifNull": ["$categories.category5", []]
                    }]
                },
                ["childrens", "signed"]
            ]
        }
    }
}, {
    "$match": {
        "filter": true
    }
}])