arrays - Mongodb array query: find records that contain an outsider in an array
问题描述
I have a database for my image tags with a web UI for labeling the tags. The tags are stored in documents with the following structure:
{
imgID: 'UID of corresponding image',
tagID: 'UID of tag',
coordinates:{x1,y1,x2,y2},
tags:[
{tag:'a', user:'username'},
{tag:'a', user:'username'},
{tag:'typo', user:'username'}
]
}
I am looking for documents that contain incorrect tags, typo
, in my example.
I tried
find({$and:[{'tags.tag':{$ne:'a'}},{'tags.tag':'a'}]}),
and
aggregate([{
$match:{'tags.tag':'a'}
},{
$match:{'tags.tag':{$ne:'a'}}
}])`
This is not a duplicate of Find documents with array that doesn't contains a specific value as I am looking for a document containing an array that DOES contain the value in question while at the same time contains ANY OTHER value. The solutions presented in that question do not work in my situation as shown above.
解决方案
You can use $elemMatch to specify the $ne
part of your query:
db.col.find({ $and:[ {'tags.tag': 'a' }, { tags: { $elemMatch: { 'tag': { $ne: 'a' } } } } ] })
推荐阅读
- oracle - Oracle 从巨大的逗号分隔字符串(200000 个字符)中获取多行
- android - 每天安排不同的通知 Flutter
- hadoop - 无法从配置单元加载数据:-chgrp:'LONEWOLF\Sudarshan' 与组的预期模式不匹配
- python - 在 PB 文件中检查 DL 模型的输入和输出尺寸
- c# - 发送邮件时出现 SMTP 服务器连接错误
- python - 类型错误:remove() 接受 1 个位置参数,但给出了 2 个
- jquery - 如何在 ajax 回调后重新加载 div,而该 div 中没有特定类
- mysql - MYSQL,将数据从一个表复制到另一个表,并以varchar为增量
- javascript - 用javascript中的重复字符替换函数返回字符串
- mysql - Liquibase 使用无效的 MySQL 语法生成更改日志,数据类型为“JSON(1073741824)”