本文介绍了查找文档,其中数组的所有元素都具有特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这基本上是一个简单的问题,但是我找不到它的查询功能.

This is basically a simple problem, but I couldn't find a query function for it.

{
  _id: 1,
  foo: [
    { bar: 9 },
    { bar: 16 }
  ]
}

{
  _id: 2,
  foo: [
    { bar: 9 },
    { bar: 9 },
    { bar: 9 }
  ]
}

示例输出:

{
  _id: 2,
  foo: [
    { bar: 9 },
    { bar: 9 },
    { bar: 9 }
  ]
}

因为这是每个foo.bar = 9的唯一文档.

Because this is the only document where every foo.bar = 9.

在此文档中的每个foo.bar的foo.bar = 9处查找所有文档."

还是我需要类似在所有文档中查找不在哪里的文件(foo.bar!= 9)"之类的东西?

Or do I need something like "FIND all documents WHERE NOT( foo.bar != 9 )"?

提前谢谢!

推荐答案

db.c.find({
    "foo.bar" : {
        $exists : true
    },
    "foo" : {
        $not : {
            $elemMatch : {
                "bar" : {
                    $ne : 9
                }
            }
        }
    }
});

这篇关于查找文档,其中数组的所有元素都具有特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 18:24