在MongoDB中,我如何在子文档的数组搜索?(In MongoDB, how do I searc

2019-10-23 05:43发布

我有一个mongodb的调查文件,每次调查都有surveyRefId的唯一标识。 我无法理解如何发现具有questionType =硬盘文件中,其surveyRefid = 377或360子文档。

这里是一个示例文件:

{
  "json": {
    "surveyRefId": 377,
    "surveyName": "survey on sociology",
    "questionsVoList": [
      {
        "questionId": "556708425215763c64b8af3d",
        "questionText": "question no 1",
        "questionTitle": "",
        "questionType": "hard",
        "preQuestion": true,
        "questionOptions": [
          {
            "questionRefId": 0,
            "optionType": "RADIOBUTTON",
            "isanswer": true,
            "optionText": "ch1"
          }
        ]
      },
      {
        "questionId": "556708425215763c64b8af3d",
        "questionText": "question no 2",
        "questionTitle": "",
        "questionType": "simple",
        "question": true,
        "questionOptions": [
          {
            "questionRefId": 0,
            "optionType": "RADIOBUTTON",
            "isanswer": true,
            "optionText": "ch1"
          }
        ],
      },
      {
       "questionId": "556708425215763c64b8af3d",
        "questionText": "question no 3",
        "questionTitle": "",
        "questionType": "hard",
        "questionOptions": [
          {
            "questionRefId": 0,
            "optionType": "RADIOBUTTON",
            "isanswer": true,
            "optionText": "ch1"
          },
          {
            "questionRefId": 0,
            "optionType": "RADIOBUTTON",
            "isanswer": false,
            "optionText": "ch2"
          }
        ],
       }
    ]
  }
}

EDIT--解决方案通过使用Java驱动程序按西尔万·勒鲁

    BasicDBObject matchSurvey = new BasicDBObject();
    matchSurvey.put("$match", new BasicDBObject("json.surveyRefId", new BasicDBObject("$in", new Integer[]{377,360})));
    BasicDBObject unwind = new BasicDBObject();
    unwind.put("$unwind", "$json.questionsVoList");
    BasicDBObject matchQuestion = new BasicDBObject();
    matchQuestion.put("$match", new BasicDBObject("json.questionsVoList.questionType", "hard"));
    HashMap map = new HashMap();
    map.put("_id", "$_id");
    map.put("questionsVoList", new BasicDBObject("$push", "$json.questionsVoList"));
    BasicDBObject group = new BasicDBObject();
    group.put("$group",map);        
    List<BasicDBObject> list = new ArrayList<BasicDBObject>();
    list.add(matchSurvey);
    list.add(unwind);
    list.add(matchQuestion);
    list.add(group);
    AggregateIterable output = collection.aggregate(list, DBObject.class);

Answer 1:

“发现有子文档questionType = "hard"有三种不同的方面来理解:

有一个“硬”查询所有文件

如果你只想要具有“硬查询所有文档,您将使用find$elemMatch

db.test.find({"json.surveyRefId": { $in: [377, 360]},
              "json.questionsVoList": {$elemMatch: {"questionType":"hard"}}})

文档的第一个 “硬”查询

如果你需要找到一个文件的第一个 “硬”查询时,您将结合使用上述查询$投影算:

db.test.find({"json.surveyRefId": { $in: [377, 360]}, 
              "json.questionsVoList": {$elemMatch: {"questionType":"hard"}}}
             {"json.surveyRefId":1, "json.questionsVoList.$":1})

所有硬盘查询

如果您需要找到文件的所有 “硬”的疑问,你将不得不使用聚合框架 :

db.test.aggregate({$match: { "json.surveyRefId": { $in: [377, 360]} }},
                  {$unwind: "$json.questionsVoList"},
                  {$match: { "json.questionsVoList.questionType": "hard"}},
                  {$group: {_id: "$_id", questionsVoList: {$push: "$json.questionsVoList"}}}
)
  • 第一$match步将基于其过滤掉不需要的文件surveyRefId
  • 然后$unwind会产生一个文件对每个子文档
  • 另一$match筛选出基于无用的文档questionType
  • 最后, $group将所有的子文档合并早在一个给定_id

生产:

{
    "_id" : ObjectId("556828d002509ae174742d11"),
    "questionsVoList" : [
        {
            "questionId" : "556708425215763c64b8af3d",
            "questionText" : "question no 1",
            "questionTitle" : "",
            "questionType" : "hard",
            "preQuestion" : true,
            "questionOptions" : [
                {
                    "questionRefId" : 0,
                    "optionType" : "RADIOBUTTON",
                    "isanswer" : true,
                    "optionText" : "ch1"
                }
            ]
        },
        {
            "questionId" : "556708425215763c64b8af3d",
            "questionText" : "question no 3",
            "questionTitle" : "",
            "questionType" : "hard",
            "questionOptions" : [
                {
                    "questionRefId" : 0,
                    "optionType" : "RADIOBUTTON",
                    "isanswer" : true,
                    "optionText" : "ch1"
                },
                {
                    "questionRefId" : 0,
                    "optionType" : "RADIOBUTTON",
                    "isanswer" : false,
                    "optionText" : "ch2"
                }
            ]
        }
    ]
}


文章来源: In MongoDB, how do I search in an array of sub-documents?