将数据存储为映射而不是用于embedMany关系的数组

时间:2019-03-18 15:49:46

标签: mongodb laravel

通过在laravel中运行以下命令:

$model = new Accommodation();
$model->fill(['name'=>'taghi']);
$model->save();
$model->room_types()->create(['name'=>'room 1','id'=>'room_1']);
$model->room_types()->create(['name'=>'room 2','id'=>'room_2']);

这是MongoDB中的结果:

{
    "_id" : ObjectId("5c8fafa4d82c24311f440482"),
        "@attributes": {
          "name": "hotel Esteghlal",
          "address": "tehran"
        },
    "updated_at" : ISODate("2019-03-18T14:48:04.000Z"),
    "created_at" : ISODate("2019-03-18T14:48:04.000Z"),
    "room_types" : [ 
        {
            "name" : "room 1",
            "_id" : ObjectId("5c8fafa4d82c24311f440483"),
        }, 
        {
            "name" : "room 2",
            "_id" : ObjectId("5c8fafa4d82c24311f440484")
        }
    ]
}
it is my expected document in mongo db:
{
    "_id" : ObjectId("5c8fafa4d82c24311f440482"),
        "@attributes": {
          "name": "hotel Esteghlal",
          "address": "tehran"
        },
        "room_types": {
      "room_1":{
        "_id" : ObjectId("5c8fafa4d82c24311f440483",
        "name":"room 1"
      },
      "room_2": {

        "name":"room 2",
        "_id" : ObjectId("5c8fafa4d82c24311f440484")    
          }
        }
}

问题的摘要是,有可能由Laravel ORM以使用子文档的自然ID作为键的子对象而不是将它们存储为数组的方式来创建嵌入式对象吗?

0 个答案:

没有答案