MongoDB $在三个级别上查找

时间:2017-08-23 14:06:08

标签: javascript node.js mongodb aggregate

我有三个收藏品

会话:

{
     "_id": ObjectId("599c34620ee315182cdc9272"),
     "updated_at": ISODate("2017-08-22T13:40:50.331Z"),
     "created_at": ISODate("2017-08-22T13:40:50.331Z"),
     "user": ObjectId("595f8426645bf5f47366fb29"),
     "person": ObjectId("599c34610ee315182cdc9270"),
     "answer": [
         ObjectId("599c34620ee315182cdc9271")
      ],
      "__v": 
}

答案:

{    
  "_id": ObjectId("599c34620ee315182cdc9271"),
  "form": ObjectId("599585f37ee86133ec39f762"),
  "data": [
    {
      "question": ObjectId("5995860f7ee86133ec39f763"),
      "value": "felipe xd",
      "other": ""
    },
    {
      "question": ObjectId("599586217ee86133ec39f764"),
      "value": "felipe@xd.com",
      "other": ""
    },
    {
      "question": ObjectId("599c21d15df1f5196cc06cd0"),
      "value": 75,
      "other": ""
    },
    {
      "question": ObjectId("599c21fe5df1f5196cc06cd1"),
      "value": true,
      "other": ""
    },
    {
      "question": ObjectId("599c23da5df1f5196cc06cd2"),
      "option": [
        "599c23da5df1f5196cc06cd3",
        "599c23da5df1f5196cc06cd5"
      ],
      "other": ""
    },
    {
      "question": ObjectId("599c23fb5df1f5196cc06cd6"),
      "value": "123123213",
      "other": ""
    }
  ]
}

形式:

{
  "_id": ObjectId("599585f37ee86133ec39f762"),
  "updated_at": ISODate("2017-08-17T12:02:59.751Z"),
  "created_at": ISODate("2017-08-17T12:02:59.751Z"),
  "title": "formulário de teste",
  "key": "formulario_de_teste",
  "type": "",
  "description": "",
  "welcomeMessage": "Hello  my darling, this is a test! xD xD",
  "thanksMessage": "Obrigado por testar =)",
  "hoursToBegin": 10,
  "subAction": ObjectId("5994336358197738ce575f1a"),
  "deleted": false,
  "questions": [ ],
  "active": true,
  "__v": 0
}

我想要实现的是使用其数组中的answer对象填充会话对象,然后使用其对象填充form,然后使用其填充data.question宾语。

我试图做的事情如下:



var query = [
  // match the object
  { "$match": {"_id": ObjectId("599c34620ee315182cdc9272")}},
  
  // unwind answer to have acces to its properties as a single item
  {"$unwind": {"path":"$answer"}},
  { "$lookup": {
    "from": "answers",
    "localField": "answer",
    "foreignField": "_id",
    "as": "answer"
  }},

  // unwind the lookup result to have access to its form property
  {"$unwind": {"path": "$answer"}},
  {"$lookup": {
      "from": "forms",
      "localField": "answer.form",
      "foreignField": "_id",
      "as": "answer.form"
    }
  },
  {"$unwind": {"path": "$answer.data"}},
  {"$lookup": {
      "from": "questions",
      "localField": "answer.data.question",
      "foreignField": "_id",
      "as": "data"
    }
  },
  {"$unwind": {"path": "$data"}},
  {"$unwind": {"path": "$answer.form"}},
  {"$group": {
    "_id": "$_id",
    "person": {"$first": "$person"},
    "user": {"$first": "$user"},
    "answer": {"$push": "$answer"},
    "data": {"$push": "$data"}
  }}
]
db.sessions.aggregate(query)




我想要实现的结构如下:



