Mongodb展开嵌套文档 [英] Mongodb unwind nested documents

查看:108
本文介绍了Mongodb展开嵌套文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB中有一个文档,我正在尝试在PHP中展开它.我要展开一个文档,该文档的子文档包含另一个子文档.如果文档仅包含字符串和数字,则可以成功执行此操作,但是如果包含另一个子文档,则无法正常工作.我收到此错误:

I have a document in MongoDB and I'm trying to unwind it in PHP. I want to unwind a document that has a subdocument that contains yet anothersubdocument. I was able to do this succesfully if the document only contains strings and numbers, but if it contains another subdocument then I can't get it to work. I get this error:

exception: $unwind: value at end of field path must be an array

您是否可以展开包含另一级子文档的子文档?如果没有,您将如何去做?

Can you not unwind a subdocument that contains another level of subdocument? If not, how would you go about doing this?

提前谢谢!

这是查询:

$project = array(
                '$project' => array(
                    '_id' => 1,
                    'items' => 1,
                )
            );

$unwind = array(
                '$unwind' => '$items'
            );


 $query = $mongo->store->aggregate($project,$unwind_items);

这是结构:

{
    "_id": {
        "$oid": "526fdc1fd6b0a8182300009c"
    },
    "items": [
               {
            "quantity": "1",
            "category_id": {
                "$oid": "526fdc1fd6b0a81823000029"
            },
            "category": "test",
            "images": [
                {
                    "name": "9by9easy.PNG",
                    "path": "upload_files/nibh-vulputate-mauris-corporation/",
                    "file_path": "upload_files/nibh-vulputate-mauris-corporation/68e7c50bde1476e96ca2461dc553cce5528fb70e41b1f.PNG",
                    "size": 8761
                },
                {
                    "name": "9by9hard.PNG",
                    "path": "upload_files/nibh-vulputate-mauris-corporation/",
                    "file_path": "upload_files/nibh-vulputate-mauris-corporation/8cd2dcf4fcd476262db2eba3fdb2c39a528fb70e42757.PNG",
                    "size": 11506
                }
            ],
            "link": "fghfhfhfg"
        }
    ],
    "name": "Nibh Vulputate Mauris Corporation",

}

推荐答案

我知道MongoDB可以实现您想做的事情. aggregate()命令可以根据需要使用任意数量的参数.

I know that what you're trying to do is possible with MongoDB. The aggregate() command can take as many arguments as you need.

在mongo shell中,这样的命令

In the mongo shell, a command like this

db.collection.aggregate(
  { $project: {
    _id: 1,
    items: 1
  } },
  { $unwind: '$items' },
  { $unwind: '$items.images' }
);

将展开items子文档,然后展开images子文档.

will unwind the items subdocument, then the images subdocument.

根据您问题中的代码,也许这可以工作

Based on the code in your question, perhaps this will work

$project = array(
  '$project' => array(
    '_id' => 1,
    'items' => 1,
  )
);

$unwind_items = array(
  '$unwind' => '$items'
);

$unwind_images = array(
  '$unwind' => '$items.images'
);


$query = $mongo->store->aggregate($project,$unwind_items,$unwind_images);

这篇关于Mongodb展开嵌套文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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