更新具有记录删除更改的mongo Document对象

时间:2018-02-21 17:16:32

标签: java mongodb mongodb-query

目前我在mongo中有这样的数据结构:

 private Document generateMongoUpdateDocument(MongoDataSource dataSource, DataChanges dataChanges, String user, Document filter)
{
    DataModel model = dataSource.getModelInstance();
    Document fieldUpdatesDoc = generateFieldUpdatesDoc(dataChanges, filter);
    Document arrayAdds = generateArrayAddsDoc(dataChanges, filter);
    Document arrayDeletes = generateArrayDeletesDoc(dataChanges, filter);

    if (fieldUpdatesDoc != null) Logger.log(DEBUG, "---- fieldUpdates document\n%s", fieldUpdatesDoc.toJson());
    if (arrayAdds != null) Logger.log(DEBUG, "---- arrayAdds document\n%s", arrayAdds.toJson());
    if (arrayDeletes != null) Logger.log(DEBUG, "---- arrayDeletes document\n%s", arrayDeletes.toJson());

    Document updateDocument = new Document();
    if (!fieldUpdatesDoc.isEmpty() || !arrayAdds.isEmpty() || !arrayDeletes.isEmpty())
    {
        fieldUpdatesDoc.append(model.getUpdatedByField().getName(), user);

        updateDocument.append("$set", fieldUpdatesDoc);

        if (!arrayAdds.isEmpty())
        {
            updateDocument.append("$push", arrayAdds);
        }
        if (!arrayDeletes.isEmpty())
        {

            updateDocument.append("$pull", arrayDeletes);
        }

        DataFields updatedAtField = model.getUpdatedAtField();
        if (null != updatedAtField)
        {
            updateDocument.append("$currentDate", new Document(updatedAtField.getName(), true));
        }
    }

    return updateDocument;
}

我想要做的是更新我的父文档,以包含我在单独文档中跟踪的删除更改。现在,我正在尝试这个:

private Document generateArrayAddsDoc(DataChanges dataChanges, Document filter)
    {
        Document arrayUpdatesDoc = new Document();
        generateArrayDocImpl(arrayUpdatesDoc, null, dataChanges, DataChanges.Type.MODEL_ADD, filter);

        return arrayUpdatesDoc;
    }

private Document generateArrayDeletesDoc(DataChanges dataChanges, Document filter )
    {
        Document arrayDeleteDoc = new Document();
        generateArrayDocImpl(arrayDeleteDoc, null, dataChanges, DataChanges.Type.MODEL_DELETE, filter);

        return arrayDeleteDoc;
    }


private void generateArrayDocImpl(Document arrayUpdateDoc, String parentFieldName, DataChanges dataChanges, DataChanges.Type type, Document filter)
    {
        if (dataChanges.getType() == DataChanges.Type.MODEL_UPDATE)
        {
            for (DataChange dataChange : dataChanges.getDataChanges())
            {
                if (dataChange.getNewValue() instanceof DataChanges)
                {
                    DataChanges arrayChanges = (DataChanges) dataChange.getNewValue();

                    Document arrayDoc = new Document();
                    String dotFieldName = generateDotFieldName(parentFieldName, dataChange.getFieldName());

                    generateArrayDocImpl(arrayDoc, dotFieldName, arrayChanges, type, filter);

                    if (!arrayDoc.isEmpty())
                    {
                        arrayUpdateDoc.append(dotFieldName, arrayDoc);
                    }
                }
            }
        }
        else if (dataChanges.getType() == DataChanges.Type.ARRAY_UPDATE)
        {
            List<Document> updateDocsList = new ArrayList<>();
            for (DataChange dataChange : dataChanges.getDataChanges())
            {
                Document arrayElementDoc = null;
                DataChanges arrayElementChanges = (DataChanges) dataChange.getNewValue();

                if (arrayElementChanges.getType() == type)
                {
                    arrayElementDoc = generateFieldUpdatesDoc(arrayElementChanges, filter);
                }

                Log.console("ARRAY_ELEMENT_CHANGES == " + arrayElementChanges.toString());

                if (null != arrayElementDoc && !arrayElementDoc.isEmpty())
                {
                    updateDocsList.add(arrayElementDoc);
                }
            }

            if (!updateDocsList.isEmpty())
            {
                arrayUpdateDoc.append("$each", updateDocsList);
            }
        }
    }

check&#34; updateDocument.append(&#34; $ pull&#34;,arrayDeletes);&#34;抛出&#34;引起:com.mongodb.MongoCommandException:命令失败,错误2:&#39;未知的顶级操作符:$ each&#39;在服务器上******:27012。完整的回答是{&#34; ok&#34; :0.0,&#34; errmsg&#34; :&#34;未知的顶级操作员:$ each&#34;,&#34; code&#34; :2}&#34;

有人可以指导/告诉我如何正确更新我的mongo文档吗?谢谢!

编辑:

{{1}}

0 个答案:

没有答案