如何选择嵌入文档列表作为根元素

时间:2014-05-18 21:32:39

标签: mongodb aggregation-framework nosql

我正在使用MongoDB和mongocsharpdriver。我的数据的一个例子如下。我想找到所有"交易的列表"跨所有"类别"。如何使用collection.find方法获取所有事务的列表?查询是否需要进行"事务"在根级别?

这是我的文件:

/* 0 */
{
    "_id" : ObjectId("53791916a33e851d2c7ec8f0"),
    "name" : "Shopping",
    "transactions" : [ 
        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ee"),
            "date" : "12/01/2012",
            "description" : "mid week",
            "amount" : 12
        }, 
        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ef"),
            "date" : "12/01/2012",
            "description" : "end of week",
            "amount" : 5
        }
    ]
}

/* 1 */
{
    "_id" : ObjectId("53791a63a33e851d2c359290"),
    "name" : "Entertainment",
    "transactions" : [ 
        {
            "_id" : ObjectId("53791a63a33e851d2c35928e"),
            "date" : "12/01/2012",
            "description" : "games",
            "amount" : 70
        }, 
        {
            "_id" : ObjectId("53791a63a33e851d2c35928f"),
            "date" : "12/01/2012",
            "description" : "films",
            "amount" : 20
        }
    ]
}

在robomongo中我使用:db.Categories.find({},{_ id:0,transactions:1})

获取:

/* 0 */
{
    "transactions" : [ 
        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ee"),
            "date" : "12/01/2012",
            "description" : "mid week",
            "amount" : 12
        }, 
        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ef"),
            "date" : "12/01/2012",
            "description" : "end of week",
            "amount" : 5
        }
    ]
}

/* 1 */
{
    "transactions" : [ 
        {
            "_id" : ObjectId("53791a63a33e851d2c35928e"),
            "date" : "12/01/2012",
            "description" : "games",
            "amount" : 70
        }, 
        {
            "_id" : ObjectId("53791a63a33e851d2c35928f"),
            "date" : "12/01/2012",
            "description" : "films",
            "amount" : 20
        }
    ]
}

这不是我想要的,查询应该返回:

        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ee"),
            "date" : "12/01/2012",
            "description" : "mid week",
            "amount" : 12
        }

        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ef"),
            "date" : "12/01/2012",
            "description" : "end of week",
            "amount" : 5
        }

        {
            "_id" : ObjectId("53791a63a33e851d2c35928e"),
            "date" : "12/01/2012",
            "description" : "games",
            "amount" : 70
        }

        {
            "_id" : ObjectId("53791a63a33e851d2c35928f"),
            "date" : "12/01/2012",
            "description" : "films",
            "amount" : 20
        }

提前致谢...

1 个答案:

答案 0 :(得分:0)

每当您想要重塑输出结构时,通常都需要使用aggregate而不是find。在这种情况下,您可以使用$unwind$group运算符来实现您的目标。

在shell中:

db.test.aggregate([
  // Duplicate each doc, once per transactions element.
  {$unwind: '$transactions'},
  // Regroup transactions back together into one big array.
  {$group: {_id: null, transactions: {$push: '$transactions'}}}
  ])

输出:

{
    "result" : [ 
        {
            "_id" : null,
            "transactions" : [ 
                {
                    "_id" : ObjectId("53791916a33e851d2c7ec8ee"),
                    "date" : "12/01/2012",
                    "description" : "mid week",
                    "amount" : 12
                }, 
                {
                    "_id" : ObjectId("53791916a33e851d2c7ec8ef"),
                    "date" : "12/01/2012",
                    "description" : "end of week",
                    "amount" : 5
                }, 
                {
                    "_id" : ObjectId("53791a63a33e851d2c35928e"),
                    "date" : "12/01/2012",
                    "description" : "games",
                    "amount" : 70
                }, 
                {
                    "_id" : ObjectId("53791a63a33e851d2c35928f"),
                    "date" : "12/01/2012",
                    "description" : "films",
                    "amount" : 20
                }
            ]
        }
    ],
    "ok" : 1
}