【问题标题】:Get one Array of objects mongodb获取一个对象数组mongodb
【发布时间】:2016-12-22 20:50:15
【问题描述】:

我的集合中有很多子文档,我只想获取对象的配置数组(非空)并忽略所有其他对象

我在stackoverflow上尝试了一种解决方案Find MongoDB records where array field is not empty (using Mongoose)

它返回配置数组以及我的集合中的其他对象

这里是样本数据

http://imgur.com/a/RXLGv

这个查询返回我所有的对象配置数组,但它也返回我不想要的空数组,有什么解决方案吗?

return Company.find({}, {
  "configuration": 1
}, {
  "configuration": {
    "$not": {
      "$size": 0
    }
  }
})

样本数据

configuration: [],
configuration: [],
configurtaion: [{
          "id": "2013",
          "name": "TRUEAUTO",
          "locationId": null,
          "deviceIdentifier": "850",
          "serialNumber": "",
          "modelNumber": "OptiPlex 3010",
          "tagNumber": null,
          "purchaseDate": null,
          "installationDate": "2016-07-28T00:00:00.000Z",
          "warrantyExpirationDate": null,
          "vendorNotes": null,
          "notes": null,
          "lastLoginName": null,
          "billFlag": "false",
          "backupServerName": null,
          "backupProtectedDeviceList": null,

          }
        }]

我想忽略空数组,只获取配置对象

【问题讨论】:

    标签: mongodb subdocument


    【解决方案1】:
    //You can use $exist property with $size 
    
    Company.find({ configuration: { $exists: true, $not: {$size: 0} } }, {
      "configuration": 1
    })
    //Or you can check
    Company.find({'configuration.0': {$exists: true}});
    

    【讨论】:

    • 我刚试过,它也给了我其他子文档,这不是必需的。请看截图
    • 嗨山塔努;我认为您的代码中有一些拼写错误,例如“configurtaion”是“configuration”的错误。
    • 第二种方法你试过了吗
    猜你喜欢
    • 2018-06-12
    • 1970-01-01
    • 2014-12-04
    • 2019-09-20
    • 2021-08-07
    • 1970-01-01
    • 2022-10-01
    • 2022-01-10
    • 2020-02-07
    相关资源
    最近更新 更多