Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

php - Mongodb unwind nested documents

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?

Thanks in advance!

This is the query:

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

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


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

This is the structure:

{
    "_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",

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

In the mongo shell, a command like this

db.collection.aggregate(
  { $project: {
    _id: 1,
    items: 1
  } },
  { $unwind: '$items' },
  { $unwind: '$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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...