首页 > 解决方案 > collection.findOne 匹配整个集合中的查询,而我需要它在对象数组中查找

问题描述

我遇到了一个场景,我需要将数据保存到 mongoDB 中,但首先我需要检查对象数组中是否存在数据
我尝试使用collection.findOne()。这是在整个集合中搜索的方法。我需要它在 document->array of Objects->array of Objects 中匹配,在本例中为farm[ ]->device[ ]
即,搜索该场中没有类似的设备名称。
还有其他方法吗?

后端代码

  User.findOne({ 'farm.device.deviceName':deviceName }).then(device => {
    if (device) {
      return res.status(400).json({ "Device Name " :  "Device already exists."});
    } else {
...saving part

我的示例架构

[
  {
    "_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"
  }
]

标签: node.jsmongodbmongoose

解决方案


您可以使查询更具体——例如也可以通过 userId 匹配——以将搜索限制为始终检查 1 个特定文档。

如果您也想将其限制在特定农场,您可以查看 $elemMatch - 它可能有助于匹配特定农场中的 2 个条件。


推荐阅读