MongoDB查询数组属性ne null不起作用

时间:2018-09-12 10:24:18

标签: mongodb

我正在尝试执行如下查询: {array.0.property: {$ne: null}}。 即使所有文档都具有不同于null的此属性,也不会返回任何内容。 经过一些测试后,我注意到它可以使用$ elemMatch进行工作,但是我只需要查询数组的第一个元素。 第一个元素应被视为所有查询都应在其中搜索的“主”元素。 我无法更改文档“模式”。 有人知道豪能解决这个问题吗? 我正在使用Mongodb 3.6.8。 感谢您的建议。

查询示例:

db.getCollection('tasks').find({'details.0.code': {$ne: null}});

示例文档:

{
  "name": "test",
  "date": 2018-07-17 06:30:00.000Z,
  .....,
  "details": [
     {
       "code": '123',
       "description": 'something',
       "resolutionYear": 2018
     },
     {
       "code": null,
       "description": 'secondary',
       "resolutionYear": 2019
     }
  ]
},
{
  "name": "exam",
  "date": 2018-09-20 09:00:00.000Z,
  .....,
  "details": [
     {
       "code": null,
       "description": 'exam',
       "resolutionYear": null
     }
  ]
}

0 个答案:

没有答案
相关问题