包含MongoDB中数组位置的数组位置的复杂查询

时间:2017-11-14 21:26:21

标签: arrays mongodb

我需要知道,我可以进行复杂的查询,它会返回数组板的结果,但只有参数Object在里面:

  • board.name
  • _id
  • 数组(lists.list和数组中的索引位置)
  • 数组中的长度卡

我在mongodb中很弱,所以我问是否有可能这样做,因为我有订单,我不知道是在查询中还是在angularjs中的循环中

这是来自db结果的查询db.getCollection('boards')。find({})(2results)

    /* 1 */
{
    "_id" : ObjectId("5a0b5c6da0502e2174fd849f"),
    "name" : "example 1",
    "users" : [ 
        ObjectId("59cd114cea98d9326ca1c421")
    ],
    "lists" : [ 
        {
            "list" : "example",
            "_id" : ObjectId("5a0b5c7ba0502e2174fd84ae"),
            "cards" : [ 
                {
                    "name" : "1",
                    "_id" : ObjectId("5a0b5c80a0502e2174fd84b4")
                }, 
                {
                    "name" : "2",
                    "_id" : ObjectId("5a0b5c80a0502e2174fd84b3")
                }, 
                {
                    "name" : "3",
                    "_id" : ObjectId("5a0b5c80a0502e2174fd84b2")
                }
            ]
        }, 
        {
            "list" : "example 1",
            "_id" : ObjectId("5a0b5c7ba0502e2174fd84ad"),
            "cards" : [ 
                {
                    "name" : "1",
                    "_id" : ObjectId("5a0b5c83a0502e2174fd84b5") 
                }
            ]
        }, 
        {
            "list" : "example",
            "_id" : ObjectId("5a0b5c7ba0502e2174fd84ac"),
            "cards" : [ 
                {
                    "name" : "2",
                    "_id" : ObjectId("5a0b5c85a0502e2174fd84b6") 
                }
            ]
        }
    ], 
    "__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("5a0b5c71a0502e2174fd84a4"),
    "name" : "example 2",
    "users" : [ 
        ObjectId("59cd114cea98d9326ca1c421")
    ],
    "lists" : [ 
        {
            "list" : "example next 1",
            "_id" : ObjectId("5a0b5c93a0502e2174fd84bc"),
            "cards" : [ 
                {
                    "name" : "1",
                    "_id" : ObjectId("5a0b5c95a0502e2174fd84c2")
                }, 
                {
                    "name" : "2",
                    "_id" : ObjectId("5a0b5c95a0502e2174fd84c1")
                }, 
                {
                    "name" : "3",
                    "_id" : ObjectId("5a0b5c95a0502e2174fd84c0")
                }
            ]
        }, 
        {
            "list" : "example next 2",
            "_id" : ObjectId("5a0b5c93a0502e2174fd84bb"),
            "cards" : [ 
                {
                    "name" : "1",
                    "_id" : ObjectId("5a0b5c98a0502e2174fd84c5")
                }, 
                {
                    "name" : "2",
                    "_id" : ObjectId("5a0b5c98a0502e2174fd84c4")
                }
            ]
        }, 
        {
            "list" : "example next 3",
            "_id" : ObjectId("5a0b5c93a0502e2174fd84ba"),
            "cards" : [ 
                {
                    "name" : "1",
                    "_id" : ObjectId("5a0b5c9aa0502e2174fd84c6")
                }
            ]
        }
    ], 
    "__v" : 0
}

1 个答案:

答案 0 :(得分:0)

是,

您可以使用聚合框架构建自己的响应。

https://docs.mongodb.com/manual/aggregation/

我使用的框架中最有用的阶段是:

$project: Structure what I want.
$match: Very similiar to select in sql.
$group: to make groups
$sort: to sort :D
$unwind: to unwind arrays.

我认为还有更多,但取决于你想要的结构,这里有一个很好的教程:

https://www.tutorialspoint.com/mongodb/mongodb_aggregation.htm