Mongo返回一个数组元素

时间:2015-06-04 00:21:41

标签: mongodb

我在mongoDB中有以下JSON文档,我在mingoimport中添加了它。

我试图从问题数组中返回一个元素,其中,问题等于“q1”。

     {
  "questions": [
    {
      "questionEntry": {
        "id": 1,
        "info": {
          "seasonNumber": 1,
          "episodeNumber": 1,
          "episodeName": "Days Gone Bye"
        },
        "questionItem": {
          "theQuestion": "q1",
          "attachedElement": {
            "type": 1,
            "value": ""
          }
        },
        "options": [
          {
            "type": 1,
            "value": "o1"
          },
          {
            "type": 1,
            "value": "o1"
          }
        ],
        "answer": {
          "questionId": 1,
          "answer": 1
        },
        "metaTags": [
          "Season 1",
          "Episode 1",
          "Rick Grimmes"
        ]
      }
    },
    {
      "questionEntry": {
        "id": 1,
        "info": {
          "seasonNumber": 1,
          "episodeNumber": 1,
          "episodeName": "Days Gone Bye"
        },
        "questionItem": {
          "theQuestion": "q2",
          "attachedElement": {
            "type": 1,
            "value": ""
          }
        },
        "options": [
          {
            "type": 1,
            "value": "o2"
          },
          {
            "type": 1,
            "value": "o2"
          }
        ],
        "answer": {
          "questionId": 1,
          "answer": 1
        },
        "metaTags": [
          "Season 1",
          "Episode 1",
          "Rick Grimmes",
          "Glenn Rhee"
        ]
      }
    }
  ]
}

我运行了查询db.questions.find({"questions.questionEntry.questionItem.theQuestion" : "q1"}),但这重新整理了整个文档(问题数组中的questionEntry都是!

我试过了db.questions.find({"questions.questionEntry.questionItem.theQuestion" : "q1"}, _id:0," questions.questionItem": {$elemMatch : {theQuestion: "q1"}}})

但是得到以下错误: Error: error: { "$err" : "Can't canonicalize query: BadValue Cannot use $elemMatch projection on a nested field.", "code" : 17287

有没有办法可以将结果限制为只包含它的数组元素?

由于

2 个答案:

答案 0 :(得分:3)

<?php
/**
 * Template Name: Gallery Page
 *
 */

 if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        the_content();

    } // end while
} // end if



function pw_show_gallery_image_urls( $content ) {

    global $post;

    // Only do this on singular items
    if( ! is_singular() )
        return $content;

    // Make sure the post has a gallery in it
    if( ! has_shortcode( $post->post_content, 'gallery' ) )
        return $content;

    // Retrieve the first gallery in the post
    $gallery = get_post_gallery_images( $post );

    $image_list = '<ul>';

    // Loop through each image in each gallery
    foreach( $gallery as $image_url ) {

        $image_list .= '<li>' . '<img src="' . $image_url . '">' . '</li>';

    }

    $image_list .= '</ul>';

    // Append our image list to the content of our post
    $content .= $image_list;

    return $content;

 }
 add_filter( 'the_content', 'pw_show_gallery_image_urls' );
?>

db.questions.find({},{"questions.questionEntry.questionItem.theQuestion" : "q1"});

请尝试这些。

答案 1 :(得分:1)

如果您想使用$elemMatch,则查询应为:

db.questions.find(
    {"questions.questionEntry.questionItem.theQuestion" : "q1"},
    {
        '_id':0,
        "questions": {
            $elemMatch : {"questionEntry.questionItem.theQuestion": "q1"}
        }
    }
)