MongoDB 嵌套数组查询 [英] MongoDB nested array query

查看:28
本文介绍了MongoDB 嵌套数组查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个是作为对another 问题,并且还发布了一个 问题".到目前为止没有回应,所以我求助于提出一个单独的问题.

I've asked this as a comment on another question, and also posted a question on mongodb-user. No responses so far, so I'm resorting to asking a separate question.

文档 指出:

如果该字段包含一个数组,则 $in 运算符选择其字段包含至少包含一个数组的文档与指定数组中的值匹配的元素(例如,等)

If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (e.g. , , etc.)

我正在使用:

mongod --version:
db version v2.2.2, pdfile version 4.5
Thu May 30 12:19:12 git version: d1b43b61a5308c4ad0679d34b262c5af9d664267

mongo --version:
MongoDB shell version: 2.0.4

在 MongoDB shell 中:

In MongoDB shell:

db.nested.insert({'level1': {'level2': [['item00', 'item01'], ['item10', 'item11']]}})

以下是应根据文档工作的查询列表及其产生的结果:

Here's a list of queries that should work according to the documentation, and the results they produce:

为什么这不起作用?

> db.nested.findOne({'level1.level2.0': 'item00'})
null

为什么我需要 $all?

Why do I need the $all?

> db.nested.findOne({'level1.level2.0': {'$all': ['item00']}})
{
    "_id" : ObjectId("51a7a4c0909dfd8872f52ed7"),
    "level1" : {
        "level2" : [
            [
                "item00",
                "item01"
            ],
            [
                "item10",
                "item11"
            ]
        ]
    }
}

以下至少一项应该有效,对吗?

At least one of the following should work, right?

> db.nested.findOne({'level1.level2.0': {'$in': ['item00']}})
null

> db.nested.findOne({'level1.level2': {'$in': ['item00']}})
null

有什么想法吗?如果查询语法不像宣传的那样有效,我们正在考虑放弃 MongoDB.

Any ideas? We're considering abandoning MongoDB if the query syntax doesn't work as advertised.

谢谢!

推荐答案

使用嵌套的 elemMatch 在数组中搜索嵌套级别.

Use nested elemMatch to search nested levels within arrays.

详情查询MongoDB中的数组

这篇关于MongoDB 嵌套数组查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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