Невозможно написать запрос агрегации MongoDB в Java - PullRequest
0 голосов
/ 26 февраля 2019

Я пытаюсь написать запрос агрегации MongoDB в Java, используя загрузку Spring.Я могу добиться желаемого результата с помощью запросов MongoDB.Тем не менее, я получаю ошибки в Java.Ниже мой запрос:

db.contents.aggregate([
 {$match: 
    {$and: [
        {"domain": "bingo.com"},
        {"locale": {$in: ["en-in", "en-us"]}},
        {"contents.contentName": "Template_2"}
      ]}
  },
 {$unwind: "$contents"},
 {$unwind: "$contents.fields"},
 {$match: { "contents.fields.title": {$in: ["Company Name"]}}},
 {$group: {
     "_id": "$_id",
     "contents": { "$push": "$contents" },
     "root":{$first:"$$ROOT"}
 }},
 {$replaceRoot: {newRoot:{$mergeObjects:["$root",{contents:'$contents'}]}}},
 {$skip: 0},
 {$limit: 5}
]);

Я пробовал ниже в Java:

MatchOperation primaryMatch = match(
                where("domain").is(domain)
                .andOperator(
                        where("contents.contentName").is(templateName),
                        where("locale").in(criteria.getLocales())));
        UnwindOperation unwindContents = unwind("contents");
        UnwindOperation unwindFields = unwind("contents.fields");
        MatchOperation matchTitle = match(
        where("contents.fields.title").in(criteria.getFields()));
GroupOperation groupOperation = 
        group("_id")
         .push("$contents").as("contents")
         .first(Aggregation.ROOT).as("root");
AggregationOperation replaceRoot = Aggregation.replaceRoot().withValueOf(ObjectOperators.valueOf("contents").mergeWith(Aggregation.ROOT));
Aggregation aggregation = Aggregation.newAggregation(primaryMatch, unwindContents, unwindFields, matchTitle, groupOperation,replaceRoot);

Я получаю ниже исключения, как: Command failed with error 40400 (Location40400): '$mergeObjects requires object inputs

Это образец документа:

{
    "pageName": "Home",
    "link": "hello.com",
    "locale": "en-us",
    "contents": [
        {
            "contentName": "Template_2",
            "fields": [
                {
                    "fieldType": "Plain Text",
                    "id": "companyName456",
                    "title": "Company Name",
                    "alternateText": "Company Name",
                    "value": "Microsoft",
                    "placeholder": "Enter your Company name"
                },
                {
                    "fieldType": "Plain Text",
                    "id": "designation789",
                    "title": "Designation",
                    "alternateText": "Designation",
                    "value": "Software Engineer",
                    "placeholder": "Enter your designation name"
                }
            ]
        }
    ]
}

Пожалуйста, руководство, как это сделать.Я пытаюсь отфильтровать массив полей и вернуть полный документ.Например, если я укажу «заголовок»: «Обозначение», он отфильтрует все остальные объекты полей и включит поле со свойством заголовка со значением «Обозначение».Заранее спасибо.

...