MongoDB在不知道键的情况下搜索嵌套对象 [英] MongoDB Search nested Objects without knowing Key

查看:76
本文介绍了MongoDB在不知道键的情况下搜索嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于使用了异步Java驱动程序+ BSON,我得到了一些被赋予任意对象键的对象列表.

I have a list of objects that are given somewhat arbitrary Object keys as a result of using the async Java driver + BSON.

我的问题是, jobStatuses 是任意的Dictionary项列表,在这些项中我不知道键,我也不知道如何访问其子值.最后,我试图建立一个查询,该查询返回给定潜在对象ID列表的 jobStatus.* ._ id 中的ANY是否为真.

My issue is given the fact that jobStatuses are an arbitrary list of Dictionary items where I don't know the key, I have no idea how to access its sub-values. In the end, I'm trying to build a query that returns if ANY of jobStatus.*._id are true given a list of potential Object ID's.

因此,我要提供ID的列表,如果 jobStatuses 中的任何项目都具有给定的ID,则我想返回true.有什么想法吗?

So I'd be giving a list of ID's and want to return true if ANY of the items in jobStatuses have any of the given ID's. Any ideas?

推荐答案

让我们尝试一下:

db.yourCollectionName.aggregate([
    {
        $project: {
            _id: 0,
            jobStatutses: { $arrayElemAt: [{ $objectToArray: "$jobStatutses" }, 0] }
        }
    }, {
        $match: { 'jobStatutses.v._id': { $in: [ObjectId("5d6d8c3a5a0d22d3c84dd6dc"), ObjectId("5d6d8c3a5a0d22d3c84dd6ed")] } }
    }
])

收集数据:

/* 1 */
{
    "_id" : ObjectId("5e06319c400289966eea6a07"),
    "jobStatutses" : {
        "5d6d8c3a5a0d22d3c84dd6dc" : {
            "_id" : ObjectId("5d6d8c3a5a0d22d3c84dd6dc"),
            "accepted" : "123",
            "completed" : 0
        }
    },
    "something" : 1
}

/* 2 */
{
    "_id" : ObjectId("5e0631ad400289966eea6dd1"),
    "jobStatutses" : {
        "5d6d8c3a5a0d22d3c84dd6ed" : {
            "_id" : ObjectId("5d6d8c3a5a0d22d3c84dd6ed"),
            "accepted" : "456",
            "completed" : 0
        }
    },
    "something" : 2
}

/* 3 */
{
    "_id" : ObjectId("5e0631cd400289966eea7542"),
    "jobStatutses" : {
        "5e06319c400289966eea6a07" : {
            "_id" : ObjectId("5e06319c400289966eea6a07"),
            "accepted" : "789",
            "completed" : 0
        }
    },
    "something" : 3
}

输出:

/* 1 */
{
    "jobStatutses" : {
        "k" : "5d6d8c3a5a0d22d3c84dd6dc",
        "v" : {
            "_id" : ObjectId("5d6d8c3a5a0d22d3c84dd6dc"),
            "accepted" : "123",
            "completed" : 0
        }
    }
}

/* 2 */
{
    "jobStatutses" : {
        "k" : "5d6d8c3a5a0d22d3c84dd6ed",
        "v" : {
            "_id" : ObjectId("5d6d8c3a5a0d22d3c84dd6ed"),
            "accepted" : "456",
            "completed" : 0
        }
    }
}

您需要做的只是检查是否从数据库返回了至少一个给定列表的文档,因此我们不必担心文档结构,只需在 result.length 中进行操作即可.您的代码说至少一个文档与输入列表匹配.

All you need is to check if at least one doc gets returned from DB for a given list or not, So we don't need to worry about document structure then just do result.length in your code to say at least one doc got matched for the input list.

这篇关于MongoDB在不知道键的情况下搜索嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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