【问题标题】:collection.Aggregate not working on array of objects which has another array of objectscollection.Aggregate 不适用于具有另一个对象数组的对象数组
【发布时间】:2020-04-10 21:28:21
【问题描述】:

我一直在尝试使用 mongoose 和 mongodb 在 Node.js 中发出 get 请求。

我的用户有一个包含设备数组对象的农场数组对象列表。我正在查询并尝试获取特定设备的字段。

这里我尝试使用聚合函数,但它返回的是空数组。

后端代码

router.get("/getParameter/:farmID/:deviceID",async(req,res)=>{
  User.aggregate([
    {$match:{"farm._id":req.params.farmID}},
    {$unwind:'$farm'},
    {$match:{"device._id":req.params.deviceID}},
    {$unwind:'$device'},
    {$match:{"device.Parameter":{$exists:true}}},
    {$project:{"Parameter":1}}
  ]).exec((err,result)=>{
    if (err) throw err
    console.log(result)
    res.send(result)
  });
})

用户架构

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
mongoose.set('useFindAndModify', false);

//Device Schema
const Device = new Schema({
  deviceName: {
    type: String,
    required: true,
    unique:true
  },
  deviceType: {
    type: String,
    required: true
  },
  description:{
      type:String,
      required:false   
  },
  deviceLocation:{
    default:Array,
    "default":[],
    required:false
  },

  Parameter:{
    type:String
  },

  Topic:{
    type:Array,
    "default":[]
  },

  AddedDate: {
    type: Date,
    default: Date.now
  },

});

// User Schema
const UserSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true
  },
  location:{
      type:String,
      required:true,

  },
  createdDate: {
    type: Date,
    default: Date.now
  },

  farm:[
      { farmName:{type:String,index:true}, 
        description:String,
        location:{type:Array,"default":[]},
        farmAddDate:{type:Date,default:Date.now},
        device:[{Device}]
     },

 ],


},
{strict:false}
);

module.exports = User = mongoose.model("users", UserSchema);


//Location Co-Ordiantes Schema
// const pointSchema = new Schema({
//   type: {
//     type: String,
//     default:"Point"
//   },
//   coordinates: {
//     type: [Number],
//     }
// });


示例文档:

