mongodb - mongodb:匹配字符串或数组
问题描述
mongodb 提供了这种语法来匹配一个字符串:
db.inventory.find( { tags: "red" } )
所以它会匹配:
{ "_id" : ObjectId("xxx"), "tags" : "red" }
但它也会匹配数组中的一个元素:
{ "_id" : ObjectId("xxx"), "tags" : [ "blank", "red" ] }
那么我如何匹配数组中的字符串或元素但不能同时匹配两者?
我知道我可以使用$where
,但出于显而易见的原因,我更喜欢更简单的方法(如果有的话):
db.inventory.find({ "$where": "this.tags == 'red'" })
db.inventory.find({ "$where": "Array.isArray(this.tags) && this.tags.includes('red')" })
解决方案
推荐阅读
- python - 使用 TensorFlow 和 tf.data.Dataset 从文件夹中采样一系列图像
- php - PHP在解析JSON数据后将对象与数组混淆
- python-3.x - Keras、Tensorflow 在模型构建时保留所有 GPU 内存
- ruby - 如何迭代 3D 散列
- django - 注释的返回类型
- python - 如何将字符串转换为gmt时间
- node.js - 第一次连接时复制无法连接到服务器 [IP:27017] [MongoError: getaddrinfo ENOTFOUND
- ms-access - 访问 - 调查的数据库关系和数据收集表
- python - 如何使用 vtkOBBTree 和 IntersectWithLine 在 python 中使用 vtk 查找线和 PolyDataFilter 的交点?
- flutter - 在键盘上方颤动额外/不需要的空间