Spring Data MongoRepository 保存具有不同字段数的对象 [英] Spring Data MongoRepository Saving Objects with Differing Numbers of Fields

查看:75
本文介绍了Spring Data MongoRepository 保存具有不同字段数的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将游戏状态存储在 MongoDB 数据库中,并使用 Spring Data 来管理我的数据库交互.我是 Spring Data 的新手,不确定如何处理以下场景.

I am storing game states in a MongoDB database and am using Spring Data to manage my database interactions. I am new to Spring Data and am unsure how to deal with the following scenario.

我有一个游戏"类型的文档,其中包含一些属性,例如 id、时间戳等……其中一个属性是用户采取的操作列表.这些操作的形式如下:

I have a document of type "Game" with a bunch of properties such as id, timestamp, etc... One of these properties is a list of actions taken by users. These actions are of the form:

{ type: 2 }, {type: 3, value: 4}, {type: 5, id: 1234}, {type 6}, {type: 5, value: 6, id: 56}

换句话说,一个动作可以有三个属性:type、value 和 id.但是,并非每个操作都需要存储所有三个值.我想避免在我的数据库中有一堆空值,并且希望我的数据库不包含 id 或未指定的值.

In other words, an action can have three properties: type, value, and id. However, not every action requires all three values to be stored. I want to avoid having a bunch of null values in my database, and would like my database to just not include and id or a value if they are not specified.

使用 Spring Data 的 MongoRepository 模型,我不确定如何实现这一点.我可以创建一个 CRUD Game 类,并且它的一个属性是一个 Action 列表(其中 Action 本身是一个具有属性类型、值和 id 的 CRUD 类),但是如果我没有指定值或 id?

Using Spring Data's MongoRepository model, I am not sure how to achieve this. I can create a CRUD Game class and have one of its properties be a list of Action (where Action itself is a CRUD class with properties type, value, and id), but won't that end up storing null values in the database if I do not specify the value or id?

简而言之,我如何使用 Spring Data 的 MongoRepository,但仍然保持能够存储具有不同参数或对象类型的对象列表的灵活性.

In short, how can I use Spring Data's MongoRepository but still maintain the flexibility of being able to store lists of objects with varying parameters or object types in general.

推荐答案

我将通过一个例子解释如何处理不同的字段.下面的Game.java POJO 类表示映射到game 集合文档的对象.

I will explain how varying fields are handled with an example. The following Game.java POJO class represents the object mapping to the game collection document.

public class Game {

    String name;
    List<Actions> actions;

    public Game(String name, List<Actions> actions) {
        this.name = name;
        this.actions = actions;
    }

    public String getName() {
        return name;
    }

    public List<Actions> getActions() {
        return actions;
    }

    // other get/set methods, override, etc..


    public static class Actions {

        Integer id;
        String type;

        public Actions() {
        }

        public Actions(Integer id) {
            this.id = id;
        }

        public Actions(Integer id, String type) {
            this.id = id;
            this.type = type;
        }

        public Integer getId() {
            return id;
        }

        public String getType() {
            return type;
        }

        // other methods
    }
}

对于 Actions 类,您需要为构造函数提供可能的组合.使用带有 idtype 等的适当构造函数.例如,创建一个 Game 对象并保存到数据库:

For the Actions class you need to provide constructors with the possible combinations. Use the appropriate constructor with id, type, etc. For example, create a Game object and save to the database:

Game.Actions actions= new Game.Actions(new Integer(1000));
Game g1 = new Game("G-1", Arrays.asList(actions));
repo.save(g1);

这是存储在数据库集合game中如下(从mongo shell查询):

This is stored in the database collection game as follows (queried from mongo shell):

{
        "_id" : ObjectId("5eeafe2043f875621d1e447b"),
        "name" : "G-1",
        "actions" : [
                {
                        "_id" : 1000
                }
        ],
        "_class" : "com.example.demo.Game"
}

注意 actions 数组.由于我们在 Game.Actions 对象中只存储了 id 字段,所以只存储了该字段.即使您指定了类中的所有字段,也只有那些提供了值的字段会被持久化.

Note the actions array. As we had stored only the id field in the Game.Actions object, only that field is stored. Even though you specify all the fields in the class, only those provided with values are persisted.

这是另外两个文档,其中 Game.Actions 仅使用 typeid + type 创建,使用适当的构造函数:

These are two more documents with Game.Actions created with type only and id + type using the appropriate constructors:

{
        "_id" : ObjectId("5eeb02fe5b86147de7dd7484"),
        "name" : "G-9",
        "actions" : [
                {
                        "type" : "type-x"
                }
        ],
        "_class" : "com.example.demo.Game"
}
{
        "_id" : ObjectId("5eeb034d70a4b6360d5398cc"),
        "name" : "G-11",
        "actions" : [
                {
                        "_id" : 2,
                        "type" : "type-y"
                }
        ],
        "_class" : "com.example.demo.Game"
}

这篇关于Spring Data MongoRepository 保存具有不同字段数的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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