【发布时间】:2017-04-28 01:40:25
【问题描述】:
我正在尝试使用the $type operator 编写查询以匹配可能具有'null' 或'objectid' 类型的字段。当我只查询一种特定类型时,它可以工作:
> db.collections.post.findOne({$or: [{x: {$type: 'null'}}]}).then(console.log)
Promise { <pending> }
> { _id: 584f573f931f2b0bf4e3d339,
date: Mon Dec 12 2016 21:04:47 GMT-0500 (Eastern Standard Time),
title: 'hgfgh',
text: 'dhffgh',
userId: 584f573a931f2b0bf4e3d338,
x: null }
当我编写一个查询同时匹配 x 和 objectid 时,我什么也得不到:
> db.collections.post.findOne({$or: [{x: {$type: 'null'}}, {x: {$type: 'objectid'}}]}).then(console.log)
Promise { <pending> }
我正在使用 node-mongodb-native-2.2。
如果字段是null 或ObjectId,我如何编写一个匹配的查询(我希望为验证器编写这个)?
【问题讨论】:
-
你可以试试 {$or: [{x: {$type: 'null'}}, {x: {$type: 'objectId'}}]} 在对象 id 中使用大写 I 吗?
-
就是这样。这让我觉得自己很愚蠢xD。虽然我真的很想责怪 mongo 的查询,因为它没有将其视为错误并引发异常:-p。
标签: node.js mongodb mongodb-query