[
  {
    "_id": "599c34620ee315182cdc9272",
    "updated_at": "2017-08-22T13:40:50.331Z",
    "created_at": "2017-08-22T13:40:50.331Z",
    "user": "595f8426645bf5f47366fb29",
    "person": "599c34610ee315182cdc9270",
    "__v": 0,
    "answer": [
      {
        "_id": "599c34620ee315182cdc9271",
        "form": {
          "_id": "599585f37ee86133ec39f762",
          "updated_at": "2017-08-17T12:02:59.751Z",
          "created_at": "2017-08-17T12:02:59.751Z",
          "title": "formulário de teste",
          "key": "formulario_de_teste",
          "type": "",
          "description": "",
          "welcomeMessage": "Hello  my darling, this is a test! xD xD",
          "thanksMessage": "Obrigado por testar =)",
          "hoursToBegin": 10,
          "subAction": "5994336358197738ce575f1a",
          "__v": 0,
          "deleted": false,
          "questions": [],
          "active": true,
          "id": "599585f37ee86133ec39f762"
        },
        "data": [
          {
            "question": {
              "_id": "5995860f7ee86133ec39f763",
              "updated_at": "2017-08-22T12:21:11.874Z",
              "created_at": "2017-08-17T12:03:27.687Z",
              "title": "Seu nome para que a gente consiga testar",
              "form": "599585f37ee86133ec39f762",
              "type": "text",
              "order": null,
              "key": "seu_nome_para_que_a_gente_consiga_testar",
              "help": "",
              "required": false,
              "description": "",
              "subType": "name",
              "__v": 0,
              "deleted": false,
              "condition": null,
              "active": true,
              "id": "5995860f7ee86133ec39f763"
            },
            "value": "felipe xd",
            "other": "",
            "option": null
          },
          {
            "question": {
              "_id": "599586217ee86133ec39f764",
              "updated_at": "2017-08-22T12:21:17.574Z",
              "created_at": "2017-08-17T12:03:45.630Z",
              "title": "seu email pra gente te criar!",
              "form": "599585f37ee86133ec39f762",
              "type": "text",
              "order": null,
              "key": "seu_email_pra_gente_te_criar!",
              "help": "",
              "required": false,
              "description": "",
              "subType": "email",
              "__v": 0,
              "deleted": false,
              "condition": null,
              "active": true,
              "id": "599586217ee86133ec39f764"
            },
            "value": "felipe@xd.com",
            "other": "",
            "option": null
          },
          {
            "question": {
              "_id": "599c21d15df1f5196cc06cd0",
              "updated_at": "2017-08-22T12:24:56.934Z",
              "created_at": "2017-08-22T12:21:37.567Z",
              "title": "333",
              "form": "599585f37ee86133ec39f762",
              "type": "scale",
              "order": null,
              "key": "333",
              "help": "",
              "required": false,
              "description": "",
              "subType": "scroll",
              "__v": 0,
              "deleted": false,
              "condition": null,
              "scale": {
                "scroll": {
                  "label": "123",
                  "min": 0,
                  "max": 100,
                  "step": 25,
                  "formater": "none"
                }
              },
              "active": true,
              "id": "599c21d15df1f5196cc06cd0"
            },
            "value": 75,
            "other": "",
            "option": null
          },
          {
            "question": {
              "_id": "599c21fe5df1f5196cc06cd1",
              "updated_at": "2017-08-22T12:26:51.509Z",
              "created_at": "2017-08-22T12:22:22.122Z",
              "title": "4444",
              "form": "599585f37ee86133ec39f762",
              "type": "boolean",
              "order": null,
              "key": "4444",
              "help": "",
              "required": false,
              "description": "",
              "subType": "yesno",
              "__v": 0,
              "deleted": false,
              "condition": {
                "question": "599c21d15df1f5196cc06cd0",
                "booleanValue": null,
                "scaleValue": 75,
                "multipleValue": []
              },
              "active": true,
              "id": "599c21fe5df1f5196cc06cd1"
            },
            "value": true,
            "other": "",
            "option": null
          },
          {
            "question": {
              "_id": "599c23da5df1f5196cc06cd2",
              "updated_at": "2017-08-22T12:30:18.622Z",
              "created_at": "2017-08-22T12:30:18.622Z",
              "title": "55555",
              "form": "599585f37ee86133ec39f762",
              "type": "multiple",
              "order": null,
              "key": "55555",
              "help": "",
              "required": false,
              "description": "",
              "subType": "check",
              "__v": 0,
              "deleted": false,
              "condition": {
                "question": "599c21fe5df1f5196cc06cd1",
                "booleanValue": true,
                "scaleValue": null,
                "multipleValue": []
              },
              "active": true,
              "id": "599c23da5df1f5196cc06cd2"
            },
            "other": "",
            "option": [
              {
                "_id": "599c23da5df1f5196cc06cd3",
                "title": "1111",
                "order": null,
                "question": "599c23da5df1f5196cc06cd2",
                "key": "1111",
                "description": "",
                "help": "",
                "deleted": false,
                "active": true,
                "id": "599c23da5df1f5196cc06cd3"
              },
              {
                "_id": "599c23da5df1f5196cc06cd5",
                "title": "33333",
                "order": null,
                "question": "599c23da5df1f5196cc06cd2",
                "key": "33333",
                "description": "",
                "help": "",
                "deleted": false,
                "active": true,
                "id": "599c23da5df1f5196cc06cd5"
              }
            ]
          },
          {
            "question": {
              "_id": "599c23fb5df1f5196cc06cd6",
              "updated_at": "2017-08-22T12:30:51.074Z",
              "created_at": "2017-08-22T12:30:51.074Z",
              "title": "66666",
              "form": "599585f37ee86133ec39f762",
              "type": "text",
              "order": null,
              "key": "66666",
              "help": "",
              "required": false,
              "description": "",
              "subType": "text",
              "__v": 0,
              "deleted": false,
              "condition": {
                "question": "599c23da5df1f5196cc06cd2",
                "booleanValue": null,
                "multipleValue": [
                  "599c23da5df1f5196cc06cd3",
                  "599c23da5df1f5196cc06cd5"
                ]
              },
              "active": true,
              "id": "599c23fb5df1f5196cc06cd6"
            },
            "value": "123123213",
            "other": "",
            "option": []
          }
        ],
        "id": "599c34620ee315182cdc9271"
      }
    ],
    "id": "599c34620ee315182cdc9272"
  }
]




我需要在MongoDB的聚合框架

的单个查询中完成

0 个答案:

没有答案