mongodb - 查询 mongo 数据是否包含数组中的值
问题描述
假设我有一个具有这种数据结构的文档:
type Book struct {
Title string `bson:"title, omitempty"`
Tags []string `bson:"tags, omitempty"`
}
如果我想找到一本带有“科幻”和“恐怖”标签的书(生成的书可以包含比这两个更多的标签),我的代码应该是什么样子?
谢谢你。
解决方案
多一点挖掘给了我这个:
filter := bson.M{
"tags": bson.M{
"$all": tags,
},
}
collection.Find(ctx, filter)
推荐阅读
- azure - 如何使用自定义扩展脚本在虚拟机上安装软件
- javascript - 使用jQuery的HTML 5嵌套拖放
- c# - 如何在 C# windows 窗体中使用具有空格的字体来创建 excel 工作表?
- google-search-console - 为什么谷歌搜索控制台需要验证 dns txt?
- c - 制作一个二维数组,用户给出数组的大小 (n*n),数组填充随机数 [1-10] 而不使用 malloc
- flutter - 使用 Flutter 开发网页
- amazon-web-services - 如何使用工件在 AWS 管道中成功部署
- spring-batch - 搜索实际运行的 spring 批处理作业
- python - 在我的应用程序中使用 amazon alexa echo
- c# - 嵌套对象列表中的字典键