[
  {
    "_id": "5d3ac84a86688123789e13b2",
    "name": "Puja",
    "email": "puja@gmail.com",
    "password": "$2a$10$r17zOU1gTJvwoB.nBdRvi.qvDmsJhIlHbp//s8l0KEKgCEvbKoZAu",
    "location": "Delhi",
    "createdDate": "2019-07-26T09:30:50.243Z",
    "farm": [
      {
        "location": [
          "11.33,22.99"
        ],
        "_id": "5d3ff9d68e34cb265c030819",
        "farmName": "Xyz",
        "description": "Testing that info is  saved ",
        "farmAddDate": "2019-07-30T08:03:34.247Z",
        "device": [
          {
            "_id": "5d402def7cef9f20c8272fc4",
            "deviceName": " DeviceX01",
            "deviceType": " Test",
            "description": " This is a test",
            "deviceLocation": "11.22,33.12",
            "Parameter": " temp",
            "Topic": "v1/ DeviceX01/11.22,33.12/ temp"
          }
        ]
      },
      {
        "location": [],
        "_id": "5d42d171e7ceef2a90245470",
        "farmName": "Xilo",
        "description": "This is test from Postman Api Request",
        "farmAddDate": "2019-08-01T11:48:01.883Z",
        "device": [
          {
            "_id": "5d441878f1877637cc712d07",
            "deviceName": "Xki",
            "deviceType": "Muc",
            "description": "This is test for updation of specific data in a specfic farm .Test Pass",
            "deviceLocation": [
              "11",
              "180"
            ],
            "Parameter": "Hello Happy",
            "Topic": "v1/Xki/11,180/Hello Happy"
          },
          {
            "_id": "5d4418953968370e64c32e1f",
            "deviceName": "Xki",
            "deviceType": "Muc",
            "description": "This is test for updation of specific data in a specfic farm .Test Pass",
            "deviceLocation": [
              "11",
              "180"
            ],
            "Parameter": "Hello Happy",
            "Topic": "v1/Xki/11,180/Hello Happy"
          },
          {
            "_id": "5d4425548b3cf92af46e2c9a",
            "deviceName": "Xki",
            "deviceType": "Muc",
            "description": "This is test for updation of specific data in a specfic farm .Test Pass",
            "deviceLocation": [
              "11",
              "180"
            ],
            "Parameter": "Hello Happy 1",
            "Topic": "sagita/5d42d171e7ceef2a90245470/Xki/11,180/Hello Happy 1"
          },
          {
            "_id": "5d44256d8b3cf92af46e2c9b",
            "deviceName": " DeviceX01",
            "deviceType": " Test",
            "description": " This is a test",
            "deviceLocation": "11.22,33.12",
            "Parameter": " set",
            "Topic": "sagita/5d42d171e7ceef2a90245470/ DeviceX01/11.22,33.12/ set",
            "DeviceJson": [
              "{ \"$schema\": \"http: //json-schema.org/draft-04/schema#\", \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"name\": { \"type\": \"string\" }, \"email\": { \"type\": \"string\" }, \"password\": { \"type\": \"string\" }, \"location\": { \"type\": \"string\" }, \"createdDate\": { \"type\": \"Date\" }, \"farm\": { \"type\": \"array\", \"items\": [ { \"type\": \"object\", \"properties\": { \"location\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" } ] }, \"_id\": { \"type\": \"ObjectId\" }, \"farmName\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"farmAddDate\": { \"type\": \"Date\" }, \"device\": { \"type\": \"array\", \"items\": [ { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"string\" }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\" ] } ] } }, \"required\": [ \"location\", \"_id\", \"farmName\", \"description\", \"farmAddDate\", \"device\" ] }, { \"type\": \"object\", \"properties\": { \"location\": { \"type\": \"array\", \"items\": {} }, \"_id\": { \"type\": \"ObjectId\" }, \"farmName\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"farmAddDate\": { \"type\": \"Date\" }, \"device\": { \"type\": \"array\", \"items\": [ { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" }, { \"type\": \"string\" } ] }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\" ] }, { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObejctId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" }, { \"type\": \"string\" } ] }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\" ] }, { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" }, { \"type\": \"string\" } ] }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\" ] }, { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"string\" }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" }, \"DeviceJson\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" } ] } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\", \"DeviceJson\" ] } ] } }, \"required\": [ \"location\", \"_id\", \"farmName\", \"description\", \"farmAddDate\", \"device\" ] }, { \"type\": \"object\", \"properties\": { \"location\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" } ] }, \"_id\": { \"type\": \"ObjectId\" }, \"farmName\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"farmAddDate\": { \"type\": \"Date\" }, \"device\": { \"type\": \"array\", \"items\": [ { \"type\": \"object\", \"properties\": { \"_id\": { \"type\": \"ObjectId\" }, \"deviceName\": { \"type\": \"string\" }, \"deviceType\": { \"type\": \"string\" }, \"description\": { \"type\": \"string\" }, \"deviceLocation\": { \"type\": \"array\", \"items\": [ { \"type\": \"string\" }, { \"type\": \"string\" } ] }, \"Parameter\": { \"type\": \"string\" }, \"Topic\": { \"type\": \"string\" } }, \"required\": [ \"_id\", \"deviceName\", \"deviceType\", \"description\", \"deviceLocation\", \"Parameter\", \"Topic\" ] } ] } }, \"required\": [ \"location\", \"_id\", \"farmName\", \"description\", \"farmAddDate\", \"device\" ] } ] }, \"__v\": { \"type\": \"integer\" }, \"farmModifyDate\": { \"type\": \"Timestamp\" }, \"lastModified\": { \"type\": \"Timestamp\" }, \"deviceModifyDate\": { \"type\": \"Timestamp\" }, \"deviceModified\": { \"type\": \"Timestamp\" } }, \"required\": [ \"_id\", \"name\", \"email\", \"password\", \"location\", \"createdDate\", \"farm\", \"__v\", \"farmModifyDate\", \"lastModified\", \"deviceModifyDate\", \"deviceModified\" ] }"
            ]
          }
        ]
      },
      {
        "location": [
          "11.22,65.33"
        ],
        "_id": "5d492a4a1294110f64196b2d",
        "farmName": "Durgapur",
        "description": "This is a test for frontend to backend",
        "farmAddDate": "2019-08-06T07:20:42.654Z",
        "device": [
          {
            "_id": "5d4bd62900b433276c2845af",
            "deviceName": "Farm @2 Device 001",
            "deviceType": "Intel Edision",
            "description": "This is a test for creation of Data Bucket of Topic",
            "deviceLocation": [
              "12.33",
              "56.33"
            ],
            "Parameter": "Temperature",
            "Topic": "sagita/model/5d492a4a1294110f64196b2d/Farm @2 Device 001/12.33,56.33/Temperature"
          }
        ]
      }
    ],
    "__v": 0,
    "lastModified": "2019-12-04T06:59:17.431Z",
    "farmModifyDate": "6764284414131175425",
    "deviceModifyDate": "6720517743902195713",
    "deviceModified": "6766475118035075073"
  }
]

