【问题标题】:How do I search and return the field and data in a multi-level array of object?如何在对象的多级数组中搜索并返回字段和数据?
【发布时间】:2020-02-19 08:13:21
【问题描述】:

我有一个由 MongoDB 中的 find 命令返回的数据集。如何在给定数据中搜索 DeviceJson[] 的值和字段


{ 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: [Array],
       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: [Array],
       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: [Array],
       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: [Array] } 
    ] 
  }


我保存在一个名为 usdData=[] 的数组中的数据集并循环通过它的索引,这是结果,我甚至想在架构中搜索内部和 DeviceJson 的值。我该怎么做?

var usdData=[];


      collection.findOne({"email":Username},function (err,doc) {
        if (err) throw err;
         usdData=doc.farm;

           for (let index = 0; index < usdData.length; index++) {

            if (usdData[index].farmName === gotfarmName) {

               return  usdData[index];
            }
             const element = usdData[index];

                 //  const dElement = JSON.stringify(element);
                //   console.log("Array started here \n"+dElement); 
                //   const ddElement= JSON.parse(dElement) ;
                console.log(usdData[1]);


           }

          return console.log();
          });

【问题讨论】:

标签: arrays node.js json object mongodb-query


【解决方案1】:

如果您知道“DeviceJson”结构,您可以执行以下操作:

01) 文档示例:

{
    "_id" : "5d42d171e7ceef2a90245470",
    "location" : [],
    "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" : [],
            "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" : [],
            "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" : [],
            "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" : [ 
                {
                    "_id" : "5db85e439a17899b8ba631cd",
                    "name" : "deviceJsonOne",
                    "complexObject" : {
                        "_id" : "5db85e439a17899b8ba631ce"
                    },
                    "someArray" : [ 
                        {
                            "someThing" : 1
                        }, 
                        {
                            "someThing" : 2
                        }
                    ]
                }, 
                {
                    "_id" : "5dae22702486f7d89ba7633c",
                    "name" : "deviceJsonTwo",
                    "complexObject" : {
                        "_id" : "5db85e439a17899b8ba631cf"
                    },
                    "someArray" : [ 
                        {
                            "someThing" : 3
                        }, 
                        {
                            "someThing" : 2
                        }
                    ]
                }
            ]
        }
    ]
}

02)查询:

    db.Collection.aggregate([
    // INITIAL FILTER... HERE YOU CAN FILTER FOR ANY FILTERS...
    { 
        $match: { "device._id" : "5d44256d8b3cf92af46e2c9b"}
    },
    // TRANSFORMING EVERY ELEMENT OF THE ARRAY 'device' AS A SINGLE ROW/DOCUMENT...
    // EXAMPLE: 
    // {id: 123, "someArray":["A","B","C"]} 
    //          --> unwind : 'someArray' 
    //          --> [{"id": 123, "someArray": "A"},{"id": 123, "someArray": "B"},{"id": 123, "someArray": "C"}]
    {
        $unwind: '$device'
    },
    // FILTERING THE DEVICES THAT HAVE THE 'DeviceJson' PROPOERTY...
    { 
        $match: { "device.DeviceJson" : {$exists: true}}
    },
    // TRANSFORMING EVERY ELEMENT OF THE ARRAY 'DeviceJson' AS A SINGLE ROW/DOCUMENT...
    {
        $unwind: '$device.DeviceJson'
    },
    // FILTERING THE ELEMENTS IN "DeviceJson"...
    { 
        $match: { "device.DeviceJson.complexObject._id" : "5db85e439a17899b8ba631cf"}
    },
    // CREATING A FIELD TO STORE THE RESULT...
    {
        $addFields: {"DeviceJson" : "$device.DeviceJson" }
    },
    // SHOWING ONLY THE FIELD 'DeviceJson' IN THE RESULT...
    {
        $project: {"DeviceJson" : 1}
    }
]);

03) 回应:

{
    "_id" : "5d42d171e7ceef2a90245470",
    "DeviceJson" : {
        "_id" : "5dae22702486f7d89ba7633c",
        "name" : "deviceJsonTwo",
        "complexObject" : {
            "_id" : "5db85e439a17899b8ba631cf"
        },
        "someArray" : [ 
            {
                "someThing" : 3
            }, 
            {
                "someThing" : 2
            }
        ]
    }
}

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2015-07-06
    • 2019-06-30
    • 2021-09-12
    • 2013-08-29
    • 2023-04-09
    相关资源
    最近更新 更多