如何检查一个数组字段在MongoDB中是否包含唯一值或另一个数组? [英] How to check if an array field contains a unique value or another array in MongoDB?

查看:564
本文介绍了如何检查一个数组字段在MongoDB中是否包含唯一值或另一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用mongodb.

I am using mongodb now.

我有Blogpost集合,并且Blogpost具有归档的标签,该标签是一个数组,例如

I have blogpost collection, and blogpost has a tags filed which is an array, e.g.

blogpost1.tags = ['tag1', 'tag2', 'tag3', 'tag4', 'tag5']
blogpost2.tags = ['tag2', 'tag3']
blogpost3.tags = ['tag2', 'tag3', 'tag4', 'tag5']
blogpost4.tags = ['tag1', 'tag4', 'tag5']

如何进行这些搜索

  1. 包含"tag1"
  2. 包含['tag1','tag2'],
  3. 包含['tag3','tag4']中的任何一个

推荐答案

尝试一下:

db.blogpost.find({ 'tags' : 'tag1'}); //1
db.blogpost.find({ 'tags' : { $all : [ 'tag1', 'tag2' ] }}); //2
db.blogpost.find({ 'tags' : { $in : [ 'tag3', 'tag4' ] }}); //3

这篇关于如何检查一个数组字段在MongoDB中是否包含唯一值或另一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