还有其他方法可以通过farm._id和device._id获取参数字段吗?

【问题讨论】:

  • 你能添加你的User架构吗
  • 你有机会检查我的答案吗?
  • 是的,但是我在执行当前的聚合管道并根据 request.get 在此行中获取结果时遇到了困难.exec((err,result)=>{ if (err) throw err console.log(result) res.send(result) });
  • @SuleymanSah 它仍然返回空数组,但您的管道似乎工作正常
  • @Pritam 我将尝试通过更新答案来修复您的查询。

标签: node.js mongodb mongoose aggregation-framework


【解决方案1】:

您需要更改第二个展开和第二个匹配阶段的顺序,并在匹配字段中使用正确的字段名称。

[
  {
    $match: {
      "farm._id": req.params.farmID
    }
  },
  {
    $unwind: "$farm"
  },
  {
    $unwind: "$farm.device"
  },
  {
    $match: {
      "farm.device._id": req.params.deviceID
    }
  },
  {
    $project: {
      "parameter": "$farm.device.Parameter"
    }
  }
])

Mongoplayground

我也会这样写我的路线:

router.get("/getParameter/:farmID/:deviceID", async (req, res) => {
  try {
    const result = await User.aggregate([
      {
        $match: {
          "farm._id": req.params.farmID
        }
      },
      {
        $unwind: "$farm"
      },
      {
        $unwind: "$farm.device"
      },
      {
        $match: {
          "farm.device._id": req.params.deviceID
        }
      },
      {
        $project: {
          parameter: "$farm.device.Parameter"
        }
      }
    ]);

    if (result.length > 0) {
      res.send(result[0].parameter); //or you can return the object itself:   res.send(result[0])
    } else {
      res.status(400).send("No value found, check your request values");
    }
  } catch (err) {
    console.log(err);
    res.status(500).send("Something went wrong");
  }
});

【讨论】:

  • 我已经尝试过了,但是当我创建 API 时它不会产生正确的输出。状态码始终是 400 No value found, check your request values
  • @Pritam 请检查您的请求参数。正如您在 mongoplayground 中看到的那样,它可以工作
  • 是的,先生,我已经检查过它在 mongoplayground 中是否有效,但我想知道为什么它没有在 node.js 服务器上运行。您检查过它吗,api 请求?
  • @Pritam 我相信这是您的数据以及您如何发送请求参数的问题。如果没有看到您的数据以及您如何发送请求,我无法说任何话。
【解决方案2】:

如果匹配的文档数量有限,则可以使用 $unwind。但是如果匹配的文档数量很大,那么你应该避免使用 $unwind 操作符,因为它会在破坏原始文档后增加文档的数量。 在这种情况下,您应该尝试使用 $filter 运算符在 $project 阶段过滤数组。

以下是您的用例的示例查询,避免使用 $unwind 运算符:

    [ 
        { $match: { farm: { $elemMatch: { _id: "5d3ff9d68e34cb265c030819", "device._id": "5d402def7cef9f20c8272fc4" } } } }, 
        { 
                $project: { 
                        _id: 1, 
                        parameter: { 
                                $let: { 
                                        vars: { farm: { $arrayElemAt: [ { $filter: { input: "$farm", as: "f", cond: { $eq: [ "$$f._id", "5d3ff9d68e34cb265c030819" ] } } }, 0 ] } }, 
                                        in: { 
                                                $let: { 
                                                        vars: { device: { $arrayElemAt: [ { $filter: { input: { $ifNull: [ "$$farm.device", [] ] }, as: "d", cond: { $eq: [ "$$d.deviceName", " DeviceX01" ] } } }, 0 ] } }, 
                                                        in: "$$device.Parameter" 
                                                }
                                        } 
                                } 
                        } 
                } 
        } 
    ]

【讨论】:

    猜你喜欢
    • 2021-07-09
    • 2021-05-14
    • 2018-10-03
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多